Struct nonempty_collections::map::NEMap

source ·
pub struct NEMap<K, V, S = RandomState> {
    pub head_key: K,
    pub head_val: V,
    pub tail: HashMap<K, V, S>,
}
Expand description

A non-empty, growable HashMap.

use nonempty_collections::nem;

let m = nem!["elves" => 3000, "orcs" => 10000];
assert_eq!(2, m.len().get());

Fields§

§head_key: K

The key of the ever-present element of the non-empty HashMap.

§head_val: V

The value of the ever-present element of the non-empty HashMap.

§tail: HashMap<K, V, S>

The remaining key-value pairs, perhaps empty.

Implementations§

source§

impl<K, V, S> NEMap<K, V, S>

source

pub fn capacity(&self) -> usize

Returns the number of elements the map can hold without reallocating.

source

pub fn hasher(&self) -> &S

Returns a reference to the map’s BuildHasher.

source

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

An iterator visiting all elements in arbitrary order. The iterator element type is (&'a K, &'a V).

source

pub fn 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.

source

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);
source

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());
source

pub const fn is_empty(&self) -> bool

👎Deprecated since 0.1.0: A NEMap is never empty.

A NEMap is never empty.

source

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>
where K: Eq + Hash, S: BuildHasher,

source

pub fn contains_key<Q>(&self, k: &Q) -> bool
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

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

pub fn get<Q>(&self, k: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

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

pub fn get_key_value<Q>(&self, k: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

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

pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

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

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

pub fn new(k: K, v: V) -> NEMap<K, V>

Creates a new NEMap with a single element.

source

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.

source

pub fn with_capacity(capacity: usize, k: K, v: V) -> NEMap<K, V>

Creates a new NEMap with a single element and specified capacity.

source

pub fn with_capacity_and_hasher( capacity: usize, hasher: S, k: K, v: V, ) -> NEMap<K, V, S>

source

pub fn with_hasher(hasher: S, k: K, v: V) -> NEMap<K, V, S>

Trait Implementations§

source§

impl<K: Clone, V: Clone, S: Clone> Clone for NEMap<K, V, S>

source§

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

Returns a copy 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 NEMap<K, V, S>

source§

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

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

impl<'de, K, V, S> Deserialize<'de> for NEMap<K, V, S>
where K: Eq + Hash + Clone + Deserialize<'de>, V: Deserialize<'de>, S: Default + BuildHasher,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<K, V, S> From<NEMap<K, V, S>> for HashMap<K, V, S>
where K: Eq + Hash, S: BuildHasher,

source§

fn from(m: NEMap<K, V, S>) -> Self

use nonempty_collections::nem;
use std::collections::HashMap;

let m: HashMap<&str, usize> = nem!["population" => 1000].into();
assert!(m.contains_key("population"));
source§

impl<K, V, S> FromNonEmptyIterator<(K, V)> for NEMap<K, V, S>
where K: Eq + Hash, S: BuildHasher + Default,

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' => 1, 'b' => 2, 'c' => 3];
assert_eq!(m0, m1);
source§

fn from_nonempty_iter<I>(iter: I) -> Self
where I: IntoNonEmptyIterator<Item = (K, V)>,

Creates a value from a NonEmptyIterator.
source§

impl<K, V, S> IntoNonEmptyIterator for NEMap<K, V, S>

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = Chain<Once<(K, V)>, IntoIter<K, V>>

Which kind of NonEmptyIterator are we turning this into?
source§

fn into_nonempty_iter(self) -> Self::IntoIter

Creates a NonEmptyIterator from a value.
source§

impl<K, V, S> PartialEq for NEMap<K, V, S>
where K: Eq + Hash, V: Eq, S: BuildHasher,

source§

fn eq(&self, other: &Self) -> bool

This is an O(n) comparison of each key/value pair, one by one. Short-circuits if any comparison fails.

use nonempty_collections::*;

let m0 = nem!['a' => 1, 'b' => 2];
let m1 = nem!['b' => 2, 'a' => 1];
assert_eq!(m0, m1);
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<K, V, S> Serialize for NEMap<K, V, S>
where K: Eq + Hash + Clone + Serialize, V: Clone + Serialize, S: Clone + BuildHasher,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<K, V, S> TryFrom<HashMap<K, V, S>> for NEMap<K, V, S>
where K: Eq + Hash + Clone, S: BuildHasher,

§

type Error = Error

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

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

Performs the conversion.
source§

impl<K, V, S> Eq for NEMap<K, V, S>
where K: Eq + Hash, V: Eq, S: BuildHasher,

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<K, V, S> UnwindSafe for NEMap<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, dst: *mut T)

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

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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,

§

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

§

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

§

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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,