Struct polymap::polymap::PolyMap
[−]
[src]
pub struct PolyMap<K: Eq + Hash> { /* fields omitted */ }
A key-value map that can contain varying types of values.
A single PolyMap
instance can map a given key to varying types of values.
Successive operations on this key must use the correct type or the operation
will panic.
If you would like to store only one value of each type,
use TypeMap
.
Example
use polymap::PolyMap; let mut map = PolyMap::new(); // Maps `&str` to `&str`. map.insert("foo", "Hello, world!"); // Maps `&str` to `i32`. map.insert("bar", 123); // Gets a reference to the stored member. let &foo: &&str = map.get("foo").unwrap(); assert_eq!(foo, "Hello, world!"); let &bar: &i32 = map.get("bar").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<K: Eq + Hash> PolyMap<K>
[src]
fn new() -> PolyMap<K>
Constructs a new PolyMap
.
fn with_capacity(n: usize, size: usize) -> PolyMap<K>
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_key<Q: ?Sized>(&self, k: &Q) -> bool where
K: Borrow<Q>,
Q: Eq + Hash,
K: Borrow<Q>,
Q: Eq + Hash,
Returns whether the map contains a value corresponding to the given key. Does not make any assertions about the type of the value.
fn contains_key_of<Q: ?Sized, T: Any>(&self, k: &Q) -> bool where
K: Borrow<Q>,
Q: Eq + Hash,
K: Borrow<Q>,
Q: Eq + Hash,
Returns whether the map contains a value corresponding to the given key whose type is the same as the given type.
Example
use polymap::PolyMap; let mut map = PolyMap::new(); map.insert("foo", 1); assert_eq!(false, map.contains_key_of::<_, &str>("foo")); assert_eq!(true, map.contains_key_of::<_, i32>("foo"));
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<Q: ?Sized, T: Any>(&self, k: &Q) -> Option<&T> where
K: Borrow<Q>,
Q: Eq + Hash,
K: Borrow<Q>,
Q: Eq + Hash,
Returns a reference to the value corresponding to the given key.
If the key is not contained within the map, None
will be returned.
Panics
If the key exists, but the type of value differs from the one requested.
fn get_mut<Q: ?Sized, T: Any>(&mut self, k: &Q) -> Option<&mut T> where
K: Borrow<Q>,
Q: Eq + Hash,
K: Borrow<Q>,
Q: Eq + Hash,
Returns a mutable reference to the value corresponding to the given key.
If the key is not contained within the map, None
will be returned.
Panics
If the key exists, but the type of value differs from the one requested.
fn insert<T: Any>(&mut self, k: K, t: T) -> Option<T>
Inserts a key-value pair into the map. If the key is already present,
that value is returned. Otherwise, None
is returned.
Panics
If the key exists, but has a value of a different type than the one given.
fn keys(&self) -> Keys<K>
Returns an iterator visiting all keys in arbitrary order.
Iterator element type is &K
.
fn len(&self) -> usize
Returns the number of elements in the map.
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, n: 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, n: usize)
Reserves space for at least n
fields.
Does nothing if the capacity is already sufficient.
fn remove<Q: ?Sized, T: Any>(&mut self, k: &Q) -> Option<T> where
K: Borrow<Q>,
Q: Eq + Hash,
K: Borrow<Q>,
Q: Eq + Hash,
Removes a key from the map, returning the value if one existed.
Panics
If the key exists, but the type of value differs from the one requested.
fn shrink_data_to_fit(&mut self)
Shrinks the internal data buffer as close as possible to the size of the currently contained elements.