Struct polymap::typemap::TypeMap
[−]
[src]
pub struct TypeMap<S = RandomState> { /* fields omitted */ }
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);
Methods
impl TypeMap<RandomState>
[src]
fn new() -> TypeMap
Constructs a new TypeMap
.
fn with_capacity(n: usize) -> TypeMap
Constructs a new PolyMap
with space reserved for n
elements.
impl<S: BuildHasher> TypeMap<S>
[src]
fn with_hasher(hash_builder: S) -> TypeMap<S>
Creates an empty TypeMap
which will use the given hash builder to hash keys.
fn clear(&mut self)
Removes all key-value pairs from the map.
fn contains<T: Any>(&self) -> bool
Returns whether the map contains a value of the given type.
fn capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
fn get<T: Any>(&self) -> Option<&T>
Returns a reference to the value of the given type.
If no value of the given type exists, None
is returned.
fn get_mut<T: Any>(&mut self) -> Option<&mut T>
Returns a mutable reference to the value of the given type.
If no value of the given type exists, None
is returned.
fn insert<T: Any>(&mut self, t: T) -> Option<T>
Inserts a value into the map. If a value of the same type is
already present, that value is returned. Otherwise, None
is returned.
fn len(&self) -> usize
Returns the number of elements in the map.
fn is_empty(&self) -> bool
Returns whether the map is empty.
fn remove<T: Any>(&mut self) -> Option<T>
Removes a value of the given type from the map, returning it.
If no value of the type exists, None
is returned.
fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
additional elements.
fn shrink_to_fit(&mut self)
Shrinks the capacity of the map as much as possible.