pub trait PartialReflect:
DynamicTypePath
+ Send
+ Syncwhere
Self: 'static,{
Show 19 methods
// Required methods
fn get_represented_type_info(&self) -> Option<&'static TypeInfo>;
fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect>;
fn as_partial_reflect(&self) -> &dyn PartialReflect;
fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect;
fn try_into_reflect(
self: Box<Self>,
) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>;
fn try_as_reflect(&self) -> Option<&dyn Reflect>;
fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect>;
fn try_apply(
&mut self,
value: &dyn PartialReflect,
) -> Result<(), ApplyError>;
fn reflect_ref(&self) -> ReflectRef<'_>;
fn reflect_mut(&mut self) -> ReflectMut<'_>;
fn reflect_owned(self: Box<Self>) -> ReflectOwned;
fn clone_value(&self) -> Box<dyn PartialReflect>;
// Provided methods
fn apply(&mut self, value: &dyn PartialReflect) { ... }
fn reflect_kind(&self) -> ReflectKind { ... }
fn reflect_hash(&self) -> Option<u64> { ... }
fn reflect_partial_eq(&self, _value: &dyn PartialReflect) -> Option<bool> { ... }
fn debug(&self, f: &mut Formatter<'_>) -> Result { ... }
fn serializable(&self) -> Option<Serializable<'_>> { ... }
fn is_dynamic(&self) -> bool { ... }
}
Expand description
The foundational trait of bevy_reflect
, used for accessing and modifying data dynamically.
This is a supertrait of Reflect
,
meaning any type which implements Reflect
implements PartialReflect
by definition.
It’s recommended to use the derive macro for Reflect
rather than manually implementing this trait.
Doing so will automatically implement this trait as well as many other useful traits for reflection,
including one of the appropriate subtraits: Struct
, TupleStruct
or Enum
.
See the crate-level documentation to see how this trait and its subtraits can be used.
Required Methods§
sourcefn get_represented_type_info(&self) -> Option<&'static TypeInfo>
fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
Returns the TypeInfo
of the type represented by this value.
For most types, this will simply return their own TypeInfo
.
However, for dynamic types, such as DynamicStruct
or DynamicList
,
this will return the type they represent
(or None
if they don’t represent any particular type).
This method is great if you have an instance of a type or a dyn Reflect
,
and want to access its TypeInfo
. However, if this method is to be called
frequently, consider using TypeRegistry::get_type_info
as it can be more
performant for such use cases.
sourcefn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect>
fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect>
Casts this type to a boxed, reflected value.
This is useful for coercing trait objects.
sourcefn as_partial_reflect(&self) -> &dyn PartialReflect
fn as_partial_reflect(&self) -> &dyn PartialReflect
Casts this type to a reflected value.
This is useful for coercing trait objects.
sourcefn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect
fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect
Casts this type to a mutable, reflected value.
This is useful for coercing trait objects.
sourcefn try_into_reflect(
self: Box<Self>,
) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>
fn try_into_reflect( self: Box<Self>, ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>
Attempts to cast this type to a boxed, fully-reflected value.
sourcefn try_as_reflect(&self) -> Option<&dyn Reflect>
fn try_as_reflect(&self) -> Option<&dyn Reflect>
Attempts to cast this type to a fully-reflected value.
sourcefn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect>
fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect>
Attempts to cast this type to a mutable, fully-reflected value.
sourcefn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError>
fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError>
Tries to apply
a reflected value to this value.
Functions the same as the apply
function but returns an error instead of
panicking.
§Handling Errors
This function may leave self
in a partially mutated state if a error was encountered on the way.
consider maintaining a cloned instance of this data you can switch to if a error is encountered.
sourcefn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
Returns an immutable enumeration of “kinds” of type.
See ReflectRef
.
sourcefn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
Returns a mutable enumeration of “kinds” of type.
See ReflectMut
.
sourcefn reflect_owned(self: Box<Self>) -> ReflectOwned
fn reflect_owned(self: Box<Self>) -> ReflectOwned
Returns an owned enumeration of “kinds” of type.
See ReflectOwned
.
sourcefn clone_value(&self) -> Box<dyn PartialReflect>
fn clone_value(&self) -> Box<dyn PartialReflect>
Clones the value as a Reflect
trait object.
When deriving Reflect
for a struct, tuple struct or enum, the value is
cloned via Struct::clone_dynamic
, TupleStruct::clone_dynamic
,
or Enum::clone_dynamic
, respectively.
Implementors of other Reflect
subtraits (e.g. List
, Map
) should
use those subtraits’ respective clone_dynamic
methods.
Provided Methods§
sourcefn apply(&mut self, value: &dyn PartialReflect)
fn apply(&mut self, value: &dyn PartialReflect)
Applies a reflected value to this value.
If a type implements an introspection subtrait, then the semantics of this method are as follows:
- If
T
is aStruct
, then the value of each named field ofvalue
is applied to the corresponding named field ofself
. Fields which are not present in both structs are ignored. - If
T
is aTupleStruct
orTuple
, then the value of each numbered field is applied to the corresponding numbered field ofself.
Fields which are not present in both values are ignored. - If
T
is anEnum
, then the variant ofself
isupdated
to match the variant ofvalue
. The corresponding fields of that variant are applied fromvalue
ontoself
. Fields which are not present in both values are ignored. - If
T
is aList
orArray
, then each element ofvalue
is applied to the corresponding element ofself
. Up toself.len()
items are applied, and excess elements invalue
are appended toself
. - If
T
is aMap
, then for each key invalue
, the associated value is applied to the value associated with the same key inself
. Keys which are not present inself
are inserted. - If
T
is none of these, thenvalue
is downcast toT
, cloned, and assigned toself
.
Note that Reflect
must be implemented manually for List
s and
Map
s in order to achieve the correct semantics, as derived
implementations will have the semantics for Struct
, TupleStruct
, Enum
or none of the above depending on the kind of type. For lists and maps, use the
list_apply
and map_apply
helper functions when implementing this method.
§Panics
Derived implementations of this method will panic:
- If the type of
value
is not of the same kind asT
(e.g. ifT
is aList
, whilevalue
is aStruct
). - If
T
is any complex type and the corresponding fields or elements ofself
andvalue
are not of the same type. - If
T
is an opaque type andself
cannot be downcast toT
sourcefn reflect_kind(&self) -> ReflectKind
fn reflect_kind(&self) -> ReflectKind
Returns a zero-sized enumeration of “kinds” of type.
See ReflectKind
.
sourcefn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
Returns a hash of the value (which includes the type).
If the underlying type does not support hashing, returns None
.
sourcefn reflect_partial_eq(&self, _value: &dyn PartialReflect) -> Option<bool>
fn reflect_partial_eq(&self, _value: &dyn PartialReflect) -> Option<bool>
Returns a “partial equality” comparison result.
If the underlying type does not support equality testing, returns None
.
sourcefn serializable(&self) -> Option<Serializable<'_>>
fn serializable(&self) -> Option<Serializable<'_>>
Returns a serializable version of the value.
If the underlying type does not support serialization, returns None
.
sourcefn is_dynamic(&self) -> bool
fn is_dynamic(&self) -> bool
Indicates whether or not this type is a dynamic type.
Dynamic types include the ones built-in to this crate,
such as DynamicStruct
, DynamicList
, and DynamicTuple
.
However, they may be custom types used as proxies for other types
or to facilitate scripting capabilities.
By default, this method will return false
.
Implementations§
source§impl dyn PartialReflect
impl dyn PartialReflect
sourcepub fn represents<T: Reflect + TypePath>(&self) -> bool
pub fn represents<T: Reflect + TypePath>(&self) -> bool
Returns true
if the underlying value represents a value of type T
, or false
otherwise.
Read is
for more information on underlying values and represented types.
sourcepub fn try_downcast<T: Any>(
self: Box<dyn PartialReflect>,
) -> Result<Box<T>, Box<dyn PartialReflect>>
pub fn try_downcast<T: Any>( self: Box<dyn PartialReflect>, ) -> Result<Box<T>, Box<dyn PartialReflect>>
Downcasts the value to type T
, consuming the trait object.
If the underlying value does not implement Reflect
or is not of type T
, returns Err(self)
.
For remote types, T
should be the type itself rather than the wrapper type.
sourcepub fn try_take<T: Any>(
self: Box<dyn PartialReflect>,
) -> Result<T, Box<dyn PartialReflect>>
pub fn try_take<T: Any>( self: Box<dyn PartialReflect>, ) -> Result<T, Box<dyn PartialReflect>>
Downcasts the value to type T
, unboxing and consuming the trait object.
If the underlying value does not implement Reflect
or is not of type T
, returns Err(self)
.
For remote types, T
should be the type itself rather than the wrapper type.