Struct Any

Source
#[repr(C)]
pub struct Any { /* private fields */ }
Expand description

Our own private dynamic Any implementation.

In contrast to Box<dyn std::any::Any>, this allows for storing a raw pointer directly in the object to avoid one level of indirection. Otherwise it’s equivalent.

Implementations§

Source§

impl Any

Source

pub fn new<T>(data: T) -> Self
where T: Any,

Construct a new any from the original any.

Source

pub unsafe fn from_ptr<T>(data: *const T) -> Self
where T: Any,

Construct a new any from a pointer.

§Safety

It is up to the caller to make sure that whatever data is pointed to is valid for the duration of the Any.

§Examples
let value = 1u32;
let any = unsafe { stk::Any::from_ptr(&value) };
assert!(any.is::<u32>());
assert_eq!(Some(&1u32), any.downcast_ref());
Source

pub unsafe fn from_mut_ptr<T>(data: *mut T) -> Self
where T: Any,

Construct a new any from a mutable pointer.

§Safety

It is up to the caller to make sure that whatever data is pointed to is valid for the duration of the Any.

§Examples
let mut value = 1u32;
let mut any = unsafe { stk::Any::from_mut_ptr(&mut value) };
assert!(any.is::<u32>());
*any.downcast_mut::<u32>().unwrap() = 2;
assert_eq!(Some(&2u32), any.downcast_ref());
Source

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

Returns true if the boxed type is the same as T.

§Examples
let any = stk::Any::new(1u32);
assert!(any.is::<u32>());
Source

pub fn downcast_ref<T>(&self) -> Option<&T>
where T: Any,

Returns some reference to the boxed value if it is of type T, or None if it isn’t.

§Examples
let any = stk::Any::new(1u32);
assert_eq!(Some(&1u32), any.downcast_ref::<u32>());
assert_eq!(None, any.downcast_ref::<&u32>());
Source

pub fn downcast_mut<T>(&mut self) -> Option<&mut T>
where T: Any,

Returns some mutable reference to the boxed value if it is of type T, or None if it isn’t.

§Examples
let mut any = stk::Any::new(1u32);
*any.downcast_mut::<u32>().unwrap() = 2;
assert_eq!(Some(&2u32), any.downcast_ref::<u32>());
Source

pub fn as_ptr(&self, expected_type: TypeId) -> Option<*const ()>

Attempt to perform a conversion to a raw pointer.

Source

pub fn as_mut_ptr(&mut self, expected_type: TypeId) -> Option<*mut ()>

Attempt to perform a conversion to a raw mutable pointer.

Source

pub fn take_mut_ptr(self, expected_type: TypeId) -> Result<*mut (), Self>

Attempt to perform a conversion to a raw mutable pointer with the intent of taking it.

If the conversion is not possible, we return a reconstructed Any as the error variant.

Source

pub fn type_name(&self) -> &'static str

Access the underlying type name for the data.

Source

pub fn type_id(&self) -> TypeId

Access the underlying type id for the data.

Trait Implementations§

Source§

impl Debug for Any

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Any

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl FromValue for Any

Source§

fn from_value(value: ValuePtr, vm: &mut Vm) -> Result<Self, StackError>

Try to convert to the given type, from the given value.

Auto Trait Implementations§

§

impl Freeze for Any

§

impl RefUnwindSafe for Any

§

impl !Send for Any

§

impl !Sync for Any

§

impl Unpin for Any

§

impl UnwindSafe for Any

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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> UnsafeFromValue for T
where T: FromValue,

Source§

type Guard = ()

The raw guard returned. Read more
Source§

unsafe fn unsafe_from_value( value: ValuePtr, vm: &mut Vm, ) -> Result<(T, <T as UnsafeFromValue>::Guard), StackError>

Convert the given reference using unsafe assumptions to a value. Read more
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V