Trait bevy_reflect::Struct
source · pub trait Struct: Reflect {
// Required methods
fn field(&self, name: &str) -> Option<&dyn Reflect>;
fn field_mut(&mut self, name: &str) -> Option<&mut dyn Reflect>;
fn field_at(&self, index: usize) -> Option<&dyn Reflect>;
fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>;
fn name_at(&self, index: usize) -> Option<&str>;
fn field_len(&self) -> usize;
fn iter_fields(&self) -> FieldIter<'_> ⓘ;
fn clone_dynamic(&self) -> DynamicStruct;
}
Expand description
A trait used to power struct-like operations via reflection.
This trait uses the Reflect
trait to allow implementors to have their fields
be dynamically addressed by both name and index.
When using #[derive(Reflect)]
on a standard struct,
this trait will be automatically implemented.
This goes for unit structs as well.
Example
use bevy_reflect::{Reflect, Struct};
#[derive(Reflect)]
struct Foo {
bar: u32,
}
let foo = Foo { bar: 123 };
assert_eq!(foo.field_len(), 1);
assert_eq!(foo.name_at(0), Some("bar"));
let field: &dyn Reflect = foo.field("bar").unwrap();
assert_eq!(field.downcast_ref::<u32>(), Some(&123));
Required Methods§
sourcefn field(&self, name: &str) -> Option<&dyn Reflect>
fn field(&self, name: &str) -> Option<&dyn Reflect>
Returns a reference to the value of the field named name
as a &dyn Reflect
.
sourcefn field_mut(&mut self, name: &str) -> Option<&mut dyn Reflect>
fn field_mut(&mut self, name: &str) -> Option<&mut dyn Reflect>
Returns a mutable reference to the value of the field named name
as a
&mut dyn Reflect
.
sourcefn field_at(&self, index: usize) -> Option<&dyn Reflect>
fn field_at(&self, index: usize) -> Option<&dyn Reflect>
Returns a reference to the value of the field with index index
as a
&dyn Reflect
.
sourcefn field_at_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>
fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>
Returns a mutable reference to the value of the field with index index
as a &mut dyn Reflect
.
sourcefn name_at(&self, index: usize) -> Option<&str>
fn name_at(&self, index: usize) -> Option<&str>
Returns the name of the field with index index
.
sourcefn iter_fields(&self) -> FieldIter<'_> ⓘ
fn iter_fields(&self) -> FieldIter<'_> ⓘ
Returns an iterator over the values of the reflectable fields for this struct.
sourcefn clone_dynamic(&self) -> DynamicStruct
fn clone_dynamic(&self) -> DynamicStruct
Clones the struct into a DynamicStruct
.