pub trait Struct: Reflect {
fn field(&self, name: &str) -> Option<&(dyn Reflect + 'static)>;
fn field_mut(&mut self, name: &str) -> Option<&mut (dyn Reflect + 'static)>;
fn field_at(&self, index: usize) -> Option<&(dyn Reflect + 'static)>;
fn field_at_mut(
&mut self,
index: usize
) -> Option<&mut (dyn Reflect + 'static)>;
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 reflected Rust regular struct type.
Implementors of this trait allow their fields to be addressed by name as well as by index.
This trait is automatically implemented for struct
types with named fields
when using #[derive(Reflect)]
.
Example
use bevy_reflect::{Reflect, Struct};
#[derive(Reflect)]
struct Foo {
bar: String,
}
let foo = Foo { bar: "Hello, world!".to_string() };
assert_eq!(foo.field_len(), 1);
assert_eq!(foo.name_at(0), Some("bar"));
let bar = foo.field("bar").unwrap();
assert_eq!(bar.downcast_ref::<String>(), Some(&"Hello, world!".to_string()));
Required Methods§
fn field(&self, name: &str) -> Option<&(dyn Reflect + 'static)>
fn field(&self, name: &str) -> Option<&(dyn Reflect + 'static)>
Returns a reference to the value of the field named name
as a &dyn Reflect
.
fn field_mut(&mut self, name: &str) -> Option<&mut (dyn Reflect + 'static)>
fn field_mut(&mut self, name: &str) -> Option<&mut (dyn Reflect + 'static)>
Returns a mutable reference to the value of the field named name
as a
&mut dyn Reflect
.
fn field_at(&self, index: usize) -> Option<&(dyn Reflect + 'static)>
fn field_at(&self, index: usize) -> Option<&(dyn Reflect + 'static)>
Returns a reference to the value of the field with index index
as a
&dyn Reflect
.
fn field_at_mut(&mut self, index: usize) -> Option<&mut (dyn Reflect + 'static)>
fn field_at_mut(&mut self, index: usize) -> Option<&mut (dyn Reflect + 'static)>
Returns a mutable reference to the value of the field with index index
as a &mut dyn Reflect
.
fn iter_fields(&self) -> FieldIter<'_> ⓘ
fn iter_fields(&self) -> FieldIter<'_> ⓘ
Returns an iterator over the values of the reflectable fields for this struct.
fn clone_dynamic(&self) -> DynamicStruct
fn clone_dynamic(&self) -> DynamicStruct
Clones the struct into a DynamicStruct
.