Trait bevy_reflect::FromReflect
source · pub trait FromReflect: Reflect + Sized {
// Required method
fn from_reflect(reflect: &dyn Reflect) -> Option<Self>;
// Provided method
fn take_from_reflect(
reflect: Box<dyn Reflect>
) -> Result<Self, Box<dyn Reflect>> { ... }
}
Expand description
A trait that enables types to be dynamically constructed from reflected data.
It’s recommended to use the derive macro rather than manually implementing this trait.
FromReflect
allows dynamic proxy types, like DynamicStruct
, to be used to generate
their concrete counterparts.
It can also be used to partially or fully clone a type (depending on whether it has
ignored fields or not).
In some cases, this trait may even be required.
Deriving Reflect
on an enum requires all its fields to implement FromReflect
.
Additionally, some complex types like Vec<T>
require that their element types
implement this trait.
The reason for such requirements is that some operations require new data to be constructed,
such as swapping to a new variant or pushing data to a homogeneous list.
See the crate-level documentation to see how this trait can be used.
Required Methods§
sourcefn from_reflect(reflect: &dyn Reflect) -> Option<Self>
fn from_reflect(reflect: &dyn Reflect) -> Option<Self>
Constructs a concrete instance of Self
from a reflected value.
Provided Methods§
sourcefn take_from_reflect(
reflect: Box<dyn Reflect>
) -> Result<Self, Box<dyn Reflect>>
fn take_from_reflect( reflect: Box<dyn Reflect> ) -> Result<Self, Box<dyn Reflect>>
Attempts to downcast the given value to Self
using,
constructing the value using from_reflect
if that fails.
This method is more efficient than using from_reflect
for cases where
the given value is likely a boxed instance of Self
(i.e. Box<Self>
)
rather than a boxed dynamic type (e.g. DynamicStruct
, DynamicList
, etc.).