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