pub trait PartialReflect:
DynamicTypePath
+ Send
+ Sync
+ 'static {
Show 20 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 + 'static);
fn as_partial_reflect_mut(&mut self) -> &mut (dyn PartialReflect + 'static);
fn try_into_reflect(
self: Box<Self>,
) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>;
fn try_as_reflect(&self) -> Option<&(dyn Reflect + 'static)>;
fn try_as_reflect_mut(&mut self) -> Option<&mut (dyn Reflect + 'static)>;
fn try_apply(
&mut self,
value: &(dyn PartialReflect + 'static),
) -> Result<(), ApplyError>;
fn reflect_ref(&self) -> ReflectRef<'_>;
fn reflect_mut(&mut self) -> ReflectMut<'_>;
fn reflect_owned(self: Box<Self>) -> ReflectOwned;
// Provided methods
fn apply(&mut self, value: &(dyn PartialReflect + 'static)) { ... }
fn reflect_kind(&self) -> ReflectKind { ... }
fn to_dynamic(&self) -> Box<dyn PartialReflect> { ... }
fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> { ... }
fn reflect_clone_and_take<T>(&self) -> Result<T, ReflectCloneError>
where T: 'static,
Self: Sized + TypePath { ... }
fn reflect_hash(&self) -> Option<u64> { ... }
fn reflect_partial_eq(
&self,
_value: &(dyn PartialReflect + 'static),
) -> Option<bool> { ... }
fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error> { ... }
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 + 'static)
fn as_partial_reflect(&self) -> &(dyn PartialReflect + 'static)
Casts this type to a reflected value.
This is useful for coercing trait objects.
Sourcefn as_partial_reflect_mut(&mut self) -> &mut (dyn PartialReflect + 'static)
fn as_partial_reflect_mut(&mut self) -> &mut (dyn PartialReflect + 'static)
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 + 'static)>
fn try_as_reflect(&self) -> Option<&(dyn Reflect + 'static)>
Attempts to cast this type to a fully-reflected value.
Sourcefn try_as_reflect_mut(&mut self) -> Option<&mut (dyn Reflect + 'static)>
fn try_as_reflect_mut(&mut self) -> Option<&mut (dyn Reflect + 'static)>
Attempts to cast this type to a mutable, fully-reflected value.
Sourcefn try_apply(
&mut self,
value: &(dyn PartialReflect + 'static),
) -> Result<(), ApplyError>
fn try_apply( &mut self, value: &(dyn PartialReflect + 'static), ) -> 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.
Provided Methods§
Sourcefn apply(&mut self, value: &(dyn PartialReflect + 'static))
fn apply(&mut self, value: &(dyn PartialReflect + 'static))
Applies a reflected value to this value.
If Self implements a reflection subtrait, then the semantics of this
method are as follows:
- If
Selfis aStruct, then the value of each named field ofvalueis applied to the corresponding named field ofself. Fields which are not present in both structs are ignored. - If
Selfis aTupleStructorTuple, 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
Selfis anEnum, then the variant ofselfisupdatedto match the variant ofvalue. The corresponding fields of that variant are applied fromvalueontoself. Fields which are not present in both values are ignored. - If
Selfis aListorArray, then each element ofvalueis applied to the corresponding element ofself. Up toself.len()items are applied, and excess elements invalueare appended toself. - If
Selfis 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 inselfare inserted, and keys fromselfwhich are not present invalueare removed. - If
Selfis aSet, then each element ofvalueis applied to the corresponding element ofSelf. If an element ofvaluedoes not exist inSelfthen it is cloned and inserted. If an element fromselfis not present invaluethen it is removed. - If
Selfis none of these, thenvalueis downcast toSelf, cloned, and assigned toself.
Note that Reflect must be implemented manually for Lists,
Maps, and Sets 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, maps, and sets, use the
list_apply, map_apply, and set_apply helper functions when implementing this method.
§Panics
Derived implementations of this method will panic:
- If the type of
valueis not of the same kind asSelf(e.g. ifSelfis aList, whilevalueis aStruct). - If
Selfis any complex type and the corresponding fields or elements ofselfandvalueare not of the same type. - If
Selfis an opaque type andvaluecannot be downcast toSelf
Sourcefn reflect_kind(&self) -> ReflectKind
fn reflect_kind(&self) -> ReflectKind
Returns a zero-sized enumeration of “kinds” of type.
See ReflectKind.
Sourcefn to_dynamic(&self) -> Box<dyn PartialReflect>
fn to_dynamic(&self) -> Box<dyn PartialReflect>
Converts this reflected value into its dynamic representation based on its kind.
For example, a List type will internally invoke List::to_dynamic_list, returning DynamicList.
A Struct type will invoke Struct::to_dynamic_struct, returning DynamicStruct.
And so on.
If the kind is opaque, then the value will attempt to be cloned directly via reflect_clone,
since opaque types do not have any standard dynamic representation.
To attempt to clone the value directly such that it returns a concrete instance of this type,
use reflect_clone.
§Panics
This method will panic if the kind is opaque and the call to reflect_clone fails.
§Example
let value = (1, true, 3.14);
let dynamic_value = value.to_dynamic();
assert!(dynamic_value.is_dynamic())Sourcefn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError>
fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError>
Attempts to clone Self using reflection.
Unlike to_dynamic, which generally returns a dynamic representation of Self,
this method attempts create a clone of Self directly, if possible.
If the clone cannot be performed, an appropriate ReflectCloneError is returned.
§Example
let value = (1, true, 3.14);
let cloned = value.reflect_clone().unwrap();
assert!(cloned.is::<(i32, bool, f64)>())Sourcefn reflect_clone_and_take<T>(&self) -> Result<T, ReflectCloneError>
fn reflect_clone_and_take<T>(&self) -> Result<T, ReflectCloneError>
For a type implementing PartialReflect, combines reflect_clone and
take in a useful fashion, automatically constructing an appropriate
ReflectCloneError if the downcast fails.
This is an associated function, rather than a method, because methods with generic types prevent dyn-compatibility.
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 + 'static),
) -> Option<bool>
fn reflect_partial_eq( &self, _value: &(dyn PartialReflect + 'static), ) -> Option<bool>
Returns a “partial equality” comparison result.
If the underlying type does not support equality testing, 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>(&self) -> bool
pub fn represents<T>(&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>(
self: Box<dyn PartialReflect>,
) -> Result<Box<T>, Box<dyn PartialReflect>>where
T: Any,
pub fn try_downcast<T>(
self: Box<dyn PartialReflect>,
) -> Result<Box<T>, Box<dyn PartialReflect>>where
T: Any,
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>(
self: Box<dyn PartialReflect>,
) -> Result<T, Box<dyn PartialReflect>>where
T: Any,
pub fn try_take<T>(
self: Box<dyn PartialReflect>,
) -> Result<T, Box<dyn PartialReflect>>where
T: Any,
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.