Skip to main content

ReflectFromPtr

Struct ReflectFromPtr 

Source
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

Source

pub fn type_id(&self) -> TypeId

Returns the TypeId that the ReflectFromPtr was constructed for.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Source§

fn clone(&self) -> ReflectFromPtr

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> CreateTypeData<T> for ReflectFromPtr
where T: Reflect,

Source§

fn create_type_data(_input: ()) -> ReflectFromPtr

Create this type data using the given input.
Source§

fn insert_dependencies(type_registration: &mut TypeRegistration)

Inserts TypeData dependencies of this TypeData. This is especially useful for trait TypeData that has a supertrait (ex: A: B). When the TypeData for A is inserted, the B TypeData will also be inserted.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts 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>

Converts 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)

Converts &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)

Converts &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
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoResult<T> for T

Source§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
Source§

impl<A> Is for A
where A: Any,

Source§

fn is<T>() -> bool
where T: Any,

Checks if the current type “is” another type, using a TypeId equality comparison. This is most useful in the context of generic logic. Read more
Source§

impl<T> Template for T
where T: Clone + Unpin,

Source§

type Output = T

The type of value produced by this Template.
Source§

fn build_template( &self, _context: &mut TemplateContext<'_, '_>, ) -> Result<<T as Template>::Output, BevyError>

Uses this template and the given entity context to produce a Template::Output.
Source§

fn clone_template(&self) -> T

Clones this template. See Clone.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TypeData for T
where T: 'static + Send + Sync + Clone,

Source§

fn clone_type_data(&self) -> Box<dyn TypeData>

Creates a type-erased clone of self.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more