bevy_reflect/enums/
enum_trait.rs

1use crate::generics::impl_generic_info_methods;
2use crate::{
3    attributes::{impl_custom_attribute_methods, CustomAttributes},
4    type_info::impl_type_methods,
5    DynamicEnum, Generics, PartialReflect, Type, TypePath, VariantInfo, VariantType,
6};
7use alloc::{boxed::Box, format, string::String};
8use bevy_platform::collections::HashMap;
9use bevy_platform::sync::Arc;
10use core::slice::Iter;
11
12/// A trait used to power [enum-like] operations via [reflection].
13///
14/// This allows enums to be processed and modified dynamically at runtime without
15/// necessarily knowing the actual type.
16/// Enums are much more complex than their struct counterparts.
17/// As a result, users will need to be mindful of conventions, considerations,
18/// and complications when working with this trait.
19///
20/// # Variants
21///
22/// An enum is a set of choices called _variants_.
23/// An instance of an enum can only exist as one of these choices at any given time.
24/// Consider Rust's [`Option<T>`]. It's an enum with two variants: [`None`] and [`Some`].
25/// If you're `None`, you can't be `Some` and vice versa.
26///
27/// > ⚠️ __This is very important:__
28/// > The [`Enum`] trait represents an enum _as one of its variants_.
29/// > It does not represent the entire enum since that's not true to how enums work.
30///
31/// Variants come in a few [flavors](VariantType):
32///
33/// | Variant Type | Syntax                         |
34/// | ------------ | ------------------------------ |
35/// | Unit         | `MyEnum::Foo`                  |
36/// | Tuple        | `MyEnum::Foo( i32, i32 )`      |
37/// | Struct       | `MyEnum::Foo{ value: String }` |
38///
39/// As you can see, a unit variant contains no fields, while tuple and struct variants
40/// can contain one or more fields.
41/// The fields in a tuple variant is defined by their _order_ within the variant.
42/// Index `0` represents the first field in the variant and so on.
43/// Fields in struct variants (excluding tuple structs), on the other hand, are
44/// represented by a _name_.
45///
46/// # Implementation
47///
48/// > 💡 This trait can be automatically implemented using [`#[derive(Reflect)]`](derive@crate::Reflect)
49/// > on an enum definition.
50///
51/// Despite the fact that enums can represent multiple states, traits only exist in one state
52/// and must be applied to the entire enum rather than a particular variant.
53/// Because of this limitation, the [`Enum`] trait must not only _represent_ any of the
54/// three variant types, but also define the _methods_ for all three as well.
55///
56/// What does this mean? It means that even though a unit variant contains no fields, a
57/// representation of that variant using the [`Enum`] trait will still contain methods for
58/// accessing fields!
59/// Again, this is to account for _all three_ variant types.
60///
61/// We recommend using the built-in [`#[derive(Reflect)]`](derive@crate::Reflect) macro to automatically handle all the
62/// implementation details for you.
63/// However, if you _must_ implement this trait manually, there are a few things to keep in mind...
64///
65/// ## Field Order
66///
67/// While tuple variants identify their fields by the order in which they are defined, struct
68/// variants identify fields by their name.
69/// However, both should allow access to fields by their defined order.
70///
71/// The reason all fields, regardless of variant type, need to be accessible by their order is
72/// due to field iteration.
73/// We need a way to iterate through each field in a variant, and the easiest way of achieving
74/// that is through the use of field order.
75///
76/// The derive macro adds proper struct variant handling for [`Enum::index_of`], [`Enum::name_at`]
77/// and [`Enum::field_at[_mut]`](Enum::field_at) methods.
78/// The first two methods are __required__ for all struct variant types.
79/// By convention, implementors should also handle the last method as well, but this is not
80/// a strict requirement.
81///
82/// ## Field Names
83///
84/// Implementors may choose to handle [`Enum::index_of`], [`Enum::name_at`], and
85/// [`Enum::field[_mut]`](Enum::field) for tuple variants by considering stringified `usize`s to be
86/// valid names (such as `"3"`).
87/// This isn't wrong to do, but the convention set by the derive macro is that it isn't supported.
88/// It's preferred that these strings be converted to their proper `usize` representations and
89/// the [`Enum::field_at[_mut]`](Enum::field_at) methods be used instead.
90///
91/// [enum-like]: https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html
92/// [reflection]: crate
93/// [`None`]: Option<T>::None
94/// [`Some`]: Option<T>::Some
95/// [`Reflect`]: bevy_reflect_derive::Reflect
96pub trait Enum: PartialReflect {
97    /// Returns a reference to the value of the field (in the current variant) with the given name.
98    ///
99    /// For non-[`VariantType::Struct`] variants, this should return `None`.
100    fn field(&self, name: &str) -> Option<&dyn PartialReflect>;
101    /// Returns a reference to the value of the field (in the current variant) at the given index.
102    fn field_at(&self, index: usize) -> Option<&dyn PartialReflect>;
103    /// Returns a mutable reference to the value of the field (in the current variant) with the given name.
104    ///
105    /// For non-[`VariantType::Struct`] variants, this should return `None`.
106    fn field_mut(&mut self, name: &str) -> Option<&mut dyn PartialReflect>;
107    /// Returns a mutable reference to the value of the field (in the current variant) at the given index.
108    fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect>;
109    /// Returns the index of the field (in the current variant) with the given name.
110    ///
111    /// For non-[`VariantType::Struct`] variants, this should return `None`.
112    fn index_of(&self, name: &str) -> Option<usize>;
113    /// Returns the name of the field (in the current variant) with the given index.
114    ///
115    /// For non-[`VariantType::Struct`] variants, this should return `None`.
116    fn name_at(&self, index: usize) -> Option<&str>;
117    /// Returns an iterator over the values of the current variant's fields.
118    fn iter_fields(&self) -> VariantFieldIter;
119    /// Returns the number of fields in the current variant.
120    fn field_len(&self) -> usize;
121    /// The name of the current variant.
122    fn variant_name(&self) -> &str;
123    /// The index of the current variant.
124    fn variant_index(&self) -> usize;
125    /// The type of the current variant.
126    fn variant_type(&self) -> VariantType;
127    // Clones the enum into a [`DynamicEnum`].
128    #[deprecated(since = "0.16.0", note = "use `to_dynamic_enum` instead")]
129    fn clone_dynamic(&self) -> DynamicEnum {
130        self.to_dynamic_enum()
131    }
132    /// Creates a new [`DynamicEnum`] from this enum.
133    fn to_dynamic_enum(&self) -> DynamicEnum {
134        DynamicEnum::from_ref(self)
135    }
136    /// Returns true if the current variant's type matches the given one.
137    fn is_variant(&self, variant_type: VariantType) -> bool {
138        self.variant_type() == variant_type
139    }
140    /// Returns the full path to the current variant.
141    fn variant_path(&self) -> String {
142        format!("{}::{}", self.reflect_type_path(), self.variant_name())
143    }
144
145    /// Will return `None` if [`TypeInfo`] is not available.
146    ///
147    /// [`TypeInfo`]: crate::TypeInfo
148    fn get_represented_enum_info(&self) -> Option<&'static EnumInfo> {
149        self.get_represented_type_info()?.as_enum().ok()
150    }
151}
152
153/// A container for compile-time enum info, used by [`TypeInfo`](crate::TypeInfo).
154#[derive(Clone, Debug)]
155pub struct EnumInfo {
156    ty: Type,
157    generics: Generics,
158    variants: Box<[VariantInfo]>,
159    variant_names: Box<[&'static str]>,
160    variant_indices: HashMap<&'static str, usize>,
161    custom_attributes: Arc<CustomAttributes>,
162    #[cfg(feature = "documentation")]
163    docs: Option<&'static str>,
164}
165
166impl EnumInfo {
167    /// Create a new [`EnumInfo`].
168    ///
169    /// # Arguments
170    ///
171    /// * `variants`: The variants of this enum in the order they are defined
172    pub fn new<TEnum: Enum + TypePath>(variants: &[VariantInfo]) -> Self {
173        let variant_indices = variants
174            .iter()
175            .enumerate()
176            .map(|(index, variant)| (variant.name(), index))
177            .collect::<HashMap<_, _>>();
178
179        let variant_names = variants.iter().map(VariantInfo::name).collect();
180
181        Self {
182            ty: Type::of::<TEnum>(),
183            generics: Generics::new(),
184            variants: variants.to_vec().into_boxed_slice(),
185            variant_names,
186            variant_indices,
187            custom_attributes: Arc::new(CustomAttributes::default()),
188            #[cfg(feature = "documentation")]
189            docs: None,
190        }
191    }
192
193    /// Sets the docstring for this enum.
194    #[cfg(feature = "documentation")]
195    pub fn with_docs(self, docs: Option<&'static str>) -> Self {
196        Self { docs, ..self }
197    }
198
199    /// Sets the custom attributes for this enum.
200    pub fn with_custom_attributes(self, custom_attributes: CustomAttributes) -> Self {
201        Self {
202            custom_attributes: Arc::new(custom_attributes),
203            ..self
204        }
205    }
206
207    /// A slice containing the names of all variants in order.
208    pub fn variant_names(&self) -> &[&'static str] {
209        &self.variant_names
210    }
211
212    /// Get a variant with the given name.
213    pub fn variant(&self, name: &str) -> Option<&VariantInfo> {
214        self.variant_indices
215            .get(name)
216            .map(|index| &self.variants[*index])
217    }
218
219    /// Get a variant at the given index.
220    pub fn variant_at(&self, index: usize) -> Option<&VariantInfo> {
221        self.variants.get(index)
222    }
223
224    /// Get the index of the variant with the given name.
225    pub fn index_of(&self, name: &str) -> Option<usize> {
226        self.variant_indices.get(name).copied()
227    }
228
229    /// Returns the full path to the given variant.
230    ///
231    /// This does _not_ check if the given variant exists.
232    pub fn variant_path(&self, name: &str) -> String {
233        format!("{}::{name}", self.type_path())
234    }
235
236    /// Checks if a variant with the given name exists within this enum.
237    pub fn contains_variant(&self, name: &str) -> bool {
238        self.variant_indices.contains_key(name)
239    }
240
241    /// Iterate over the variants of this enum.
242    pub fn iter(&self) -> Iter<'_, VariantInfo> {
243        self.variants.iter()
244    }
245
246    /// The number of variants in this enum.
247    pub fn variant_len(&self) -> usize {
248        self.variants.len()
249    }
250
251    impl_type_methods!(ty);
252
253    /// The docstring of this enum, if any.
254    #[cfg(feature = "documentation")]
255    pub fn docs(&self) -> Option<&'static str> {
256        self.docs
257    }
258
259    impl_custom_attribute_methods!(self.custom_attributes, "enum");
260
261    impl_generic_info_methods!(generics);
262}
263
264/// An iterator over the fields in the current enum variant.
265pub struct VariantFieldIter<'a> {
266    container: &'a dyn Enum,
267    index: usize,
268}
269
270impl<'a> VariantFieldIter<'a> {
271    pub fn new(container: &'a dyn Enum) -> Self {
272        Self {
273            container,
274            index: 0,
275        }
276    }
277}
278
279impl<'a> Iterator for VariantFieldIter<'a> {
280    type Item = VariantField<'a>;
281
282    fn next(&mut self) -> Option<Self::Item> {
283        let value = match self.container.variant_type() {
284            VariantType::Unit => None,
285            VariantType::Tuple => Some(VariantField::Tuple(self.container.field_at(self.index)?)),
286            VariantType::Struct => {
287                let name = self.container.name_at(self.index)?;
288                Some(VariantField::Struct(name, self.container.field(name)?))
289            }
290        };
291        self.index += value.is_some() as usize;
292        value
293    }
294
295    fn size_hint(&self) -> (usize, Option<usize>) {
296        let size = self.container.field_len();
297        (size, Some(size))
298    }
299}
300
301impl<'a> ExactSizeIterator for VariantFieldIter<'a> {}
302
303pub enum VariantField<'a> {
304    Struct(&'a str, &'a dyn PartialReflect),
305    Tuple(&'a dyn PartialReflect),
306}
307
308impl<'a> VariantField<'a> {
309    pub fn name(&self) -> Option<&'a str> {
310        if let Self::Struct(name, ..) = self {
311            Some(*name)
312        } else {
313            None
314        }
315    }
316
317    pub fn value(&self) -> &'a dyn PartialReflect {
318        match *self {
319            Self::Struct(_, value) | Self::Tuple(value) => value,
320        }
321    }
322}
323
324// Tests that need access to internal fields have to go here rather than in mod.rs
325#[cfg(test)]
326mod tests {
327    use crate::*;
328
329    #[derive(Reflect, Debug, PartialEq)]
330    enum MyEnum {
331        A,
332        B(usize, i32),
333        C { foo: f32, bar: bool },
334    }
335    #[test]
336    fn next_index_increment() {
337        // unit enums always return none, so index should stay at 0
338        let unit_enum = MyEnum::A;
339        let mut iter = unit_enum.iter_fields();
340        let size = iter.len();
341        for _ in 0..2 {
342            assert!(iter.next().is_none());
343            assert_eq!(size, iter.index);
344        }
345        // tuple enums we iter over each value (unnamed fields), stop after that
346        let tuple_enum = MyEnum::B(0, 1);
347        let mut iter = tuple_enum.iter_fields();
348        let size = iter.len();
349        for _ in 0..2 {
350            let prev_index = iter.index;
351            assert!(iter.next().is_some());
352            assert_eq!(prev_index, iter.index - 1);
353        }
354        for _ in 0..2 {
355            assert!(iter.next().is_none());
356            assert_eq!(size, iter.index);
357        }
358
359        // struct enums, we iterate over each field in the struct
360        let struct_enum = MyEnum::C {
361            foo: 0.,
362            bar: false,
363        };
364        let mut iter = struct_enum.iter_fields();
365        let size = iter.len();
366        for _ in 0..2 {
367            let prev_index = iter.index;
368            assert!(iter.next().is_some());
369            assert_eq!(prev_index, iter.index - 1);
370        }
371        for _ in 0..2 {
372            assert!(iter.next().is_none());
373            assert_eq!(size, iter.index);
374        }
375    }
376}