pub struct PolyMap<K: Eq + Hash, S = RandomState> { /* private fields */ }
Expand description
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);
Implementations§
Source§impl<K: Eq + Hash, S: BuildHasher> PolyMap<K, S>
impl<K: Eq + Hash, S: BuildHasher> PolyMap<K, S>
Sourcepub fn with_hasher(hash_builder: S) -> PolyMap<K, S>
pub fn with_hasher(hash_builder: S) -> PolyMap<K, S>
Creates an empty PolyMap
which will use the given hash builder to hash keys.
Sourcepub fn contains_key<Q>(&self, k: &Q) -> bool
pub fn contains_key<Q>(&self, k: &Q) -> bool
Returns whether the map contains a value corresponding to the given key. Does not make any assertions about the type of the value.
Sourcepub fn contains_key_of<Q, T: Any>(&self, k: &Q) -> bool
pub fn contains_key_of<Q, T: Any>(&self, k: &Q) -> bool
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"));
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, key: K) -> Entry<'_, K, T>
pub fn entry<T: Any>(&mut self, key: K) -> Entry<'_, K, T>
Returns the key’s corresponding entry in the map for in-place manipulation.
Whether the entry is occupied or vacant, the type of value that can be
inserted into the returned entry is constrained to T
.
§Panics
If the entry exists, but the type of value differs from the one requested.
Sourcepub fn get<Q, T: Any>(&self, k: &Q) -> Option<&T>
pub fn get<Q, T: Any>(&self, k: &Q) -> Option<&T>
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.
Sourcepub fn get_mut<Q, T: Any>(&mut self, k: &Q) -> Option<&mut T>
pub fn get_mut<Q, T: Any>(&mut self, k: &Q) -> Option<&mut T>
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.
Sourcepub fn insert<T: Any>(&mut self, k: K, t: T) -> Option<T>
pub 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.
Sourcepub fn keys(&self) -> Keys<'_, K> ⓘ
pub fn keys(&self) -> Keys<'_, K> ⓘ
Returns an iterator visiting all keys in arbitrary order.
Iterator element type is &K
.
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<Q, T: Any>(&mut self, k: &Q) -> Option<T>
pub fn remove<Q, T: Any>(&mut self, k: &Q) -> Option<T>
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.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the map as much as possible.