Trait bevy::reflect::Enum

pub trait Enum: Reflect {
Show 14 methods // Required methods fn field(&self, name: &str) -> Option<&(dyn Reflect + 'static)>; fn field_at(&self, index: usize) -> Option<&(dyn Reflect + 'static)>; fn field_mut(&mut self, name: &str) -> Option<&mut (dyn Reflect + 'static)>; fn field_at_mut( &mut self, index: usize ) -> Option<&mut (dyn Reflect + 'static)>; fn index_of(&self, name: &str) -> Option<usize>; fn name_at(&self, index: usize) -> Option<&str>; fn iter_fields(&self) -> VariantFieldIter<'_> ; fn field_len(&self) -> usize; fn variant_name(&self) -> &str; fn variant_index(&self) -> usize; fn variant_type(&self) -> VariantType; fn clone_dynamic(&self) -> DynamicEnum; // Provided methods fn is_variant(&self, variant_type: VariantType) -> bool { ... } fn variant_path(&self) -> String { ... }
}
Expand description

A trait used to power enum-like operations via reflection.

This allows enums to be processed and modified dynamically at runtime without necessarily knowing the actual type. Enums are much more complex than their struct counterparts. As a result, users will need to be mindful of conventions, considerations, and complications when working with this trait.

§Variants

An enum is a set of choices called variants. An instance of an enum can only exist as one of these choices at any given time. Consider Rust’s Option<T>. It’s an enum with two variants: None and Some. If you’re None, you can’t be Some and vice versa.

⚠️ This is very important: The Enum trait represents an enum as one of its variants. It does not represent the entire enum since that’s not true to how enums work.

Variants come in a few flavors:

Variant TypeSyntax
UnitMyEnum::Foo
TupleMyEnum::Foo( i32, i32 )
StructMyEnum::Foo{ value: String }

As you can see, a unit variant contains no fields, while tuple and struct variants can contain one or more fields. The fields in a tuple variant is defined by their order within the variant. Index 0 represents the first field in the variant and so on. Fields in struct variants (excluding tuple structs), on the other hand, are represented by a name.

§Implementation

💡 This trait can be automatically implemented using #[derive(Reflect)] on an enum definition.

Despite the fact that enums can represent multiple states, traits only exist in one state and must be applied to the entire enum rather than a particular variant. Because of this limitation, the Enum trait must not only represent any of the three variant types, but also define the methods for all three as well.

What does this mean? It means that even though a unit variant contains no fields, a representation of that variant using the Enum trait will still contain methods for accessing fields! Again, this is to account for all three variant types.

We recommend using the built-in #[derive(Reflect)] macro to automatically handle all the implementation details for you. However, if you must implement this trait manually, there are a few things to keep in mind…

§Field Order

While tuple variants identify their fields by the order in which they are defined, struct variants identify fields by their name. However, both should allow access to fields by their defined order.

The reason all fields, regardless of variant type, need to be accessible by their order is due to field iteration. We need a way to iterate through each field in a variant, and the easiest way of achieving that is through the use of field order.

The derive macro adds proper struct variant handling for Enum::index_of, Enum::name_at and Enum::field_at[_mut] methods. The first two methods are required for all struct variant types. By convention, implementors should also handle the last method as well, but this is not a strict requirement.

§Field Names

Implementors may choose to handle Enum::index_of, Enum::name_at, and Enum::field[_mut] for tuple variants by considering stringified usizes to be valid names (such as "3"). This isn’t wrong to do, but the convention set by the derive macro is that it isn’t supported. It’s preferred that these strings be converted to their proper usize representations and the Enum::field_at[_mut] methods be used instead.

Required Methods§

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

Returns a reference to the value of the field (in the current variant) with the given name.

For non-VariantType::Struct variants, this should return None.

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

Returns a reference to the value of the field (in the current variant) at the given index.

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

Returns a mutable reference to the value of the field (in the current variant) with the given name.

For non-VariantType::Struct variants, this should return None.

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

Returns a mutable reference to the value of the field (in the current variant) at the given index.

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

Returns the index of the field (in the current variant) with the given name.

For non-VariantType::Struct variants, this should return None.

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

Returns the name of the field (in the current variant) with the given index.

For non-VariantType::Struct variants, this should return None.

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

Returns an iterator over the values of the current variant’s fields.

fn field_len(&self) -> usize

Returns the number of fields in the current variant.

fn variant_name(&self) -> &str

The name of the current variant.

fn variant_index(&self) -> usize

The index of the current variant.

fn variant_type(&self) -> VariantType

The type of the current variant.

fn clone_dynamic(&self) -> DynamicEnum

Provided Methods§

fn is_variant(&self, variant_type: VariantType) -> bool

Returns true if the current variant’s type matches the given one.

fn variant_path(&self) -> String

Returns the full path to the current variant.

Implementations on Foreign Types§

§

impl<T> Enum for Option<T>
where Option<T>: Any + Send + Sync, T: TypePath + FromReflect + RegisterForReflection,

§

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

§

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

§

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

§

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

§

fn index_of(&self, __name_param: &str) -> Option<usize>

§

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

§

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

§

fn field_len(&self) -> usize

§

fn variant_name(&self) -> &str

§

fn variant_index(&self) -> usize

§

fn variant_type(&self) -> VariantType

§

fn clone_dynamic(&self) -> DynamicEnum

Implementors§

§

impl Enum for Interpolation

§

impl Enum for Keyframes
where Keyframes: Any + Send + Sync, Vec<Quat>: FromReflect + TypePath + RegisterForReflection, Vec<Vec3>: FromReflect + TypePath + RegisterForReflection, Vec<f32>: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for RepeatAnimation
where RepeatAnimation: Any + Send + Sync, u32: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for PlaybackMode

§

impl Enum for Color
where Color: Any + Send + Sync, Srgba: FromReflect + TypePath + RegisterForReflection, LinearRgba: FromReflect + TypePath + RegisterForReflection, Hsla: FromReflect + TypePath + RegisterForReflection, Hsva: FromReflect + TypePath + RegisterForReflection, Hwba: FromReflect + TypePath + RegisterForReflection, Laba: FromReflect + TypePath + RegisterForReflection, Lcha: FromReflect + TypePath + RegisterForReflection, Oklaba: FromReflect + TypePath + RegisterForReflection, Oklcha: FromReflect + TypePath + RegisterForReflection, Xyza: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for BloomCompositeMode

§

impl Enum for Camera3dDepthLoadOp
where Camera3dDepthLoadOp: Any + Send + Sync, f32: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for ScreenSpaceTransmissionQuality

§

impl Enum for Sensitivity
where Sensitivity: Any + Send + Sync,

§

impl Enum for DebandDither

§

impl Enum for Tonemapping
where Tonemapping: Any + Send + Sync,

§

impl Enum for GizmoLineJoint
where GizmoLineJoint: Any + Send + Sync, u32: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for GizmoLineStyle

§

impl Enum for LightGizmoColor
where LightGizmoColor: Any + Send + Sync, Color: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for ButtonState
where ButtonState: Any + Send + Sync,

§

impl Enum for GamepadConnection
where GamepadConnection: Any + Send + Sync, GamepadInfo: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for GamepadEvent
where GamepadEvent: Any + Send + Sync, GamepadConnectionEvent: FromReflect + TypePath + RegisterForReflection, GamepadButtonChangedEvent: FromReflect + TypePath + RegisterForReflection, GamepadAxisChangedEvent: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for Key
where Key: Any + Send + Sync, SmolStr: FromReflect + TypePath + RegisterForReflection, NativeKey: FromReflect + TypePath + RegisterForReflection, Option<char>: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for NativeKey
where NativeKey: Any + Send + Sync, u32: FromReflect + TypePath + RegisterForReflection, u16: FromReflect + TypePath + RegisterForReflection, SmolStr: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for NativeKeyCode
where NativeKeyCode: Any + Send + Sync, u32: FromReflect + TypePath + RegisterForReflection, u16: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for MouseScrollUnit

§

impl Enum for GamepadAxisType
where GamepadAxisType: Any + Send + Sync, u8: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for GamepadButtonType
where GamepadButtonType: Any + Send + Sync, u8: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for KeyCode
where KeyCode: Any + Send + Sync, NativeKeyCode: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for MouseButton
where MouseButton: Any + Send + Sync, u16: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for ForceTouch
where ForceTouch: Any + Send + Sync, f64: FromReflect + TypePath + RegisterForReflection, Option<f64>: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for TouchPhase
where TouchPhase: Any + Send + Sync,

§

impl Enum for ClusterConfig
where ClusterConfig: Any + Send + Sync, UVec3: FromReflect + TypePath + RegisterForReflection, ClusterZConfig: FromReflect + TypePath + RegisterForReflection, bool: FromReflect + TypePath + RegisterForReflection, u32: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for ClusterFarZMode
where ClusterFarZMode: Any + Send + Sync, f32: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for FogFalloff
where FogFalloff: Any + Send + Sync, f32: FromReflect + TypePath + RegisterForReflection, Vec3: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for OpaqueRendererMethod

§

impl Enum for ParallaxMappingMethod
where ParallaxMappingMethod: Any + Send + Sync, u32: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for ScreenSpaceAmbientOcclusionQualityLevel

§

impl Enum for ShadowFilteringMethod

§

impl Enum for NormalizedRenderTarget
where NormalizedRenderTarget: Any + Send + Sync, NormalizedWindowRef: FromReflect + TypePath + RegisterForReflection, Handle<Image>: FromReflect + TypePath + RegisterForReflection, ManualTextureViewHandle: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for RenderTarget
where RenderTarget: Any + Send + Sync, WindowRef: FromReflect + TypePath + RegisterForReflection, Handle<Image>: FromReflect + TypePath + RegisterForReflection, ManualTextureViewHandle: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for ScalingMode
where ScalingMode: Any + Send + Sync, f32: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for Indices
where Indices: Any + Send + Sync, Vec<u16>: FromReflect + TypePath + RegisterForReflection, Vec<u32>: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for AlphaMode
where AlphaMode: Any + Send + Sync, f32: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for ClearColorConfig
where ClearColorConfig: Any + Send + Sync, Color: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for Msaa
where Msaa: Any + Send + Sync,

§

impl Enum for Projection
where Projection: Any + Send + Sync, PerspectiveProjection: FromReflect + TypePath + RegisterForReflection, OrthographicProjection: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for Visibility
where Visibility: Any + Send + Sync,

§

impl Enum for Anchor
where Anchor: Any + Send + Sync, Vec2: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for ImageScaleMode
where ImageScaleMode: Any + Send + Sync, TextureSlicer: FromReflect + TypePath + RegisterForReflection, bool: FromReflect + TypePath + RegisterForReflection, f32: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for SliceScaleMode
where SliceScaleMode: Any + Send + Sync, f32: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for BreakLineOn
where BreakLineOn: Any + Send + Sync,

§

impl Enum for JustifyText
where JustifyText: Any + Send + Sync,

§

impl Enum for TimerMode
where TimerMode: Any + Send + Sync,

§

impl Enum for AlignContent

§

impl Enum for AlignItems
where AlignItems: Any + Send + Sync,

§

impl Enum for AlignSelf
where AlignSelf: Any + Send + Sync,

§

impl Enum for Direction
where Direction: Any + Send + Sync,

§

impl Enum for Display
where Display: Any + Send + Sync,

§

impl Enum for FlexDirection

§

impl Enum for FlexWrap
where FlexWrap: Any + Send + Sync,

§

impl Enum for FocusPolicy
where FocusPolicy: Any + Send + Sync,

§

impl Enum for GridAutoFlow

§

impl Enum for GridTrackRepetition
where GridTrackRepetition: Any + Send + Sync, u16: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for Interaction
where Interaction: Any + Send + Sync,

§

impl Enum for JustifyContent

§

impl Enum for JustifyItems

§

impl Enum for JustifySelf
where JustifySelf: Any + Send + Sync,

§

impl Enum for OverflowAxis

§

impl Enum for PositionType

§

impl Enum for Val
where Val: Any + Send + Sync, f32: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for ZIndex
where ZIndex: Any + Send + Sync, i32: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for ApplicationLifetime

§

impl Enum for CompositeAlphaMode

§

impl Enum for CursorGrabMode

§

impl Enum for CursorIcon
where CursorIcon: Any + Send + Sync,

§

impl Enum for FileDragAndDrop
where FileDragAndDrop: Any + Send + Sync, Entity: FromReflect + TypePath + RegisterForReflection, PathBuf: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for Ime
where Ime: Any + Send + Sync, Entity: FromReflect + TypePath + RegisterForReflection, String: FromReflect + TypePath + RegisterForReflection, Option<(usize, usize)>: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for MonitorSelection
where MonitorSelection: Any + Send + Sync, usize: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for PresentMode
where PresentMode: Any + Send + Sync,

§

impl Enum for WindowLevel
where WindowLevel: Any + Send + Sync,

§

impl Enum for WindowMode
where WindowMode: Any + Send + Sync,

§

impl Enum for WindowPosition
where WindowPosition: Any + Send + Sync, MonitorSelection: FromReflect + TypePath + RegisterForReflection, IVec2: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for WindowRef
where WindowRef: Any + Send + Sync, Entity: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for WindowTheme
where WindowTheme: Any + Send + Sync,

§

impl Enum for WinitEvent
where WinitEvent: Any + Send + Sync, ApplicationLifetime: FromReflect + TypePath + RegisterForReflection, CursorEntered: FromReflect + TypePath + RegisterForReflection, CursorLeft: FromReflect + TypePath + RegisterForReflection, CursorMoved: FromReflect + TypePath + RegisterForReflection, FileDragAndDrop: FromReflect + TypePath + RegisterForReflection, Ime: FromReflect + TypePath + RegisterForReflection, ReceivedCharacter: FromReflect + TypePath + RegisterForReflection, RequestRedraw: FromReflect + TypePath + RegisterForReflection, WindowBackendScaleFactorChanged: FromReflect + TypePath + RegisterForReflection, WindowCloseRequested: FromReflect + TypePath + RegisterForReflection, WindowCreated: FromReflect + TypePath + RegisterForReflection, WindowDestroyed: FromReflect + TypePath + RegisterForReflection, WindowFocused: FromReflect + TypePath + RegisterForReflection, WindowMoved: FromReflect + TypePath + RegisterForReflection, WindowOccluded: FromReflect + TypePath + RegisterForReflection, WindowResized: FromReflect + TypePath + RegisterForReflection, WindowScaleFactorChanged: FromReflect + TypePath + RegisterForReflection, WindowThemeChanged: FromReflect + TypePath + RegisterForReflection, MouseButtonInput: FromReflect + TypePath + RegisterForReflection, MouseMotion: FromReflect + TypePath + RegisterForReflection, MouseWheel: FromReflect + TypePath + RegisterForReflection, TouchpadMagnify: FromReflect + TypePath + RegisterForReflection, TouchpadRotate: FromReflect + TypePath + RegisterForReflection, TouchInput: FromReflect + TypePath + RegisterForReflection, KeyboardInput: FromReflect + TypePath + RegisterForReflection,

§

impl Enum for DynamicEnum

§

impl<A> Enum for AssetId<A>
where A: Asset + TypePath, AssetId<A>: Any + Send + Sync, AssetIndex: FromReflect + TypePath + RegisterForReflection, Uuid: FromReflect + TypePath + RegisterForReflection,

§

impl<A> Enum for Handle<A>
where A: Asset + TypePath, Handle<A>: Any + Send + Sync, Arc<StrongHandle>: FromReflect + TypePath + RegisterForReflection, AssetId<A>: FromReflect + TypePath + RegisterForReflection,