pub struct TypeMap<S = RandomState> { /* private fields */ }
Expand description
A container for values of varying types.
Values contained in a TypeMap
are stored uniquely according to their type.
This means that, for each possible type, only one value may be stored in
a single TypeMap
instance.
If you would like to store multiple values of a given type, mapped to
individual keys, use PolyMap
.
§Example
use polymap::TypeMap;
let mut map = TypeMap::new();
// Stores a `&str` value
map.insert("Hello, world!");
// Stores an `i32` value
map.insert(123);
// Gets a reference to the stored value
let &foo: &&str = map.get().unwrap();
assert_eq!(foo, "Hello, world!");
let &bar: &i32 = map.get().unwrap();
assert_eq!(bar, 123);
Implementations§
Source§impl TypeMap<RandomState>
impl TypeMap<RandomState>
Source§impl<S: BuildHasher> TypeMap<S>
impl<S: BuildHasher> TypeMap<S>
Sourcepub fn with_hasher(hash_builder: S) -> TypeMap<S>
pub fn with_hasher(hash_builder: S) -> TypeMap<S>
Creates an empty TypeMap
which will use the given hash builder to hash keys.
Sourcepub fn contains<T: Any>(&self) -> bool
pub fn contains<T: Any>(&self) -> bool
Returns whether the map contains a value corresponding to the given type.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
Sourcepub fn entry<T: Any>(&mut self) -> Entry<'_, T>
pub fn entry<T: Any>(&mut self) -> Entry<'_, T>
Returns the type’s corresponding entry in the map for in-place manipulation.
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 given type.
If the type is not contained within the map, None
will be returned.
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 given type.
If the type is not contained within the map, None
will be returned.
Sourcepub fn insert<T: Any>(&mut self, t: T) -> Option<T>
pub fn insert<T: Any>(&mut self, t: T) -> Option<T>
Inserts a value into the map. If a value of that type is already present,
that value is returned. Otherwise, None
is returned.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
additional elements.
Sourcepub fn remove<T: Any>(&mut self) -> Option<T>
pub fn remove<T: Any>(&mut self) -> Option<T>
Removes a value from the map, returning the value if one existed.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the map as much as possible.