pub struct PopulatedHashMap<K, V, S = RandomState>(/* private fields */);Expand description
PopulatedHashMap is a wrapper around HashMap that guarantees that the
map is non-empty. This is useful when you want to ensure that a map is
populated before using it.
Implementations§
Source§impl<K: Eq + Hash, V> PopulatedHashMap<K, V, RandomState>
impl<K: Eq + Hash, V> PopulatedHashMap<K, V, RandomState>
Sourcepub fn new(key: K, value: V) -> PopulatedHashMap<K, V, RandomState>
pub fn new(key: K, value: V) -> PopulatedHashMap<K, V, RandomState>
Creates a singleton populated hash map i.e. a hash map with a single key-value pair.
Sourcepub fn with_capacity(
capacity: NonZeroUsize,
key: K,
value: V,
) -> PopulatedHashMap<K, V, RandomState>
pub fn with_capacity( capacity: NonZeroUsize, key: K, value: V, ) -> PopulatedHashMap<K, V, RandomState>
Creates an empty PopulatedHashMap with the specified capacity
and containing the given key-value-pair.
Note the capacity must be non-zero and a key value pair must be supplied because this is a populated hash map i.e. non-empty hash map.
Source§impl<K: Eq + Hash, V, S: BuildHasher> PopulatedHashMap<K, V, S>
impl<K: Eq + Hash, V, S: BuildHasher> PopulatedHashMap<K, V, S>
pub fn with_hasher( hash_builder: S, key: K, value: V, ) -> PopulatedHashMap<K, V, S>
pub fn with_capacity_and_hasher( capacity: NonZeroUsize, hash_builder: S, key: K, value: V, ) -> PopulatedHashMap<K, V, S>
Sourcepub fn get<Q: Hash + Eq + ?Sized>(&self, k: &Q) -> Option<&V>where
K: Borrow<Q>,
pub fn get<Q: Hash + Eq + ?Sized>(&self, k: &Q) -> Option<&V>where
K: Borrow<Q>,
Returns a reference to the value corresponding to the key.
The key may be any borrowed form of the map’s key type, but Hash
and Eq on the borrowed form must match those for the key type.
Sourcepub fn get_key_value<Q: Hash + Eq + ?Sized>(&self, k: &Q) -> Option<(&K, &V)>where
K: Borrow<Q>,
pub fn get_key_value<Q: Hash + Eq + ?Sized>(&self, k: &Q) -> Option<(&K, &V)>where
K: Borrow<Q>,
Returns the key-value pair corresponding to the supplied key.
The supplied key may be any borrowed form of the map’s key type,
but Hash and Eq on the borrowed form must match those for the key
type.
Sourcepub fn contains_key<Q: Hash + Eq + ?Sized>(&self, k: &Q) -> boolwhere
K: Borrow<Q>,
pub fn contains_key<Q: Hash + Eq + ?Sized>(&self, k: &Q) -> boolwhere
K: Borrow<Q>,
Returns true if the map contains a value for the specified key.
The key may be any borrowed form of the map’s key type, but Hash
and Eq on the borrowed form must match those for the key type.
Sourcepub fn get_mut<Q: Hash + Eq + ?Sized>(&mut self, k: &Q) -> Option<&mut V>where
K: Borrow<Q>,
pub fn get_mut<Q: Hash + Eq + ?Sized>(&mut self, k: &Q) -> Option<&mut V>where
K: Borrow<Q>,
Returns a mutable reference to the value corresponding to the key.
The key may be any borrowed form of the map’s key type, but Hash
and Eq on the borrowed form must match those for the key type.
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts a key-value pair into the map.
If the map did not have this key present, None is returned.
If the map did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be == without being identical. See the module-level documentation for more.
Sourcepub fn inserted(
hash_map: HashMap<K, V, S>,
key: K,
value: V,
) -> (Option<V>, PopulatedHashMap<K, V, S>)
pub fn inserted( hash_map: HashMap<K, V, S>, key: K, value: V, ) -> (Option<V>, PopulatedHashMap<K, V, S>)
Inserts a key-value pair into the map return the map as a PopulatedHashMap since inserting guarantees a len() > 0.
If the map did not have this key present, None is returned.
If the map did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be == without being identical. See the module-level documentation for more.
This method is useful when you want to ensure that the map is populated after inserting a key-value pair.
use std::collections::HashMap;
use populated::PopulatedHashMap;
use std::num::NonZeroUsize;
let mut hash_map = HashMap::from([(42, "the answer")]);
let (old, populated_hash_map) = PopulatedHashMap::inserted(hash_map, 42, "the updated answer");
assert_eq!(populated_hash_map.len(), NonZeroUsize::new(1).unwrap());
assert_eq!(old, Some("the answer"));Sourcepub fn remove<Q: Hash + Eq + ?Sized>(&mut self, k: &Q) -> Option<V>where
K: Borrow<Q>,
pub fn remove<Q: Hash + Eq + ?Sized>(&mut self, k: &Q) -> Option<V>where
K: Borrow<Q>,
Removes a key from the map, returning the value at the key if the key was previously in the map.
The key may be any borrowed form of the map’s key type, but Hash and
Eq on the borrowed form must match those for the key type.
Sourcepub fn remove_entry<Q: Hash + Eq + ?Sized>(&mut self, k: &Q) -> Option<(K, V)>where
K: Borrow<Q>,
pub fn remove_entry<Q: Hash + Eq + ?Sized>(&mut self, k: &Q) -> Option<(K, V)>where
K: Borrow<Q>,
Removes a key from the map, returning the stored key and value if the key was previously in the map.
The key may be any borrowed form of the map’s key type, but Hash
and Eq on the borrowed form must match those for the key type.
Source§impl<K, V, S> PopulatedHashMap<K, V, S>
impl<K, V, S> PopulatedHashMap<K, V, S>
Sourcepub fn capacity(&self) -> NonZeroUsize
pub fn capacity(&self) -> NonZeroUsize
Returns the number of elements the map can hold without reallocating.
This number is a lower bound; the PopulatedHashMap<K, V> might
be able to hold more, but is guaranteed to be able to hold at
least this many.
Sourcepub fn len(&self) -> NonZeroUsize
pub fn len(&self) -> NonZeroUsize
Returns the number of elements in the map.
pub fn retain( self, predicate: impl FnMut(&K, &mut V) -> bool, ) -> HashMap<K, V, S>
pub fn clear(self) -> HashMap<K, V, S>
pub fn hasher(&self) -> &S
Source§impl<K, V, S> PopulatedHashMap<K, V, S>
impl<K, V, S> PopulatedHashMap<K, V, S>
pub fn iter(&self) -> PopulatedIter<'_, K, V>
pub fn iter_mut(&mut self) -> PopulatedIterMut<'_, K, V>
pub fn keys(&self) -> PopulatedKeys<'_, K, V>
pub fn values(&self) -> PopulatedValues<'_, K, V>
pub fn values_mut(&mut self) -> PopulatedValuesMut<'_, K, V>
Trait Implementations§
Source§impl<K: Clone, V: Clone, S: Clone> Clone for PopulatedHashMap<K, V, S>
impl<K: Clone, V: Clone, S: Clone> Clone for PopulatedHashMap<K, V, S>
Source§fn clone(&self) -> PopulatedHashMap<K, V, S>
fn clone(&self) -> PopulatedHashMap<K, V, S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<K, V, S> From<PopulatedHashMap<K, V, S>> for HashMap<K, V, S>
impl<K, V, S> From<PopulatedHashMap<K, V, S>> for HashMap<K, V, S>
Source§fn from(populated_hash_map: PopulatedHashMap<K, V, S>) -> HashMap<K, V, S>
fn from(populated_hash_map: PopulatedHashMap<K, V, S>) -> HashMap<K, V, S>
Source§impl<K: Eq + Hash, V> FromPopulatedIterator<(K, V)> for PopulatedHashMap<K, V>
impl<K: Eq + Hash, V> FromPopulatedIterator<(K, V)> for PopulatedHashMap<K, V>
Source§fn from_populated_iter(iter: impl IntoPopulatedIterator<Item = (K, V)>) -> Self
fn from_populated_iter(iter: impl IntoPopulatedIterator<Item = (K, V)>) -> Self
PopulatedIterator into Self.Source§impl<Q: Eq + Hash + ?Sized, K: Eq + Hash + Borrow<Q>, V, S: BuildHasher> Index<&Q> for PopulatedHashMap<K, V, S>
impl<Q: Eq + Hash + ?Sized, K: Eq + Hash + Borrow<Q>, V, S: BuildHasher> Index<&Q> for PopulatedHashMap<K, V, S>
Source§impl<'a, K, V, S> IntoIterator for &'a PopulatedHashMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a PopulatedHashMap<K, V, S>
Source§impl<'a, K, V, S> IntoIterator for &'a mut PopulatedHashMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a mut PopulatedHashMap<K, V, S>
Source§impl<K, V, S> IntoIterator for PopulatedHashMap<K, V, S>
impl<K, V, S> IntoIterator for PopulatedHashMap<K, V, S>
Source§impl<'a, K, V, S> IntoPopulatedIterator for &'a PopulatedHashMap<K, V, S>
impl<'a, K, V, S> IntoPopulatedIterator for &'a PopulatedHashMap<K, V, S>
type PopulatedIntoIter = PopulatedIter<'a, K, V>
Source§fn into_populated_iter(self) -> PopulatedIter<'a, K, V>
fn into_populated_iter(self) -> PopulatedIter<'a, K, V>
PopulatedIterator.Source§impl<'a, K, V, S> IntoPopulatedIterator for &'a mut PopulatedHashMap<K, V, S>
impl<'a, K, V, S> IntoPopulatedIterator for &'a mut PopulatedHashMap<K, V, S>
type PopulatedIntoIter = PopulatedIterMut<'a, K, V>
Source§fn into_populated_iter(self) -> PopulatedIterMut<'a, K, V>
fn into_populated_iter(self) -> PopulatedIterMut<'a, K, V>
PopulatedIterator.Source§impl<K, V, S> IntoPopulatedIterator for PopulatedHashMap<K, V, S>
impl<K, V, S> IntoPopulatedIterator for PopulatedHashMap<K, V, S>
type PopulatedIntoIter = PopulatedIntoIter<K, V>
Source§fn into_populated_iter(self) -> PopulatedIntoIter<K, V>
fn into_populated_iter(self) -> PopulatedIntoIter<K, V>
PopulatedIterator.