Skip to main content

Struct

Trait Struct 

pub trait Struct: PartialReflect {
    // Required methods
    fn field(&self, name: &str) -> Option<&(dyn PartialReflect + 'static)>;
    fn field_mut(
        &mut self,
        name: &str,
    ) -> Option<&mut (dyn PartialReflect + 'static)>;
    fn field_at(&self, index: usize) -> Option<&(dyn PartialReflect + 'static)>;
    fn field_at_mut(
        &mut self,
        index: usize,
    ) -> Option<&mut (dyn PartialReflect + 'static)>;
    fn name_at(&self, index: usize) -> Option<&str>;
    fn index_of_name(&self, name: &str) -> Option<usize>;
    fn field_len(&self) -> usize;
    fn iter_fields(&self) -> FieldIter<'_> ;

    // Provided methods
    fn to_dynamic_struct(&self) -> DynamicStruct { ... }
    fn get_represented_struct_info(&self) -> Option<&'static StructInfo> { ... }
}
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::{PartialReflect, Reflect, structs::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 PartialReflect = foo.field("bar").unwrap();
assert_eq!(field.try_downcast_ref::<u32>(), Some(&123));

Required Methods§

fn field(&self, name: &str) -> Option<&(dyn PartialReflect + 'static)>

Gets a reference to the value of the field named name as a &dyn PartialReflect.

fn field_mut( &mut self, name: &str, ) -> Option<&mut (dyn PartialReflect + 'static)>

Gets a mutable reference to the value of the field named name as a &mut dyn PartialReflect.

fn field_at(&self, index: usize) -> Option<&(dyn PartialReflect + 'static)>

Gets a reference to the value of the field with index index as a &dyn PartialReflect.

fn field_at_mut( &mut self, index: usize, ) -> Option<&mut (dyn PartialReflect + 'static)>

Gets a mutable reference to the value of the field with index index as a &mut dyn PartialReflect.

fn name_at(&self, index: usize) -> Option<&str>

Gets the name of the field with index index.

fn index_of_name(&self, name: &str) -> Option<usize>

Gets the index of the field with the given name.

fn field_len(&self) -> usize

Returns the number of fields in the struct.

fn iter_fields(&self) -> FieldIter<'_>

Returns an iterator over the values of the reflectable fields for this struct.

Provided Methods§

fn to_dynamic_struct(&self) -> DynamicStruct

Creates a new DynamicStruct from this struct.

fn get_represented_struct_info(&self) -> Option<&'static StructInfo>

Will return None if TypeInfo is not available.

Trait Implementations§

§

impl GetField for dyn Struct

§

fn get_field<T>(&self, name: &str) -> Option<&T>
where T: Reflect,

Gets a reference to the value of the field named name, downcast to T.
§

fn get_field_mut<T>(&mut self, name: &str) -> Option<&mut T>
where T: Reflect,

Gets a mutable reference to the value of the field named name, downcast to T.
§

impl<'a> IntoIterator for &'a (dyn Struct + 'static)

§

type Item = (&'a str, &'a (dyn PartialReflect + 'static))

The type of the elements being iterated over.
§

type IntoIter = FieldIter<'a>

Which kind of iterator are we turning this into?
§

fn into_iter(self) -> <&'a (dyn Struct + 'static) as IntoIterator>::IntoIter

Creates an iterator from a value. Read more

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

§

impl Struct for CuMsgOrigin

§

fn field(&self, name: &str) -> Option<&(dyn PartialReflect + 'static)>

§

fn field_mut( &mut self, name: &str, ) -> Option<&mut (dyn PartialReflect + 'static)>

§

fn field_at(&self, index: usize) -> Option<&(dyn PartialReflect + 'static)>

§

fn field_at_mut( &mut self, index: usize, ) -> Option<&mut (dyn PartialReflect + 'static)>

§

fn name_at(&self, index: usize) -> Option<&str>

§

fn index_of_name(&self, name: &str) -> Option<usize>

§

fn field_len(&self) -> usize

§

fn iter_fields(&self) -> FieldIter<'_>

§

fn to_dynamic_struct(&self) -> DynamicStruct

§

impl Struct for CuTimeRange

§

fn field(&self, name: &str) -> Option<&(dyn PartialReflect + 'static)>

§

fn field_mut( &mut self, name: &str, ) -> Option<&mut (dyn PartialReflect + 'static)>

§

fn field_at(&self, index: usize) -> Option<&(dyn PartialReflect + 'static)>

§

fn field_at_mut( &mut self, index: usize, ) -> Option<&mut (dyn PartialReflect + 'static)>

§

fn name_at(&self, index: usize) -> Option<&str>

§

fn index_of_name(&self, name: &str) -> Option<usize>

§

fn field_len(&self) -> usize

§

fn iter_fields(&self) -> FieldIter<'_>

§

fn to_dynamic_struct(&self) -> DynamicStruct

§

impl Struct for PartialCuTimeRange

§

fn field(&self, name: &str) -> Option<&(dyn PartialReflect + 'static)>

§

fn field_mut( &mut self, name: &str, ) -> Option<&mut (dyn PartialReflect + 'static)>

§

fn field_at(&self, index: usize) -> Option<&(dyn PartialReflect + 'static)>

§

fn field_at_mut( &mut self, index: usize, ) -> Option<&mut (dyn PartialReflect + 'static)>

§

fn name_at(&self, index: usize) -> Option<&str>

§

fn index_of_name(&self, name: &str) -> Option<usize>

§

fn field_len(&self) -> usize

§

fn iter_fields(&self) -> FieldIter<'_>

§

fn to_dynamic_struct(&self) -> DynamicStruct

Implementors§

§

impl Struct for DynamicStruct

§

impl Struct for OffsetAccess

Source§

impl<I> Struct for CuSimSinkTask<I>
where CuSimSinkTask<I>: Any + Send + Sync,

Source§

impl<O> Struct for CuSimSrcTaskPack<O>
where CuSimSrcTaskPack<O>: Any + Send + Sync,

Source§

impl<T, O> Struct for CuAsyncSrcTask<T, O>
where CuAsyncSrcTask<T, O>: Any + Send + Sync, T: for<'m> CuSrcTask<Output<'m> = CuMsg<O>> + Send + 'static, O: CuMsgPayload + Send + 'static,

Source§

impl<T, O> Struct for CuAsyncTask<T, O>
where CuAsyncTask<T, O>: Any + Send + Sync, T: for<'m> CuTask<Output<'m> = CuMsg<O>> + Send + 'static, O: CuMsgPayload + Send + 'static,

Source§

impl<T> Struct for CuSimSrcTask<T>
where CuSimSrcTask<T>: Any + Send + Sync,

Source§

impl<Tx: BridgeChannelSet + 'static, Rx: BridgeChannelSet + 'static> Struct for CuSimBridge<Tx, Rx>
where CuSimBridge<Tx, Rx>: Any + Send + Sync,