#[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
impl Any
Sourcepub unsafe fn from_mut_ptr<T>(data: *mut T) -> Selfwhere
T: Any,
pub unsafe fn from_mut_ptr<T>(data: *mut T) -> Selfwhere
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());Sourcepub fn is<T>(&self) -> boolwhere
T: Any,
pub fn is<T>(&self) -> boolwhere
T: Any,
Returns true if the boxed type is the same as T.
§Examples
let any = stk::Any::new(1u32);
assert!(any.is::<u32>());Sourcepub fn downcast_ref<T>(&self) -> Option<&T>where
T: Any,
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>());Sourcepub fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: Any,
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>());Sourcepub fn as_ptr(&self, expected_type: TypeId) -> Option<*const ()>
pub fn as_ptr(&self, expected_type: TypeId) -> Option<*const ()>
Attempt to perform a conversion to a raw pointer.
Sourcepub fn as_mut_ptr(&mut self, expected_type: TypeId) -> Option<*mut ()>
pub fn as_mut_ptr(&mut self, expected_type: TypeId) -> Option<*mut ()>
Attempt to perform a conversion to a raw mutable pointer.
Trait Implementations§
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> 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
Mutably borrows from an owned value. Read more
Source§impl<T> UnsafeFromValue for Twhere
T: FromValue,
impl<T> UnsafeFromValue for Twhere
T: FromValue,
Source§unsafe fn unsafe_from_value(
value: ValuePtr,
vm: &mut Vm,
) -> Result<(T, <T as UnsafeFromValue>::Guard), StackError>
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