pub struct ReflectFromPtr { /* private fields */ }Expand description
Reflect values are commonly used in situations where the actual types of values
are not known at runtime. In such situations you might have access to a *const () pointer
that you know implements Reflect, but have no way of turning it into a &dyn Reflect.
This is where ReflectFromPtr comes in, when creating a ReflectFromPtr for a given type T: Reflect.
Internally, this saves a concrete function *mut T -> *mut dyn Reflect which lets you create a trait object of Reflect
from a pointer.
§Example
use bevy_reflect::{TypeRegistry, Reflect, ReflectFromPtr};
use core::any::Any;
#[derive(Reflect)]
struct Reflected(String);
let mut type_registry = TypeRegistry::default();
type_registry.register::<Reflected>();
let mut value = Reflected("Hello world!".to_string());
let value: &dyn Any = &value;
let reflect_data = type_registry.get(core::any::TypeId::of::<Reflected>()).unwrap();
let reflect_from_ptr = reflect_data.data::<ReflectFromPtr>().unwrap();
let value = reflect_from_ptr.as_reflect(value).unwrap();
assert_eq!(value.downcast_ref::<Reflected>().unwrap().0, "Hello world!");Implementations§
Source§impl ReflectFromPtr
impl ReflectFromPtr
Sourcepub fn type_id(&self) -> TypeId
pub fn type_id(&self) -> TypeId
Returns the TypeId that the ReflectFromPtr was constructed for.
Sourcepub fn raw_pointer_cast(&self) -> fn(*mut ()) -> *mut dyn Reflect
pub fn raw_pointer_cast(&self) -> fn(*mut ()) -> *mut dyn Reflect
Returns the underlying function to cast from an arbitrary pointer to a [dyn Reflect]
pointer.
This function effectively just adds the vtable pointer for Reflect for the type this
ReflectFromPtr was constructed for, to whatever pointer you pass in. This function does
no validation or manipulation - it just does a pointer cast.
The returned function is technically safe. We simply manipulate some pointers. However,
using the result of this function requires the normal safety requirements. In particular,
you must ensure that the original pointer actually pointed to a value of the type that this
ReflectFromPtr was constructed for. This can be verified by checking that the type ID
returned by ReflectFromPtr::type_id matches the original pointer’s runtime type ID.
Note: It is not sufficient that you got this ReflectFromPtr from the type registry
for type ID A. It is possible for someone to overwrite the ReflectFromPtr type data with
that of a different type. Therefore, to ensure soundness, you must check ReflectFromPtr::type_id.
If you need to call this function for a *const (), you may simple use
cast_mut for the input and cast_const for the output.
Sourcepub fn as_reflect<'a>(
&self,
any: &'a (dyn Any + 'static),
) -> Option<&'a (dyn Reflect + 'static)>
pub fn as_reflect<'a>( &self, any: &'a (dyn Any + 'static), ) -> Option<&'a (dyn Reflect + 'static)>
Converts a [&dyn Any] into a [&dyn Reflect] if the type matches the type used to
construct this ReflectFromPtr.
Sourcepub fn as_reflect_mut<'a>(
&self,
any: &'a mut (dyn Any + 'static),
) -> Option<&'a mut (dyn Reflect + 'static)>
pub fn as_reflect_mut<'a>( &self, any: &'a mut (dyn Any + 'static), ) -> Option<&'a mut (dyn Reflect + 'static)>
Converts a [&mut dyn Any] into a [&mut dyn Reflect] if the type matches the type used to
construct this ReflectFromPtr.
Sourcepub fn box_as_reflect(
&self,
any: Box<dyn Any>,
) -> Result<Box<dyn Reflect>, Box<dyn Any>>
pub fn box_as_reflect( &self, any: Box<dyn Any>, ) -> Result<Box<dyn Reflect>, Box<dyn Any>>
Converts a Box<dyn Any> into a Box<dyn Reflect> if the type matches the type used to
construct this ReflectFromPtr.
If the type does not match Self::type_id, returns an Err holding any.
Sourcepub fn arc_as_reflect(
&self,
any: Arc<dyn Any>,
) -> Result<Arc<dyn Reflect>, Arc<dyn Any>>
pub fn arc_as_reflect( &self, any: Arc<dyn Any>, ) -> Result<Arc<dyn Reflect>, Arc<dyn Any>>
Converts a Arc<dyn Any> into a Arc<dyn Reflect> if the type matches the type used to
construct this ReflectFromPtr.
If the type does not match Self::type_id, returns an Err holding any.
Sourcepub unsafe fn ptr_as_reflect<'a>(
&self,
val: Ptr<'a>,
) -> &'a (dyn Reflect + 'static)
pub unsafe fn ptr_as_reflect<'a>( &self, val: Ptr<'a>, ) -> &'a (dyn Reflect + 'static)
Convert Ptr into &dyn Reflect.
§Safety
val must be a pointer to value of the type that the ReflectFromPtr was constructed for.
This can be verified by checking that the type id returned by ReflectFromPtr::type_id is the expected one.
Sourcepub unsafe fn ptr_as_reflect_mut<'a>(
&self,
val: PtrMut<'a>,
) -> &'a mut (dyn Reflect + 'static)
pub unsafe fn ptr_as_reflect_mut<'a>( &self, val: PtrMut<'a>, ) -> &'a mut (dyn Reflect + 'static)
Convert PtrMut into &mut dyn Reflect.
§Safety
val must be a pointer to a value of the type that the ReflectFromPtr was constructed for
This can be verified by checking that the type id returned by ReflectFromPtr::type_id is the expected one.
Trait Implementations§
Source§impl Clone for ReflectFromPtr
impl Clone for ReflectFromPtr
Source§fn clone(&self) -> ReflectFromPtr
fn clone(&self) -> ReflectFromPtr
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> CreateTypeData<T> for ReflectFromPtrwhere
T: Reflect,
impl<T> CreateTypeData<T> for ReflectFromPtrwhere
T: Reflect,
Source§fn create_type_data(_input: ()) -> ReflectFromPtr
fn create_type_data(_input: ()) -> ReflectFromPtr
Source§fn insert_dependencies(type_registration: &mut TypeRegistration)
fn insert_dependencies(type_registration: &mut TypeRegistration)
Auto Trait Implementations§
impl Freeze for ReflectFromPtr
impl RefUnwindSafe for ReflectFromPtr
impl Send for ReflectFromPtr
impl Sync for ReflectFromPtr
impl Unpin for ReflectFromPtr
impl UnsafeUnpin for ReflectFromPtr
impl UnwindSafe for ReflectFromPtr
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ConditionalSend for Twhere
T: Send,
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>, which can then be
downcast into Box<dyn 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>, which 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.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
Source§fn into_result(self) -> Result<T, RunSystemError>
fn into_result(self) -> Result<T, RunSystemError>
Source§impl<T> Template for T
impl<T> Template for T
Source§fn build_template(
&self,
_context: &mut TemplateContext<'_, '_>,
) -> Result<<T as Template>::Output, BevyError>
fn build_template( &self, _context: &mut TemplateContext<'_, '_>, ) -> Result<<T as Template>::Output, BevyError>
entity context to produce a Template::Output.Source§fn clone_template(&self) -> T
fn clone_template(&self) -> T
Clone.Source§impl<T> TypeData for T
impl<T> TypeData for T
Source§fn clone_type_data(&self) -> Box<dyn TypeData>
fn clone_type_data(&self) -> Box<dyn TypeData>
self.