Skip to main content

AnyCloneBox

Struct AnyCloneBox 

Source
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

Source

pub fn new<T: Clone + Send + Sync + 'static>(value: T) -> Self

Creates a new AnyCloneBox containing the given value.

Source

pub fn type_id(&self) -> TypeId

Returns the TypeId of the value contained in this AnyCloneBox.

Source

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

Returns the type name of the value contained in this AnyCloneBox.

Source

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

Attempts to downcast the AnyCloneBox to an immutable reference of type T.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for AnyCloneBox

Source§

fn clone(&self) -> Self

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

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