|
| 1 | +use crate::{FromType, Reflect}; |
| 2 | + |
| 3 | +/// A trait for types which can be constructed from a reflected type. |
| 4 | +/// |
| 5 | +/// This trait can be derived on types which implement [`Reflect`]. Some complex |
| 6 | +/// types (such as `Vec<T>`) may only be reflected if their element types |
| 7 | +/// implement this trait. |
| 8 | +/// |
| 9 | +/// For structs and tuple structs, fields marked with the `#[reflect(ignore)]` |
| 10 | +/// attribute will be constructed using the `Default` implementation of the |
| 11 | +/// field type, rather than the corresponding field value (if any) of the |
| 12 | +/// reflected value. |
| 13 | +pub trait FromReflect: Reflect + Sized { |
| 14 | + /// Constructs a concrete instance of `Self` from a reflected value. |
| 15 | + fn from_reflect(reflect: &dyn Reflect) -> Option<Self>; |
| 16 | +} |
| 17 | + |
| 18 | +/// Type data that represents the [`FromReflect`] trait and allows it to be used dynamically. |
| 19 | +/// |
| 20 | +/// `FromReflect` allows dynamic types (e.g. [`DynamicStruct`], [`DynamicEnum`], etc.) to be converted |
| 21 | +/// to their full, concrete types. This is most important when it comes to deserialization where it isn't |
| 22 | +/// guaranteed that every field exists when trying to construct the final output. |
| 23 | +/// |
| 24 | +/// However, to do this, you normally need to specify the exact concrete type: |
| 25 | +/// |
| 26 | +/// ``` |
| 27 | +/// # use bevy_reflect::{DynamicTupleStruct, FromReflect, Reflect}; |
| 28 | +/// #[derive(Reflect, FromReflect, PartialEq, Eq, Debug)] |
| 29 | +/// #[reflect(FromReflect)] |
| 30 | +/// struct Foo(#[reflect(default = "default_value")] usize); |
| 31 | +/// |
| 32 | +/// fn default_value() -> usize { 123 } |
| 33 | +/// |
| 34 | +/// let reflected = DynamicTupleStruct::default(); |
| 35 | +/// |
| 36 | +/// let concrete: Foo = <Foo as FromReflect>::from_reflect(&reflected).unwrap(); |
| 37 | +/// |
| 38 | +/// assert_eq!(Foo(123), concrete); |
| 39 | +/// ``` |
| 40 | +/// |
| 41 | +/// In a dynamic context where the type might not be known at compile-time, this is nearly impossible to do. |
| 42 | +/// That is why this type data struct exists— it allows us to construct the full type without knowing |
| 43 | +/// what the actual type is. |
| 44 | +/// |
| 45 | +/// # Example |
| 46 | +/// |
| 47 | +/// ``` |
| 48 | +/// # use bevy_reflect::{DynamicTupleStruct, FromReflect, Reflect, ReflectFromReflect, TypeRegistry}; |
| 49 | +/// # #[derive(Reflect, FromReflect, PartialEq, Eq, Debug)] |
| 50 | +/// # #[reflect(FromReflect)] |
| 51 | +/// # struct Foo(#[reflect(default = "default_value")] usize); |
| 52 | +/// # fn default_value() -> usize { 123 } |
| 53 | +/// # let mut registry = TypeRegistry::new(); |
| 54 | +/// # registry.register::<Foo>(); |
| 55 | +/// |
| 56 | +/// let mut reflected = DynamicTupleStruct::default(); |
| 57 | +/// reflected.set_name(std::any::type_name::<Foo>().to_string()); |
| 58 | +/// |
| 59 | +/// let registration = registry.get_with_name(reflected.type_name()).unwrap(); |
| 60 | +/// let rfr = registration.data::<ReflectFromReflect>().unwrap(); |
| 61 | +/// |
| 62 | +/// let concrete: Box<dyn Reflect> = rfr.from_reflect(&reflected).unwrap(); |
| 63 | +/// |
| 64 | +/// assert_eq!(Foo(123), concrete.take::<Foo>().unwrap()); |
| 65 | +/// ``` |
| 66 | +/// |
| 67 | +/// [`DynamicStruct`]: crate::DynamicStruct |
| 68 | +/// [`DynamicEnum`]: crate::DynamicEnum |
| 69 | +#[derive(Clone)] |
| 70 | +pub struct ReflectFromReflect { |
| 71 | + from_reflect: fn(&dyn Reflect) -> Option<Box<dyn Reflect>>, |
| 72 | +} |
| 73 | + |
| 74 | +impl ReflectFromReflect { |
| 75 | + /// Perform a [`FromReflect::from_reflect`] conversion on the given reflection object. |
| 76 | + /// |
| 77 | + /// This will convert the object to a concrete type if it wasn't already, and return |
| 78 | + /// the value as `Box<dyn Reflect>`. |
| 79 | + #[allow(clippy::wrong_self_convention)] |
| 80 | + pub fn from_reflect(&self, reflect_value: &dyn Reflect) -> Option<Box<dyn Reflect>> { |
| 81 | + (self.from_reflect)(reflect_value) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl<T: FromReflect + Reflect> FromType<T> for ReflectFromReflect { |
| 86 | + fn from_type() -> Self { |
| 87 | + Self { |
| 88 | + from_reflect: |reflect_value| { |
| 89 | + T::from_reflect(reflect_value).map(|value| Box::new(value) as Box<dyn Reflect>) |
| 90 | + }, |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments