Struct polymap::polymap::PolyMap
[−]
[src]
pub struct PolyMap<K: Eq + Hash, S = RandomState> { /* 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);
Methods
impl<K: Eq + Hash> PolyMap<K, RandomState>
[src]
fn new() -> PolyMap<K>
Constructs a new PolyMap
.
fn with_capacity(n: usize) -> PolyMap<K>
Constructs a new PolyMap
with space reserved for n
elements.
impl<K: Eq + Hash, S: BuildHasher> PolyMap<K, S>
[src]
fn with_hasher(hash_builder: S) -> PolyMap<K, S>
Creates an empty PolyMap
which will use the given hash builder to hash keys.
fn clear(&mut self)
Removes all key-value pairs from the map.
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 capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
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 is_empty(&self) -> bool
Returns whether the map is empty.
fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
additional elements.
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_to_fit(&mut self)
Shrinks the capacity of the map as much as possible.