pub trait Struct: Reflect {
    // Required methods
    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 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§

source

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

Returns a reference to the value of the field named name as a &dyn Reflect.

source

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.

source

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.

source

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.

source

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

Returns the name of the field with index index.

source

fn field_len(&self) -> usize

Returns the number of fields in the struct.

source

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

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

source

fn clone_dynamic(&self) -> DynamicStruct

Clones the struct into a DynamicStruct.

Trait Implementations§

source§

impl GetField for dyn Struct

source§

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

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

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

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

Implementors§

source§

impl Struct for Name

source§

impl Struct for ComponentTicks

source§

impl Struct for Tick

source§

impl Struct for EntityHash
where EntityHash: Any + Send + Sync,

source§

impl Struct for AxisSettings

source§

impl Struct for ButtonAxisSettings

source§

impl Struct for ButtonSettings

source§

impl Struct for GamepadAxisChangedEvent

source§

impl Struct for GamepadButtonChangedEvent

source§

impl Struct for GamepadButtonInput

source§

impl Struct for GamepadConnectionEvent

source§

impl Struct for GamepadInfo

source§

impl Struct for GamepadSettings

source§

impl Struct for KeyboardInput

source§

impl Struct for MouseButtonInput

source§

impl Struct for MouseMotion

source§

impl Struct for MouseWheel

source§

impl Struct for Gamepad

source§

impl Struct for GamepadAxis

source§

impl Struct for GamepadButton

source§

impl Struct for TouchInput

source§

impl Struct for BVec2

source§

impl Struct for BVec3

source§

impl Struct for BVec4

source§

impl Struct for Mat2

source§

impl Struct for Mat3

source§

impl Struct for Mat4

source§

impl Struct for Quat

source§

impl Struct for Vec2

source§

impl Struct for Vec3

source§

impl Struct for Vec4

source§

impl Struct for IVec2

source§

impl Struct for IVec3

source§

impl Struct for IVec4

source§

impl Struct for Capsule2d

source§

impl Struct for Capsule3d

source§

impl Struct for Circle

source§

impl Struct for Cone

source§

impl Struct for ConicalFrustum

source§

impl Struct for Cuboid

source§

impl Struct for Cylinder

source§

impl Struct for Ellipse

source§

impl Struct for Line2d

source§

impl Struct for Line3d

source§

impl Struct for Plane2d

source§

impl Struct for Plane3d

source§

impl Struct for Rectangle

source§

impl Struct for RegularPolygon

source§

impl Struct for Segment2d

source§

impl Struct for Segment3d

source§

impl Struct for Sphere

source§

impl Struct for Torus

source§

impl Struct for Triangle2d

source§

impl Struct for Affine2

source§

impl Struct for Affine3A

source§

impl Struct for DAffine2

source§

impl Struct for DAffine3

source§

impl Struct for DMat2

source§

impl Struct for DMat3

source§

impl Struct for DMat4

source§

impl Struct for DQuat

source§

impl Struct for DVec2

source§

impl Struct for DVec3

source§

impl Struct for DVec4

source§

impl Struct for I64Vec2

source§

impl Struct for I64Vec3

source§

impl Struct for I64Vec4

source§

impl Struct for IRect

source§

impl Struct for Mat3A

source§

impl Struct for Rect

source§

impl Struct for U64Vec2

source§

impl Struct for U64Vec3

source§

impl Struct for U64Vec4

source§

impl Struct for URect

source§

impl Struct for Vec3A

source§

impl Struct for UVec2

source§

impl Struct for UVec3

source§

impl Struct for UVec4

source§

impl Struct for Fixed

source§

impl Struct for Real

source§

impl Struct for Stopwatch

source§

impl Struct for Timer

source§

impl Struct for Virtual

source§

impl Struct for Transform

source§

impl Struct for Cursor

source§

impl Struct for CursorEntered

source§

impl Struct for CursorLeft

source§

impl Struct for CursorMoved

source§

impl Struct for EnabledButtons

source§

impl Struct for InternalWindowState

source§

impl Struct for PrimaryWindow

source§

impl Struct for ReceivedCharacter

source§

impl Struct for RequestRedraw

source§

impl Struct for Window

source§

impl Struct for WindowBackendScaleFactorChanged

source§

impl Struct for WindowCloseRequested

source§

impl Struct for WindowClosed

source§

impl Struct for WindowCreated

source§

impl Struct for WindowDestroyed

source§

impl Struct for WindowFocused

source§

impl Struct for WindowMoved

source§

impl Struct for WindowOccluded

source§

impl Struct for WindowResizeConstraints

source§

impl Struct for WindowResized

source§

impl Struct for WindowResolution

source§

impl Struct for WindowScaleFactorChanged

source§

impl Struct for WindowThemeChanged

source§

impl Struct for DynamicStruct

source§

impl<T> Struct for ButtonInput<T>
where T: Copy + Eq + Hash + Send + Sync + 'static + TypePath, ButtonInput<T>: Any + Send + Sync, HashSet<T>: FromReflect + TypePath,

source§

impl<T> Struct for Time<T>

source§

impl<const N: usize> Struct for Polygon<N>
where Polygon<N>: Any + Send + Sync, [Vec2; N]: FromReflect + TypePath,

source§

impl<const N: usize> Struct for Polyline2d<N>

source§

impl<const N: usize> Struct for Polyline3d<N>