Skip to main content

AnyMap

Struct AnyMap 

Source
pub struct AnyMap { /* private fields */ }
Expand description

A type-erased map storing values of different types by their TypeId.

AnyMap allows storing and retrieving values of any type that implements Clone + Send + Sync. The map uses TypeId as keys, enabling type-safe lookups while maintaining type erasure through boxed trait objects.

§Examples

use any_container::AnyMap;

let mut map = AnyMap::new();
map.insert(42i32);
map.insert("world".to_string());

assert_eq!(map.len(), 2);
assert_eq!(*map.get::<i32>().unwrap(), 42);

Implementations§

Source§

impl AnyMap

Source

pub fn new() -> AnyMap

Creates a new empty AnyMap.

Source

pub fn with_capacity(capacity: usize) -> AnyMap

Creates a new empty AnyMap with the specified capacity.

Source

pub fn capacity(&self) -> usize

Returns the number of elements that the map can hold without reallocating.

Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements in the map. The map may reserve more space than requested to avoid frequent reallocations.

Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the map as much as possible. This reduces the allocated memory to fit the current contents.

Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

Source

pub fn clear(&mut self)

Removes all elements from the map.

Source

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

Returns a reference to the value corresponding to the type T.

Source

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

Returns a mutable reference to the value corresponding to the type T.

Source

pub fn insert<T: Any + Clone + Send + Sync>(&mut self, value: T) -> Option<T>

Source

pub fn insert_boxed(&mut self, value: AnyCloneBox) -> Option<AnyCloneBox>

Inserts a boxed value into the map. If a value of the same TypeId already exists, it will be replaced and the old value will be returned. This method is useful when you already have an AnyCloneBox instance.

Source

pub fn try_insert<T: Any + Clone + Send + Sync>( &mut self, value: T, ) -> Result<(), T>

Inserts a value of type T into the map. If a value of type T already exists, it will not be replaced and the new value will be returned as an error.

Source

pub fn try_insert_boxed( &mut self, value: AnyCloneBox, ) -> Result<(), AnyCloneBox>

Inserts a boxed value into the map. If a value of the same type already exists, it will not be replaced and the new value will be returned as an error.

Source

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

Removes the stored value of type T from the map and returns it, if it exists.

Source

pub fn contains<T: Any>(&self) -> bool

Returns true if the map contains a value of type T.

Trait Implementations§

Source§

impl Clone for AnyMap

Source§

fn clone(&self) -> AnyMap

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 Debug for AnyMap

Source§

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

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

impl Default for AnyMap

Source§

fn default() -> Self

Returns the “default value” for a type. 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.