Struct bevy_reflect::ReflectFromReflect
source · pub struct ReflectFromReflect { /* private fields */ }
Expand description
Type data that represents the FromReflect
trait and allows it to be used dynamically.
FromReflect
allows dynamic types (e.g. DynamicStruct
, DynamicEnum
, etc.) to be converted
to their full, concrete types. This is most important when it comes to deserialization where it isn’t
guaranteed that every field exists when trying to construct the final output.
However, to do this, you normally need to specify the exact concrete type:
#[derive(Reflect, PartialEq, Eq, Debug)]
struct Foo(#[reflect(default = "default_value")] usize);
fn default_value() -> usize { 123 }
let reflected = DynamicTupleStruct::default();
let concrete: Foo = <Foo as FromReflect>::from_reflect(&reflected).unwrap();
assert_eq!(Foo(123), concrete);
In a dynamic context where the type might not be known at compile-time, this is nearly impossible to do. That is why this type data struct exists— it allows us to construct the full type without knowing what the actual type is.
§Example
let mut reflected = DynamicTupleStruct::default();
reflected.set_represented_type(Some(<Foo as Typed>::type_info()));
let registration = registry.get_with_type_path(<Foo as TypePath>::type_path()).unwrap();
let rfr = registration.data::<ReflectFromReflect>().unwrap();
let concrete: Box<dyn Reflect> = rfr.from_reflect(&reflected).unwrap();
assert_eq!(Foo(123), concrete.take::<Foo>().unwrap());
Implementations§
source§impl ReflectFromReflect
impl ReflectFromReflect
sourcepub fn from_reflect(
&self,
reflect_value: &dyn Reflect
) -> Option<Box<dyn Reflect>>
pub fn from_reflect( &self, reflect_value: &dyn Reflect ) -> Option<Box<dyn Reflect>>
Perform a FromReflect::from_reflect
conversion on the given reflection object.
This will convert the object to a concrete type if it wasn’t already, and return
the value as Box<dyn Reflect>
.
Trait Implementations§
source§impl Clone for ReflectFromReflect
impl Clone for ReflectFromReflect
source§fn clone(&self) -> ReflectFromReflect
fn clone(&self) -> ReflectFromReflect
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T: FromReflect> FromType<T> for ReflectFromReflect
impl<T: FromReflect> FromType<T> for ReflectFromReflect
Auto Trait Implementations§
impl Freeze for ReflectFromReflect
impl RefUnwindSafe for ReflectFromReflect
impl Send for ReflectFromReflect
impl Sync for ReflectFromReflect
impl Unpin for ReflectFromReflect
impl UnwindSafe for ReflectFromReflect
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.