Path: blob/main/crates/bevy_reflect/derive/src/struct_utility.rs
6599 views
use crate::ReflectStruct;12/// A helper struct for creating remote-aware field accessors.3///4/// These are "remote-aware" because when a field is a remote field, it uses a [`transmute`] internally5/// to access the field.6///7/// [`transmute`]: std::mem::transmute8pub(crate) struct FieldAccessors {9/// The referenced field accessors, such as `&self.foo`.10pub fields_ref: Vec<proc_macro2::TokenStream>,11/// The mutably referenced field accessors, such as `&mut self.foo`.12pub fields_mut: Vec<proc_macro2::TokenStream>,13/// The ordered set of field indices (basically just the range of [0, `field_count`).14pub field_indices: Vec<usize>,15/// The number of fields in the reflected struct.16pub field_count: usize,17}1819impl FieldAccessors {20pub fn new(reflect_struct: &ReflectStruct) -> Self {21let (fields_ref, fields_mut): (Vec<_>, Vec<_>) = reflect_struct22.active_fields()23.map(|field| {24(25reflect_struct.access_for_field(field, false),26reflect_struct.access_for_field(field, true),27)28})29.unzip();3031let field_count = fields_ref.len();32let field_indices = (0..field_count).collect();3334Self {35fields_ref,36fields_mut,37field_indices,38field_count,39}40}41}424344