NESet

Struct NESet 

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

  • clear
  • drain
  • drain_filter
  • remove
  • retain
  • take

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>

Source

pub fn capacity(&self) -> NonZeroUsize

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

Source

pub fn hasher(&self) -> &S

Returns a reference to the set’s BuildHasher.

Source

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

Source

pub fn nonempty_iter(&self) -> Iter<'_, T>

An iterator visiting all elements in arbitrary order.

Source

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

pub const fn is_empty(&self) -> bool

👎Deprecated since 0.1.0: A NESet is never empty.

A NESet is never empty.

Source§

impl<T> NESet<T>
where T: Eq + Hash,

Source

pub fn new(value: T) -> Self

Creates a new NESet with a single element.

Source

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

Source

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

pub fn contains<Q>(&self, value: &Q) -> bool
where T: Borrow<Q>, Q: Eq + Hash + ?Sized,

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

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

pub fn get<Q>(&self, value: &Q) -> Option<&T>
where T: Borrow<Q>, Q: Eq + Hash,

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

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

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

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

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

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

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.

Source

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.

Source

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.

Source

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

pub fn with_capacity_and_hasher( capacity: NonZeroUsize, hasher: S, value: T, ) -> NESet<T, S>

Source

pub fn with_hasher(hasher: S, value: T) -> NESet<T, S>

Trait Implementations§

Source§

impl<T, S> AsMut<HashSet<T, S>> for NESet<T, S>

Source§

fn as_mut(&mut self) -> &mut HashSet<T, S>

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

impl<T, S> AsRef<HashSet<T, S>> for NESet<T, S>

Source§

fn as_ref(&self) -> &HashSet<T, S>

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

impl<T: Clone, S: Clone> Clone for NESet<T, S>

Source§

fn clone(&self) -> NESet<T, 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<T: Debug, S> Debug for NESet<T, S>

Source§

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

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

impl<'de, T, S> Deserialize<'de> for NESet<T, S>
where T: Eq + Hash + 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<T> Extend<T> for NESet<T>
where T: Eq + Hash,

Source§

fn extend<I: IntoIterator<Item = T>>(&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<T, S> From<NESet<T, S>> for HashSet<T, S>
where T: Eq + Hash, S: BuildHasher,

Source§

fn from(s: NESet<T, S>) -> Self

use std::collections::HashSet;

use nonempty_collections::nes;

let s: HashSet<_> = nes![1, 2, 3].into();
let mut v: Vec<_> = s.into_iter().collect();
v.sort();
assert_eq!(vec![1, 2, 3], v);
Source§

impl<T, S> FromNonEmptyIterator<T> for NESet<T, S>
where T: Eq + Hash, S: BuildHasher + Default,

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) -> Self
where 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>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

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<T, S> IntoIterator for NESet<T, S>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

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, T, S> IntoNonEmptyIterator for &'a NESet<T, S>

Source§

type IntoNEIter = Iter<'a, T>

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<T, S> IntoNonEmptyIterator for NESet<T, S>

Source§

type IntoNEIter = IntoIter<T>

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<T, S> PartialEq for NESet<T, S>
where T: Eq + Hash, S: BuildHasher,

Source§

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

use nonempty_collections::nes;

let s0 = nes![1, 2, 3];
let s1 = nes![1, 2, 3];
let s2 = nes![1, 2];
let s3 = nes![1, 2, 3, 4];

assert!(s0 == s1);
assert!(s0 != s2);
assert!(s0 != s3);
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<T, S> Serialize for NESet<T, S>
where T: Eq + Hash + 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<T> Singleton for NESet<T>
where T: Eq + Hash,

Source§

fn singleton(item: Self::Item) -> Self

use nonempty_collections::{NESet, Singleton, nes};

let s = NESet::singleton(1);
assert_eq!(nes![1], s);
Source§

type Item = T

Source§

impl<T, S> TryFrom<HashSet<T, S>> for NESet<T, S>
where T: Eq + Hash, S: BuildHasher + Default,

Source§

type Error = Error

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

fn try_from(set: HashSet<T, S>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T, S> Eq for NESet<T, S>
where T: Eq + Hash, S: BuildHasher,

Auto Trait Implementations§

§

impl<T, S> Freeze for NESet<T, S>
where S: Freeze,

§

impl<T, S> RefUnwindSafe for NESet<T, S>

§

impl<T, S> Send for NESet<T, S>
where S: Send, T: Send,

§

impl<T, S> Sync for NESet<T, S>
where S: Sync, T: Sync,

§

impl<T, S> Unpin for NESet<T, S>
where S: Unpin, T: Unpin,

§

impl<T, S> UnwindSafe for NESet<T, S>
where S: UnwindSafe, T: 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<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.
Source§

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