Skip to main content

NEBTreeMap

Struct NEBTreeMap 

Source
pub struct NEBTreeMap<K, V> { /* private fields */ }
Expand description

A non-empty, growable BTreeMap.

use nonempty_collections::nebtm;

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

Implementations§

Source§

impl<K, V> NEBTreeMap<K, V>
where K: Ord,

Source

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

Creates a new NEBTreeMap with a single element.

Source§

impl<K, V> NEBTreeMap<K, V>

Source

pub fn try_from_map(map: BTreeMap<K, V>) -> Option<Self>

Attempt a conversion from BTreeMap, consuming the given BTreeMap. Will return None if the BTreeMap is empty.

use std::collections::*;

use nonempty_collections::*;

let mut map = BTreeMap::new();
map.extend([("a", 1), ("b", 2)]);
assert_eq!(Some(nebtm! {"a" => 1, "b" => 2}), NEBTreeMap::try_from_map(map));
let map: BTreeMap<(), ()> = BTreeMap::new();
assert_eq!(None, NEBTreeMap::try_from_map(map));
Source

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().

Source

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().

Source

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).

Source

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.

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 = nebtm!["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::nebtm;

let m = nebtm!["a" => 1, "b" => 2];
assert_eq!(2, m.len().get());
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 = nebtm!["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> NEBTreeMap<K, V>
where K: Ord,

Source

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

Returns true if the map contains a value.

use nonempty_collections::nebtm;

let m = nebtm!["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: Ord + ?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::nebtm;

let m = nebtm!["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: Ord + ?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::nebtm;

let m = nebtm!["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: Ord + ?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::nebtm;

let mut m = nebtm!["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 BTreeMap::insert for more.

use nonempty_collections::nebtm;

let mut m = nebtm!["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"));

Trait Implementations§

Source§

impl<K, V> AsMut<BTreeMap<K, V>> for NEBTreeMap<K, V>

Source§

fn as_mut(&mut self) -> &mut BTreeMap<K, V>

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<K, V> AsRef<BTreeMap<K, V>> for NEBTreeMap<K, V>

Source§

fn as_ref(&self) -> &BTreeMap<K, V>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<K: Clone, V: Clone> Clone for NEBTreeMap<K, V>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug> Debug for NEBTreeMap<K, V>

Source§

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

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

impl<'de, K, V> Deserialize<'de> for NEBTreeMap<K, V>
where K: Ord + Clone + Deserialize<'de>, V: Deserialize<'de>,

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> Eq for NEBTreeMap<K, V>
where K: Ord, V: Eq,

Source§

impl<K, V> Extend<(K, V)> for NEBTreeMap<K, V>
where K: Ord,

Source§

fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K, V> From<NEBTreeMap<K, V>> for BTreeMap<K, V>
where K: Ord,

Source§

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

use nonempty_collections::nebtm;
use std::collections::BTreeMap;

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

impl<K, V> FromNonEmptyIterator<(K, V)> for NEBTreeMap<K, V>
where K: Ord,

use nonempty_collections::*;

let v = nev![('a', 1), ('b', 2), ('c', 3), ('a', 4)];
let m0: NEBTreeMap<_, _> = v.into_nonempty_iter().collect();
let m1: NEBTreeMap<_, _> = nebtm!['a' => 4, '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> IntoIterator for NEBTreeMap<K, V>

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> IntoIterator for &'a NEBTreeMap<K, V>

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> IntoIterator for &'a mut NEBTreeMap<K, V>

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> IntoNonEmptyIterator for NEBTreeMap<K, V>

Source§

type IntoNEIter = IntoIter<K, V>

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

fn into_nonempty_iter(self) -> Self::IntoNEIter

Creates a NonEmptyIterator from a value.
Source§

impl<'a, K, V> IntoNonEmptyIterator for &'a NEBTreeMap<K, V>

Source§

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

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

fn into_nonempty_iter(self) -> Self::IntoNEIter

Creates a NonEmptyIterator from a value.
Source§

impl<K, V> PartialEq for NEBTreeMap<K, V>
where K: Ord, V: PartialEq,

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 = nebtm!['a' => 1, 'b' => 2];
let m1 = nebtm!['b' => 2, 'a' => 1];
assert_eq!(m0, m1);
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<K, V> Serialize for NEBTreeMap<K, V>
where K: Ord + Clone + Serialize, V: Clone + Serialize,

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> Singleton for NEBTreeMap<K, V>
where K: Ord,

Source§

fn singleton((k, v): Self::Item) -> Self

use nonempty_collections::{NEBTreeMap, Singleton, nebtm};

let m = NEBTreeMap::singleton(('a', 1));
assert_eq!(nebtm!['a' => 1], m);
Source§

type Item = (K, V)

Source§

impl<K, V> TryFrom<BTreeMap<K, V>> for NEBTreeMap<K, V>
where K: Ord,

Source§

type Error = Error

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

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

Performs the conversion.

Auto Trait Implementations§

§

impl<K, V> Freeze for NEBTreeMap<K, V>

§

impl<K, V> RefUnwindSafe for NEBTreeMap<K, V>

§

impl<K, V> Send for NEBTreeMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for NEBTreeMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for NEBTreeMap<K, V>

§

impl<K, V> UnsafeUnpin for NEBTreeMap<K, V>

§

impl<K, V> UnwindSafe for NEBTreeMap<K, V>

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> IntoIteratorExt for T
where T: IntoIterator,

Source§

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

The type of the elements being iterated over.
Source§

type IntoIter = NonEmptyIterAdapter<Peekable<<T as IntoIterator>::IntoIter>>

Which kind of NonEmptyIterator are we turning this into?
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.