Struct polymap::typemap::TypeMap
[−]
[src]
pub struct TypeMap { /* 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);
Notes
Values are stored in an internal buffer that is reallocated when filled.
Methods reserve_data
, reserve_data_exact
, and constructor with_capacity
can be used to reserve a larger buffer ahead of time to prevent expensive
reallocation and move operations.
Methods
impl TypeMap
[src]
fn new() -> TypeMap
Constructs a new TypeMap
.
fn with_capacity(n: usize, size: usize) -> TypeMap
Constructs a new PolyMap
with space reserved for n
fields
and size
bytes of data.
fn clear(&mut self)
Removes all key-value pairs from the map, calling any destructors on stored values.
fn contains<T: Any>(&self) -> bool
Returns whether the map contains a value of the given type.
fn data_capacity(&self) -> usize
Returns the capacity, in bytes, of the internal data buffer.
fn data_size(&self) -> usize
Returns the size, in bytes, of the internal data buffer.
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 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_data(&mut self, additional: usize)
Reserves capacity for at least additional
additional bytes of storage
space within the internal data buffer.
fn reserve_data_exact(&mut self, additional: usize)
Reserves space for at least n
bytes in the internal data buffer.
Does nothing if the capacity is already sufficient.
fn reserve_fields(&mut self, additional: usize)
Reserves capacity for at least additional
additional fields.
fn reserve_fields_exact(&mut self, additional: usize)
Reserves space for at least n
fields.
Does nothing if the capacity is already sufficient.
fn shrink_data_to_fit(&mut self)
Shrinks the internal data buffer as close as possible to the size of the currently contained elements.