pub struct AnyCloneBox { /* private fields */ }Expand description
A type-erased cloneable box that stores any Clone + Send + Sync type.
AnyCloneBox enables runtime type-erased storage of values while maintaining
the ability to clone and downcast them to their original types. It is the
fundamental building block for type-erased containers in the crate.
§Examples
use any_container::AnyCloneBox;
// Create an AnyCloneBox with an i32
let boxed = AnyCloneBox::new(42i32);
// Downcast to get the original value
assert_eq!(*boxed.downcast_ref::<i32>().unwrap(), 42);
// Downcasting to a wrong type returns None
assert!(boxed.downcast_ref::<String>().is_none());
// Clone the boxed value
let _clone = boxed.clone();Implementations§
Source§impl AnyCloneBox
impl AnyCloneBox
Sourcepub fn new<T: Clone + Send + Sync + 'static>(value: T) -> Self
pub fn new<T: Clone + Send + Sync + 'static>(value: T) -> Self
Creates a new AnyCloneBox containing the given value.
Sourcepub fn type_name(&self) -> &'static str
pub fn type_name(&self) -> &'static str
Returns the type name of the value contained in this AnyCloneBox.
Sourcepub fn downcast_ref<T: Any>(&self) -> Option<&T>
pub fn downcast_ref<T: Any>(&self) -> Option<&T>
Attempts to downcast the AnyCloneBox to an immutable reference of type T.
Sourcepub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T
pub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T
Unsafely downcasts the AnyCloneBox to an immutable reference of type T without checking the type.
§Safety
The caller must ensure that the type T is the same as the type of the value contained in
this AnyCloneBox. If the types do not match, this will result in undefined behaviour.
Sourcepub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T>
Attempts to downcast the AnyCloneBox to a mutable reference of type T.
This method allows mutation of the contained value through a type-safe interface.
Returns None if the contained type doesn’t match T.
Sourcepub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T
pub unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T
Unsafely downcasts the AnyCloneBox to a mutable reference of type T without checking the type.
§Safety
The caller must ensure that the type T is the same as the type of the value contained in
this AnyCloneBox. If the types do not match, this will result in undefined behaviour.
Sourcepub fn downcast<T: Any>(self) -> Result<Box<T>, Self>
pub fn downcast<T: Any>(self) -> Result<Box<T>, Self>
Consumes the AnyCloneBox and attempts to downcast it to a boxed value of type T.
Sourcepub unsafe fn downcast_unchecked<T: Any>(self) -> Box<T>
pub unsafe fn downcast_unchecked<T: Any>(self) -> Box<T>
Unsafely downcasts the AnyCloneBox to a boxed value of type T without checking the type.
§Safety
The caller must ensure that the type T is the same as the type of the value contained in
this AnyCloneBox. If the types do not match, this will result in undefined behaviour.