Enum

Trait Enum 

Source
pub trait Enum: PartialReflect {
Show 15 methods // Required methods fn field(&self, name: &str) -> Option<&(dyn PartialReflect + 'static)>; fn field_at(&self, index: usize) -> Option<&(dyn PartialReflect + 'static)>; fn field_mut( &mut self, name: &str, ) -> Option<&mut (dyn PartialReflect + 'static)>; fn field_at_mut( &mut self, index: usize, ) -> Option<&mut (dyn PartialReflect + '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; // Provided methods fn to_dynamic_enum(&self) -> DynamicEnum { ... } fn is_variant(&self, variant_type: VariantType) -> bool { ... } fn variant_path(&self) -> String { ... } fn get_represented_enum_info(&self) -> Option<&'static EnumInfo> { ... }
}
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§

Source

fn field(&self, name: &str) -> Option<&(dyn PartialReflect + '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.

Source

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

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

Source

fn field_mut( &mut self, name: &str, ) -> Option<&mut (dyn PartialReflect + '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.

Source

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

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

Source

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.

Source

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.

Source

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

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

Source

fn field_len(&self) -> usize

Returns the number of fields in the current variant.

Source

fn variant_name(&self) -> &str

The name of the current variant.

Source

fn variant_index(&self) -> usize

The index of the current variant.

Source

fn variant_type(&self) -> VariantType

The type of the current variant.

Provided Methods§

Source

fn to_dynamic_enum(&self) -> DynamicEnum

Creates a new DynamicEnum from this enum.

Source

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

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

Source

fn variant_path(&self) -> String

Returns the full path to the current variant.

Source

fn get_represented_enum_info(&self) -> Option<&'static EnumInfo>

Will return None if TypeInfo is not available.

Implementations on Foreign Types§

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

fn field_len(&self) -> usize

Source§

fn variant_name(&self) -> &str

Source§

fn variant_index(&self) -> usize

Source§

fn variant_type(&self) -> VariantType

Source§

fn to_dynamic_enum(&self) -> DynamicEnum

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

fn field_len(&self) -> usize

Source§

fn variant_name(&self) -> &str

Source§

fn variant_index(&self) -> usize

Source§

fn variant_type(&self) -> VariantType

Source§

fn to_dynamic_enum(&self) -> DynamicEnum

Implementors§

Source§

impl Enum for AccessibilitySystems

Source§

impl Enum for UntypedAssetId

Source§

impl Enum for Camera3dDepthLoadOp

Source§

impl Enum for NormalizedRenderTarget

Source§

impl Enum for RenderTarget

Source§

impl Enum for bevy::camera::ScalingMode

Source§

impl Enum for ScreenSpaceTransmissionQuality

Source§

impl Enum for CubemapLayout

Source§

impl Enum for DebandDither

Source§

impl Enum for Tonemapping

Source§

impl Enum for ButtonState

Source§

impl Enum for GamepadConnection

Source§

impl Enum for GamepadEvent

Source§

impl Enum for GamepadInput

Source§

impl Enum for GamepadRumbleRequest

Source§

impl Enum for RawGamepadEvent

Source§

impl Enum for Key

Source§

impl Enum for NativeKey

Source§

impl Enum for NativeKeyCode

Source§

impl Enum for MouseScrollUnit

Source§

impl Enum for ForceTouch

Source§

impl Enum for TouchPhase

Source§

impl Enum for ClusterConfig

Source§

impl Enum for ClusterFarZMode

Source§

impl Enum for ShadowFilteringMethod

Source§

impl Enum for CompassOctant

Source§

impl Enum for CompassQuadrant

Source§

impl Enum for CapsuleUvProfile

Source§

impl Enum for CircularMeshUvMode

Source§

impl Enum for ConeAnchor

Source§

impl Enum for CylinderAnchor

Source§

impl Enum for Indices

Source§

impl Enum for SphereKind

Source§

impl Enum for AtmosphereMode

Source§

impl Enum for OpaqueRendererMethod

Source§

impl Enum for ScreenSpaceAmbientOcclusionQualityLevel

Source§

impl Enum for UvChannel

Source§

impl Enum for PickingInteraction

Source§

impl Enum for PointerAction

Source§

impl Enum for PointerId

Source§

impl Enum for PressDirection

Source§

impl Enum for BloomCompositeMode

Source§

impl Enum for DepthOfFieldMode

Source§

impl Enum for AlignContent

Source§

impl Enum for AlignItems

Source§

impl Enum for AlignSelf

Source§

impl Enum for AlphaMode

Source§

impl Enum for BoxSizing

Source§

impl Enum for ClearColorConfig

Source§

impl Enum for Color

Source§

impl Enum for Display

Source§

impl Enum for EaseFunction

Source§

impl Enum for EulerRot

Source§

impl Enum for FileDragAndDrop

Source§

impl Enum for FlexDirection

Source§

impl Enum for FlexWrap

Source§

impl Enum for FogFalloff

Source§

impl Enum for GamepadAxis

Source§

impl Enum for GamepadButton

Source§

impl Enum for Gradient

Source§

impl Enum for GridAutoFlow

Source§

impl Enum for GridTrackRepetition

Source§

impl Enum for Ime

Source§

impl Enum for Interaction

Source§

impl Enum for InterpolationColorSpace

Source§

impl Enum for JumpAt

Source§

impl Enum for Justify

Source§

impl Enum for JustifyContent

Source§

impl Enum for JustifyItems

Source§

impl Enum for JustifySelf

Source§

impl Enum for KeyCode

Source§

impl Enum for LineBreak

Source§

impl Enum for MaxTrackSizingFunction

Source§

impl Enum for MinTrackSizingFunction

Source§

impl Enum for MonitorSelection

Source§

impl Enum for MouseButton

Source§

impl Enum for Msaa

Source§

impl Enum for NodeImageMode

Source§

impl Enum for OverflowAxis

Source§

impl Enum for OverflowClipBox

Source§

impl Enum for ParallaxMappingMethod

Source§

impl Enum for PointerButton

Source§

impl Enum for PositionType

Source§

impl Enum for Projection

Source§

impl Enum for RadialGradientShape

Source§

impl Enum for bevy::prelude::ScalingMode

Source§

impl Enum for SliceScaleMode

Source§

impl Enum for SpriteImageMode

Source§

impl Enum for TimerMode

Source§

impl Enum for UntypedHandle

Source§

impl Enum for Val

Source§

impl Enum for VideoModeSelection

Source§

impl Enum for Visibility

Source§

impl Enum for WindowPosition

Source§

impl Enum for FontSmoothing

Source§

impl Enum for LineHeight

Source§

impl Enum for FocusPolicy

Source§

impl Enum for AppLifecycle

Source§

impl Enum for CompositeAlphaMode

Source§

impl Enum for CursorGrabMode

Source§

impl Enum for CursorIcon

Source§

impl Enum for CustomCursor

Source§

impl Enum for PresentMode

Source§

impl Enum for ScreenEdge

Source§

impl Enum for SystemCursorIcon

Source§

impl Enum for WindowEvent

Source§

impl Enum for WindowLevel

Source§

impl Enum for WindowMode

Source§

impl Enum for WindowRef

Source§

impl Enum for WindowTheme

Source§

impl Enum for DynamicEnum

Source§

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

Source§

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

Source§

impl<A> Enum for Handle<A>
where A: Asset + TypePath, Handle<A>: Any + Send + Sync,

Source§

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