pub struct NESet<T, S = RandomState> { /* private fields */ }Expand description
A non-empty, growable HashSet.
§Construction and Access
The nes macro is the simplest way to construct an NESet:
use nonempty_collections::*;
let s = nes![1, 1, 2, 2, 3, 3, 4, 4];
let mut v: NEVec<_> = s.nonempty_iter().collect();
v.sort();
assert_eq!(nev![&1, &2, &3, &4], v);use nonempty_collections::nes;
let s = nes!["Fëanor", "Fingolfin", "Finarfin"];
assert!(s.contains(&"Fëanor"));§Conversion
If you have a HashSet but want an NESet, try NESet::try_from_set.
Naturally, this might not succeed.
If you have an NESet but want a HashSet, try their corresponding
From instance. This will always succeed.
use std::collections::HashSet;
use nonempty_collections::nes;
let n0 = nes![1, 2, 3];
let s0 = HashSet::from(n0);
// Or just use `Into`.
let n1 = nes![1, 2, 3];
let s1: HashSet<_> = n1.into();§API Differences with HashSet
Note that the following methods aren’t implemented for NESet:
cleardraindrain_filterremoveretaintake
As these methods are all “mutate-in-place” style and are difficult to reconcile with the non-emptiness guarantee.
Implementations§
Source§impl<T, S> NESet<T, S>
impl<T, S> NESet<T, S>
Sourcepub fn capacity(&self) -> NonZeroUsize
pub fn capacity(&self) -> NonZeroUsize
Returns the number of elements the set can hold without reallocating.
Sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Returns a regular iterator over the values in this non-empty set.
For a NonEmptyIterator see Self::nonempty_iter().
Sourcepub fn nonempty_iter(&self) -> Iter<'_, T>
pub fn nonempty_iter(&self) -> Iter<'_, T>
An iterator visiting all elements in arbitrary order.
Sourcepub fn len(&self) -> NonZeroUsize
pub fn len(&self) -> NonZeroUsize
Returns the number of elements in the set. Always 1 or more.
use nonempty_collections::nes;
let s = nes![1, 2, 3];
assert_eq!(3, s.len().get());Source§impl<T> NESet<T>
impl<T> NESet<T>
Sourcepub fn with_capacity(capacity: NonZeroUsize, value: T) -> NESet<T>
pub fn with_capacity(capacity: NonZeroUsize, value: T) -> NESet<T>
Creates a new NESet with a single element and specified capacity.
use std::hash::RandomState;
use std::num::NonZeroUsize;
use nonempty_collections::*;
let set = NESet::with_capacity(NonZeroUsize::MIN, "hello");
assert_eq!(nes! {"hello"}, set);
assert!(set.capacity().get() >= 1);Source§impl<T, S> NESet<T, S>
impl<T, S> NESet<T, S>
Sourcepub fn try_from_set(set: HashSet<T, S>) -> Option<NESet<T, S>>
pub fn try_from_set(set: HashSet<T, S>) -> Option<NESet<T, S>>
Attempt a conversion from a HashSet, consuming the given HashSet.
Will return None if the HashSet is empty.
use std::collections::HashSet;
use nonempty_collections::nes;
use nonempty_collections::NESet;
let mut s = HashSet::new();
s.extend([1, 2, 3]);
let n = NESet::try_from_set(s);
assert_eq!(Some(nes![1, 2, 3]), n);
let s: HashSet<()> = HashSet::new();
assert_eq!(None, NESet::try_from_set(s));Sourcepub fn contains<Q>(&self, value: &Q) -> bool
pub fn contains<Q>(&self, value: &Q) -> bool
Returns true if the set contains a value.
use nonempty_collections::nes;
let s = nes![1, 2, 3];
assert!(s.contains(&3));
assert!(!s.contains(&10));Sourcepub fn difference<'a>(&'a self, other: &'a NESet<T, S>) -> Difference<'a, T, S>
pub fn difference<'a>(&'a self, other: &'a NESet<T, S>) -> Difference<'a, T, S>
Visits the values representing the difference, i.e., the values that are
in self but not in other.
use nonempty_collections::nes;
let s0 = nes![1, 2, 3];
let s1 = nes![3, 4, 5];
let mut v: Vec<_> = s0.difference(&s1).collect();
v.sort();
assert_eq!(vec![&1, &2], v);Sourcepub fn get<Q>(&self, value: &Q) -> Option<&T>
pub fn get<Q>(&self, value: &Q) -> Option<&T>
Returns a reference to the value in the set, if any, that is equal to the given value.
The value may be any borrowed form of the set’s value type, but Hash
and Eq on the borrowed form must match those for the value type.
use nonempty_collections::nes;
let s = nes![1, 2, 3];
assert_eq!(Some(&3), s.get(&3));
assert_eq!(None, s.get(&10));Sourcepub fn insert(&mut self, value: T) -> bool
pub fn insert(&mut self, value: T) -> bool
Adds a value to the set.
If the set did not have this value present, true is returned.
If the set did have this value present, false is returned.
use nonempty_collections::nes;
let mut s = nes![1, 2, 3];
assert_eq!(false, s.insert(2));
assert_eq!(true, s.insert(4));Sourcepub fn intersection<'a>(
&'a self,
other: &'a NESet<T, S>,
) -> Intersection<'a, T, S>
pub fn intersection<'a>( &'a self, other: &'a NESet<T, S>, ) -> Intersection<'a, T, S>
Visits the values representing the interesection, i.e., the values that
are both in self and other.
use nonempty_collections::nes;
let s0 = nes![1, 2, 3];
let s1 = nes![3, 4, 5];
let mut v: Vec<_> = s0.intersection(&s1).collect();
v.sort();
assert_eq!(vec![&3], v);Sourcepub fn is_disjoint(&self, other: &NESet<T, S>) -> bool
pub fn is_disjoint(&self, other: &NESet<T, S>) -> bool
Returns true if self has no elements in common with other.
This is equivalent to checking for an empty intersection.
use nonempty_collections::nes;
let s0 = nes![1, 2, 3];
let s1 = nes![4, 5, 6];
assert!(s0.is_disjoint(&s1));Sourcepub fn is_subset(&self, other: &NESet<T, S>) -> bool
pub fn is_subset(&self, other: &NESet<T, S>) -> bool
Returns true if the set is a subset of another, i.e., other contains
at least all the values in self.
use nonempty_collections::nes;
let sub = nes![1, 2, 3];
let sup = nes![1, 2, 3, 4];
assert!(sub.is_subset(&sup));
assert!(!sup.is_subset(&sub));Sourcepub fn is_superset(&self, other: &NESet<T, S>) -> bool
pub fn is_superset(&self, other: &NESet<T, S>) -> bool
Returns true if the set is a superset of another, i.e., self
contains at least all the values in other.
use nonempty_collections::nes;
let sub = nes![1, 2, 3];
let sup = nes![1, 2, 3, 4];
assert!(sup.is_superset(&sub));
assert!(!sub.is_superset(&sup));Sourcepub fn replace(&mut self, value: T) -> Option<T>
pub fn replace(&mut self, value: T) -> Option<T>
Adds a value to the set, replacing the existing value, if any, that is equal to the given one. Returns the replaced value.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements to be inserted
in the NESet. The collection may reserve more space to avoid frequent
reallocations.
§Panics
Panics if the new allocation size overflows usize.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the set 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 union<'a>(&'a self, other: &'a NESet<T, S>) -> Union<'a, T, S>
pub fn union<'a>(&'a self, other: &'a NESet<T, S>) -> Union<'a, T, S>
Visits the values representing the union, i.e., all the values in self
or other, without duplicates.
Note that a Union is always non-empty.
use nonempty_collections::*;
let s0 = nes![1, 2, 3];
let s1 = nes![3, 4, 5];
let mut v: NEVec<_> = s0.union(&s1).collect();
v.sort();
assert_eq!(nev![&1, &2, &3, &4, &5], v);Sourcepub fn with_capacity_and_hasher(
capacity: NonZeroUsize,
hasher: S,
value: T,
) -> NESet<T, S>
pub fn with_capacity_and_hasher( capacity: NonZeroUsize, hasher: S, value: T, ) -> NESet<T, S>
Sourcepub fn with_hasher(hasher: S, value: T) -> NESet<T, S>
pub fn with_hasher(hasher: S, value: T) -> NESet<T, S>
See HashSet::with_hasher.
Trait Implementations§
Source§impl<'de, T, S> Deserialize<'de> for NESet<T, S>
impl<'de, T, S> Deserialize<'de> for NESet<T, 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<T> Extend<T> for NESet<T>
impl<T> Extend<T> for NESet<T>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&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<T, S> FromNonEmptyIterator<T> for NESet<T, S>
use nonempty_collections::*;
let s0 = nes![1, 2, 3];
let s1: NESet<_> = s0.nonempty_iter().cloned().collect();
assert_eq!(s0, s1);
impl<T, S> FromNonEmptyIterator<T> for NESet<T, S>
use nonempty_collections::*;
let s0 = nes![1, 2, 3];
let s1: NESet<_> = s0.nonempty_iter().cloned().collect();
assert_eq!(s0, s1);Source§fn from_nonempty_iter<I>(iter: I) -> Selfwhere
I: IntoNonEmptyIterator<Item = T>,
fn from_nonempty_iter<I>(iter: I) -> Selfwhere
I: IntoNonEmptyIterator<Item = T>,
use nonempty_collections::*;
let v = nev![1, 1, 2, 3, 2];
let s = NESet::from_nonempty_iter(v);
assert_eq!(nes![1, 2, 3], s);Source§impl<'a, T, S> IntoIterator for &'a NESet<T, S>
impl<'a, T, S> IntoIterator for &'a NESet<T, S>
Source§impl<T, S> IntoIterator for NESet<T, S>
impl<T, S> IntoIterator for NESet<T, S>
Source§impl<'a, T, S> IntoNonEmptyIterator for &'a NESet<T, S>
impl<'a, T, S> IntoNonEmptyIterator for &'a NESet<T, S>
Source§type IntoNEIter = Iter<'a, T>
type IntoNEIter = Iter<'a, T>
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<T, S> IntoNonEmptyIterator for NESet<T, S>
impl<T, S> IntoNonEmptyIterator for NESet<T, S>
Source§type IntoNEIter = IntoIter<T>
type IntoNEIter = IntoIter<T>
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<T, S> PartialEq for NESet<T, S>
impl<T, S> PartialEq for NESet<T, S>
impl<T, S> Eq for NESet<T, S>
Auto Trait Implementations§
impl<T, S> Freeze for NESet<T, S>where
S: Freeze,
impl<T, S> RefUnwindSafe for NESet<T, S>where
S: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, S> Send for NESet<T, S>
impl<T, S> Sync for NESet<T, S>
impl<T, S> Unpin for NESet<T, S>
impl<T, S> UnwindSafe for NESet<T, S>where
S: UnwindSafe,
T: UnwindSafe,
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?