pub struct NEMap<K, V, S = RandomState> { /* private fields */ }Expand description
A non-empty, growable HashMap.
use nonempty_collections::nem;
let m = nem!["elves" => 3000, "orcs" => 10000];
assert_eq!(2, m.len().get());Implementations§
Source§impl<K, V> NEMap<K, V>
impl<K, V> NEMap<K, V>
Sourcepub fn with_capacity(capacity: NonZeroUsize, k: K, v: V) -> NEMap<K, V>
pub fn with_capacity(capacity: NonZeroUsize, k: K, v: V) -> NEMap<K, V>
Creates a new NEMap with a single element and specified capacity.
use std::num::*;
use nonempty_collections::*;
let map = NEMap::with_capacity(NonZeroUsize::MIN, 1, 1);
assert_eq!(nem! { 1 => 1 }, map);
assert!(map.capacity().get() >= 1);Source§impl<K, V, S> NEMap<K, V, S>
impl<K, V, S> NEMap<K, V, S>
Sourcepub fn try_from_map(map: HashMap<K, V, S>) -> Option<Self>
pub fn try_from_map(map: HashMap<K, V, S>) -> Option<Self>
Attempt a conversion from HashMap, consuming the given HashMap.
Will return None if the HashMap is empty.
use std::collections::*;
use nonempty_collections::*;
let mut map = HashMap::new();
map.extend([("a", 1), ("b", 2)]);
assert_eq!(Some(nem! {"a" => 1, "b" => 2}), NEMap::try_from_map(map));
let map: HashMap<(), ()> = HashMap::new();
assert_eq!(None, NEMap::try_from_map(map));Sourcepub fn capacity(&self) -> NonZeroUsize
pub fn capacity(&self) -> NonZeroUsize
Returns the number of elements the map can hold without reallocating.
Sourcepub fn iter(&self) -> Iter<'_, K, V>
pub fn iter(&self) -> Iter<'_, K, V>
Returns a regular iterator over the entries in this non-empty map.
For a NonEmptyIterator see Self::nonempty_iter().
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V>
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>
Returns a regular mutable iterator over the entries in this non-empty map.
For a NonEmptyIterator see Self::nonempty_iter_mut().
Sourcepub fn nonempty_iter(&self) -> Iter<'_, K, V>
pub fn nonempty_iter(&self) -> Iter<'_, K, V>
An iterator visiting all elements in arbitrary order. The iterator
element type is (&'a K, &'a V).
Sourcepub fn nonempty_iter_mut(&mut self) -> IterMut<'_, K, V>
pub fn nonempty_iter_mut(&mut self) -> IterMut<'_, K, V>
An iterator visiting all elements in arbitrary order. The iterator
element type is (&'a K, &'a mut V).
§Panics
If you manually advance this iterator until empty and then call first,
you’re in for a surprise.
Sourcepub fn keys(&self) -> Keys<'_, K, V>
pub fn keys(&self) -> Keys<'_, K, V>
An iterator visiting all keys in arbitrary order. The iterator element
type is &'a K.
use nonempty_collections::*;
let m = nem!["Valmar" => "Vanyar", "Tirion" => "Noldor", "Alqualondë" => "Teleri"];
let mut v: NEVec<_> = m.keys().collect();
v.sort();
assert_eq!(nev![&"Alqualondë", &"Tirion", &"Valmar"], v);Sourcepub fn len(&self) -> NonZeroUsize
pub fn len(&self) -> NonZeroUsize
Returns the number of elements in the map. Always 1 or more.
use nonempty_collections::nem;
let m = nem!["a" => 1, "b" => 2];
assert_eq!(2, m.len().get());Sourcepub const fn is_empty(&self) -> bool
👎Deprecated since 0.1.0: A NEMap is never empty.
pub const fn is_empty(&self) -> bool
A NEMap is never empty.
Sourcepub fn values(&self) -> Values<'_, K, V>
pub fn values(&self) -> Values<'_, K, V>
An iterator visiting all values in arbitrary order. The iterator element
type is &'a V.
use nonempty_collections::*;
let m = nem!["Valmar" => "Vanyar", "Tirion" => "Noldor", "Alqualondë" => "Teleri"];
let mut v: NEVec<_> = m.values().collect();
v.sort();
assert_eq!(nev![&"Noldor", &"Teleri", &"Vanyar"], v);Source§impl<K, V, S> NEMap<K, V, S>
impl<K, V, S> NEMap<K, V, S>
Sourcepub fn contains_key<Q>(&self, k: &Q) -> bool
pub fn contains_key<Q>(&self, k: &Q) -> bool
Returns true if the map contains a value.
use nonempty_collections::nem;
let m = nem!["Jack" => 8];
assert!(m.contains_key("Jack"));
assert!(!m.contains_key("Colin"));Sourcepub fn get<Q>(&self, k: &Q) -> Option<&V>
pub fn get<Q>(&self, k: &Q) -> Option<&V>
Returns a reference to the value corresponding to the key.
The key may be any borrowed form of the map’s value type, but Hash and
Eq on the borrowed form must match those for the key type.
use nonempty_collections::nem;
let m = nem!["silmarils" => 3];
assert_eq!(Some(&3), m.get("silmarils"));
assert_eq!(None, m.get("arkenstone"));Sourcepub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
Returns the key-value pair corresponding to the key.
The key may be any borrowed form of the map’s value type, but Hash and
Eq on the borrowed form must match those for the key type.
use nonempty_collections::nem;
let m = nem!["silmarils" => 3];
assert_eq!(Some((&"silmarils", &3)), m.get_key_value("silmarils"));
assert_eq!(None, m.get_key_value("arkenstone"));Sourcepub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
Returns a reference to the value corresponding to the key.
The key may be any borrowed form of the map’s value type, but Hash and
Eq on the borrowed form must match those for the key type.
use nonempty_collections::nem;
let mut m = nem!["silmarils" => 3];
let mut v = m.get_mut("silmarils").unwrap();
// And thus it came to pass that the Silmarils found their long homes:
// one in the airs of heaven, and one in the fires of the heart of the
// world, and one in the deep waters.
*v -= 3;
assert_eq!(Some(&0), m.get("silmarils"));Sourcepub fn insert(&mut self, k: K, v: V) -> Option<V>
pub fn insert(&mut self, k: K, v: V) -> Option<V>
Insert a key-value pair into the map.
If the map did not have this 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 HashMap::insert
for more.
use nonempty_collections::nem;
let mut m = nem!["Vilya" => "Elrond", "Nenya" => "Galadriel"];
assert_eq!(None, m.insert("Narya", "Cirdan"));
// The Ring of Fire was given to Gandalf upon his arrival in Middle Earth.
assert_eq!(Some("Cirdan"), m.insert("Narya", "Gandalf"));Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
Sourcepub fn with_capacity_and_hasher(
capacity: NonZeroUsize,
hasher: S,
k: K,
v: V,
) -> NEMap<K, V, S>
pub fn with_capacity_and_hasher( capacity: NonZeroUsize, hasher: S, k: K, v: V, ) -> NEMap<K, V, S>
Sourcepub fn with_hasher(hasher: S, k: K, v: V) -> NEMap<K, V, S>
pub fn with_hasher(hasher: S, k: K, v: V) -> NEMap<K, V, S>
See HashMap::with_hasher.
Trait Implementations§
Source§impl<'de, K, V, S> Deserialize<'de> for NEMap<K, V, S>
impl<'de, K, V, S> Deserialize<'de> for NEMap<K, V, S>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<K, V> Extend<(K, V)> for NEMap<K, V>
impl<K, V> Extend<(K, V)> for NEMap<K, V>
Source§fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K, V, S> FromNonEmptyIterator<(K, V)> for NEMap<K, V, S>
use nonempty_collections::*;
let v = nev![('a', 1), ('b', 2), ('c', 3), ('a', 4)];
let m0: NEMap<_, _> = v.into_nonempty_iter().collect();
let m1: NEMap<_, _> = nem!['a' => 4, 'b' => 2, 'c' => 3];
assert_eq!(m0, m1);
impl<K, V, S> FromNonEmptyIterator<(K, V)> for NEMap<K, V, S>
use nonempty_collections::*;
let v = nev![('a', 1), ('b', 2), ('c', 3), ('a', 4)];
let m0: NEMap<_, _> = v.into_nonempty_iter().collect();
let m1: NEMap<_, _> = nem!['a' => 4, 'b' => 2, 'c' => 3];
assert_eq!(m0, m1);Source§fn from_nonempty_iter<I>(iter: I) -> Selfwhere
I: IntoNonEmptyIterator<Item = (K, V)>,
fn from_nonempty_iter<I>(iter: I) -> Selfwhere
I: IntoNonEmptyIterator<Item = (K, V)>,
NonEmptyIterator.Source§impl<'a, K, V, S> IntoIterator for &'a NEMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a NEMap<K, V, S>
Source§impl<'a, K, V, S> IntoIterator for &'a mut NEMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a mut NEMap<K, V, S>
Source§impl<K, V, S> IntoIterator for NEMap<K, V, S>
impl<K, V, S> IntoIterator for NEMap<K, V, S>
Source§impl<'a, K, V, S> IntoNonEmptyIterator for &'a NEMap<K, V, S>
impl<'a, K, V, S> IntoNonEmptyIterator for &'a NEMap<K, V, S>
Source§type IntoNEIter = Iter<'a, K, V>
type IntoNEIter = Iter<'a, K, V>
NonEmptyIterator are we turning this into?Source§fn into_nonempty_iter(self) -> Self::IntoNEIter
fn into_nonempty_iter(self) -> Self::IntoNEIter
NonEmptyIterator from a value.Source§impl<K, V, S> IntoNonEmptyIterator for NEMap<K, V, S>
impl<K, V, S> IntoNonEmptyIterator for NEMap<K, V, S>
Source§type IntoNEIter = IntoIter<K, V>
type IntoNEIter = IntoIter<K, V>
NonEmptyIterator are we turning this into?Source§fn into_nonempty_iter(self) -> Self::IntoNEIter
fn into_nonempty_iter(self) -> Self::IntoNEIter
NonEmptyIterator from a value.Source§impl<K, V, S> PartialEq for NEMap<K, V, S>
impl<K, V, S> PartialEq for NEMap<K, V, S>
impl<K, V, S> Eq for NEMap<K, V, S>
Auto Trait Implementations§
impl<K, V, S> Freeze for NEMap<K, V, S>where
S: Freeze,
impl<K, V, S> RefUnwindSafe for NEMap<K, V, S>
impl<K, V, S> Send for NEMap<K, V, S>
impl<K, V, S> Sync for NEMap<K, V, S>
impl<K, V, S> Unpin for NEMap<K, V, S>
impl<K, V, S> UnwindSafe for NEMap<K, V, S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoIteratorExt for Twhere
T: IntoIterator,
impl<T> IntoIteratorExt for Twhere
T: IntoIterator,
Source§fn try_into_nonempty_iter(self) -> Option<<T as IntoIteratorExt>::IntoIter>
fn try_into_nonempty_iter(self) -> Option<<T as IntoIteratorExt>::IntoIter>
Converts self into a non-empty iterator or returns None if
the iterator is empty.
Source§type Item = <T as IntoIterator>::Item
type Item = <T as IntoIterator>::Item
Source§type IntoIter = NonEmptyIterAdapter<Peekable<<T as IntoIterator>::IntoIter>>
type IntoIter = NonEmptyIterAdapter<Peekable<<T as IntoIterator>::IntoIter>>
NonEmptyIterator are we turning this into?