PopulatedHashMap

Struct PopulatedHashMap 

Source
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>

Source

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.

Source

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>

Source

pub fn with_hasher( hash_builder: S, key: K, value: V, ) -> PopulatedHashMap<K, V, S>

Source

pub fn with_capacity_and_hasher( capacity: NonZeroUsize, hash_builder: S, key: K, value: V, ) -> PopulatedHashMap<K, V, S>

Source

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.

Source

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.

Source

pub fn contains_key<Q: Hash + Eq + ?Sized>(&self, k: &Q) -> bool
where 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.

Source

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.

Source

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.

Source

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"));
Source

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.

Source

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>

Source

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.

Source

pub fn len(&self) -> NonZeroUsize

Returns the number of elements in the map.

Source

pub fn retain( self, predicate: impl FnMut(&K, &mut V) -> bool, ) -> HashMap<K, V, S>

Source

pub fn clear(self) -> HashMap<K, V, S>

Source

pub fn hasher(&self) -> &S

Source§

impl<K, V, S> PopulatedHashMap<K, V, S>

Source

pub fn iter(&self) -> PopulatedIter<'_, K, V>

Source

pub fn iter_mut(&mut self) -> PopulatedIterMut<'_, K, V>

Source

pub fn keys(&self) -> PopulatedKeys<'_, K, V>

Source

pub fn values(&self) -> PopulatedValues<'_, K, V>

Source

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>

Source§

fn clone(&self) -> PopulatedHashMap<K, V, S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug, S: Debug> Debug for PopulatedHashMap<K, V, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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>

Converts to this type from the input type.
Source§

impl<K: Eq + Hash, V> FromPopulatedIterator<(K, V)> for PopulatedHashMap<K, V>

Source§

fn from_populated_iter(iter: impl IntoPopulatedIterator<Item = (K, V)>) -> Self

Converts a PopulatedIterator into Self.
Source§

impl<Q: Eq + Hash + ?Sized, K: Eq + Hash + Borrow<Q>, V, S: BuildHasher> Index<&Q> for PopulatedHashMap<K, V, S>

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, index: &Q) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, K, V, S> IntoIterator for &'a PopulatedHashMap<K, V, S>

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V, S> IntoIterator for &'a mut PopulatedHashMap<K, V, S>

Source§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V, S> IntoIterator for PopulatedHashMap<K, V, S>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V, S> IntoPopulatedIterator for &'a PopulatedHashMap<K, V, S>

Source§

type PopulatedIntoIter = PopulatedIter<'a, K, V>

Source§

fn into_populated_iter(self) -> PopulatedIter<'a, K, V>

Converts the type into a PopulatedIterator.
Source§

impl<'a, K, V, S> IntoPopulatedIterator for &'a mut PopulatedHashMap<K, V, S>

Source§

type PopulatedIntoIter = PopulatedIterMut<'a, K, V>

Source§

fn into_populated_iter(self) -> PopulatedIterMut<'a, K, V>

Converts the type into a PopulatedIterator.
Source§

impl<K, V, S> IntoPopulatedIterator for PopulatedHashMap<K, V, S>

Source§

type PopulatedIntoIter = PopulatedIntoIter<K, V>

Source§

fn into_populated_iter(self) -> PopulatedIntoIter<K, V>

Converts the type into a PopulatedIterator.
Source§

impl<K, V, S> TryFrom<HashMap<K, V, S>> for PopulatedHashMap<K, V, S>

Source§

type Error = HashMap<K, V, S>

The type returned in the event of a conversion error.
Source§

fn try_from( hash_map: HashMap<K, V, S>, ) -> Result<PopulatedHashMap<K, V, S>, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<K, V, S> Freeze for PopulatedHashMap<K, V, S>
where S: Freeze,

§

impl<K, V, S> RefUnwindSafe for PopulatedHashMap<K, V, S>

§

impl<K, V, S> Send for PopulatedHashMap<K, V, S>
where S: Send, K: Send, V: Send,

§

impl<K, V, S> Sync for PopulatedHashMap<K, V, S>
where S: Sync, K: Sync, V: Sync,

§

impl<K, V, S> Unpin for PopulatedHashMap<K, V, S>
where S: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, S> UnwindSafe for PopulatedHashMap<K, V, S>
where K: UnwindSafe, V: UnwindSafe, S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.