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
impl AnyMap
Sourcepub fn with_capacity(capacity: usize) -> AnyMap
pub fn with_capacity(capacity: usize) -> AnyMap
Creates a new empty AnyMap with the specified capacity.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements that the map can hold without reallocating.
Sourcepub fn reserve(&mut self, additional: usize)
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.
Sourcepub fn shrink_to_fit(&mut self)
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.
Sourcepub fn get<T: Any>(&self) -> Option<&T>
pub fn get<T: Any>(&self) -> Option<&T>
Returns a reference to the value corresponding to the type T.
Sourcepub fn get_mut<T: Any>(&mut self) -> Option<&mut T>
pub fn get_mut<T: Any>(&mut self) -> Option<&mut T>
Returns a mutable reference to the value corresponding to the type T.
pub fn insert<T: Any + Clone + Send + Sync>(&mut self, value: T) -> Option<T>
Sourcepub fn insert_boxed(&mut self, value: AnyCloneBox) -> Option<AnyCloneBox>
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.
Sourcepub fn try_insert<T: Any + Clone + Send + Sync>(
&mut self,
value: T,
) -> Result<(), T>
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.
Sourcepub fn try_insert_boxed(
&mut self,
value: AnyCloneBox,
) -> Result<(), AnyCloneBox>
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.