Struct enumap::set::EnumSet

source ·
pub struct EnumSet<const LENGTH: usize, E: Enum<LENGTH>>(/* private fields */);
Expand description

A set implemented as a EnumMap where the value is ().

Implementations§

source§

impl<const LENGTH: usize, E: Enum<LENGTH>> EnumSet<LENGTH, E>

source

pub fn new() -> Self

Creates an empty EnumSet.

With debug_assertions enabled, the constructor verifies the implementation of the Enum trait.

source

pub fn clear(&mut self)

Clears the set, removing all values.

§Examples
use enumap::{Enum, EnumSet};

let mut a = EnumSet::from([Fruit::Orange, Fruit::Banana]);

assert!(!a.is_empty());
a.clear();
assert!(a.is_empty());
source

pub fn contains(&self, value: E) -> bool

Returns true if the set contains a value.

§Examples
use enumap::{Enum, EnumSet};

let a = EnumSet::from([Fruit::Orange, Fruit::Banana]);

assert!(a.contains(Fruit::Orange));
assert!(a.contains(Fruit::Banana));
assert!(!a.contains(Fruit::Grape));
source

pub fn difference<'a>( &'a self, other: &'a EnumSet<LENGTH, E> ) -> Difference<'a, LENGTH, E>

Visits the values representing the difference, i.e., the values that are in self but not in other.

§Examples
use enumap::{Enum, EnumSet};

let a = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
let b = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Grape]);

// Can be seen as `a - b`.
for x in a.difference(&b) {
    println!("{x:?}"); // Print Apple
}

let diff: Vec<Fruit> = a.difference(&b).collect();
assert_eq!(diff, vec![Fruit::Apple]);

// Note that difference is not symmetric,
// and `b - a` means something else:
let diff: Vec<Fruit> = b.difference(&a).collect();
assert_eq!(diff, vec![Fruit::Grape]);
source

pub fn insert(&mut self, value: E) -> bool

Adds a value to the set.

Returns whether the value was newly inserted. That is:

If the set did not previously contain this value, true is returned. If the set already contained this value, false is returned.

§Examples
use enumap::EnumSet;

let mut set = EnumSet::new();

assert_eq!(set.insert(Fruit::Orange), true);
assert_eq!(set.insert(Fruit::Orange), false);
assert_eq!(set.len(), 1);
source

pub fn intersection<'a>( &'a self, other: &'a EnumSet<LENGTH, E> ) -> Intersection<'a, LENGTH, E>

Visits the values representing the intersection, i.e., the values that are both in self and other.

§Examples
use enumap::{Enum, EnumSet};

let a = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
let b = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Grape]);

// Print Orange, Banana in order.
for x in a.intersection(&b) {
    println!("{x:?}");
}

let intersection: Vec<Fruit> = a.intersection(&b).collect();
assert_eq!(intersection, vec![Fruit::Orange, Fruit::Banana]);
source

pub fn is_disjoint(&self, other: &EnumSet<LENGTH, E>) -> bool

Returns true if self has no elements in common with other. This is equivalent to checking for an empty intersection.

§Examples
use enumap::{Enum, EnumSet};

let a = EnumSet::from([Fruit::Orange, Fruit::Banana]);
let mut b = EnumSet::new();

assert_eq!(a.is_disjoint(&b), true);
b.insert(Fruit::Grape);
assert_eq!(a.is_disjoint(&b), true);
b.insert(Fruit::Orange);
assert_eq!(a.is_disjoint(&b), false);
source

pub fn is_empty(&self) -> bool

Returns true if the set contains no elements.

§Examples
use enumap::EnumSet;

let mut set = EnumSet::new();
assert!(set.is_empty());
set.insert(Fruit::Orange);
assert!(!set.is_empty());
source

pub fn is_subset(&self, other: &EnumSet<LENGTH, E>) -> bool

Returns true if the set is a subset of another, i.e., other contains at least all the values in self.

§Examples
use enumap::{Enum, EnumSet};

let sup = EnumSet::from([Fruit::Orange, Fruit::Banana]);
let mut set = EnumSet::new();

assert_eq!(set.is_subset(&sup), true);
set.insert(Fruit::Orange);
assert_eq!(set.is_subset(&sup), true);
set.insert(Fruit::Grape);
assert_eq!(set.is_subset(&sup), false);
source

pub fn is_superset(&self, other: &EnumSet<LENGTH, E>) -> bool

Returns true if the set is a superset of another, i.e., self contains at least all the values in other.

§Examples
use enumap::{Enum, EnumSet};

let sub = EnumSet::from([Fruit::Orange, Fruit::Banana]);
let mut set = EnumSet::new();

assert_eq!(set.is_superset(&sub), false);
set.insert(Fruit::Orange);
assert_eq!(set.is_superset(&sub), false);
set.insert(Fruit::Banana);
assert_eq!(set.is_superset(&sub), true);
set.insert(Fruit::Grape);
assert_eq!(set.is_superset(&sub), true);
source

pub fn iter(&self) -> Iter<'_, LENGTH, E>

An iterator visiting all elements in order. The iterator element type is E.

§Examples
use enumap::EnumSet;

let mut set = EnumSet::from([
    Fruit::Orange,
    Fruit::Grape,
]);

for value in set.iter() {
    println!("{value:?}");
}
source

pub fn len(&self) -> usize

Returns the number of elements in the set.

§Examples
use enumap::EnumSet;

let mut set = EnumSet::new();
assert_eq!(set.len(), 0);
set.insert(Fruit::Grape);
assert_eq!(set.len(), 1);
source

pub fn remove(&mut self, value: E) -> bool

Removes a value from the set. Returns whether the value was present in the set.

§Examples
use enumap::EnumSet;

let mut set = EnumSet::new();

set.insert(Fruit::Orange);
assert_eq!(set.remove(Fruit::Orange), true);
assert_eq!(set.remove(Fruit::Orange), false);
assert!(set.is_empty());
source

pub fn union<'a>( &'a self, other: &'a EnumSet<LENGTH, E> ) -> Union<'a, LENGTH, E>

Visits the values representing the union, i.e., all the values in self or other, without duplicates.

§Examples
use enumap::{Enum, EnumSet};

let a = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
let b = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Grape]);

// Print Orange, Banana, Grape, Apple in order.
for x in a.union(&b) {
    println!("{x:?}");
}

let union: Vec<Fruit> = a.union(&b).collect();
assert_eq!(union, vec![Fruit::Orange, Fruit::Banana, Fruit::Grape, Fruit::Apple]);
source

pub fn symmetric_difference<'a>( &'a self, other: &'a EnumSet<LENGTH, E> ) -> SymmetricDifference<'a, LENGTH, E>

Visits the values representing the symmetric difference, i.e., the values that are in self or in other but not in both.

§Examples
use enumap::{Enum, EnumSet};

let a = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
let b = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Grape]);

// Print Grape, Apple in order.
for x in a.symmetric_difference(&b) {
    println!("{x:?}");
}

let diff1: Vec<Fruit> = a.symmetric_difference(&b).collect();
let diff2: Vec<Fruit> = b.symmetric_difference(&a).collect();
assert_eq!(diff1, diff2);
assert_eq!(diff1, vec![Fruit::Grape, Fruit::Apple]);

Trait Implementations§

source§

impl<const LENGTH: usize, E: Enum<LENGTH>> BitAnd<&EnumSet<LENGTH, E>> for &EnumSet<LENGTH, E>

source§

fn bitand(self, rhs: &EnumSet<LENGTH, E>) -> Self::Output

Returns the intersection of self and rhs as a new EnumSet<LENGTH, E>.

§Examples
use enumap::{Enum, EnumSet};

let a = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
let b = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Grape]);

let set = &a & &b;
assert_eq!(set, EnumSet::from([Fruit::Orange, Fruit::Banana]));
§

type Output = EnumSet<LENGTH, E>

The resulting type after applying the & operator.
source§

impl<const LENGTH: usize, E: Enum<LENGTH>> BitAnd<E> for EnumSet<LENGTH, E>

source§

fn bitand(self, rhs: E) -> Self::Output

Returns the intersection of self and rhs as a new EnumSet<LENGTH, E>.

§Examples
use enumap::{Enum, EnumSet};

let set = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);

let set = set & Fruit::Apple;
assert_eq!(set, EnumSet::from([Fruit::Apple]));

let set = set & Fruit::Grape;
assert_eq!(set, EnumSet::new());
§

type Output = EnumSet<LENGTH, E>

The resulting type after applying the & operator.
source§

impl<const LENGTH: usize, E: Enum<LENGTH>> BitOr<&EnumSet<LENGTH, E>> for &EnumSet<LENGTH, E>

source§

fn bitor(self, rhs: &EnumSet<LENGTH, E>) -> Self::Output

Returns the union of self and rhs as a new EnumSet<LENGTH, E>.

§Examples
use enumap::{Enum, EnumSet};

let a = EnumSet::from([Fruit::Orange, Fruit::Apple]);
let b = EnumSet::from([Fruit::Orange, Fruit::Grape]);

let set = &a | &b;
assert_eq!(set, EnumSet::from([Fruit::Orange, Fruit::Grape, Fruit::Apple]));
§

type Output = EnumSet<LENGTH, E>

The resulting type after applying the | operator.
source§

impl<const LENGTH: usize, E: Enum<LENGTH>> BitOr<E> for EnumSet<LENGTH, E>

source§

fn bitor(self, rhs: E) -> Self::Output

Returns the union of self and rhs as a new EnumSet<LENGTH, E>.

§Examples
use enumap::{Enum, EnumSet};

let set = EnumSet::from([Fruit::Orange, Fruit::Apple]);

let set = set | Fruit::Banana | Fruit::Orange;
assert_eq!(set, EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]));
§

type Output = EnumSet<LENGTH, E>

The resulting type after applying the | operator.
source§

impl<const LENGTH: usize, E: Enum<LENGTH>> BitXor<&EnumSet<LENGTH, E>> for &EnumSet<LENGTH, E>

source§

fn bitxor(self, rhs: &EnumSet<LENGTH, E>) -> Self::Output

Returns the symmetric difference of self and rhs as a new EnumSet<LENGTH, E>.

§Examples
use enumap::{Enum, EnumSet};

let a = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
let b = EnumSet::from([Fruit::Orange, Fruit::Grape]);

let set = &a ^ &b;
assert_eq!(set, EnumSet::from([Fruit::Banana, Fruit::Grape, Fruit::Apple]));
§

type Output = EnumSet<LENGTH, E>

The resulting type after applying the ^ operator.
source§

impl<const LENGTH: usize, E: Enum<LENGTH>> BitXor<E> for EnumSet<LENGTH, E>

source§

fn bitxor(self, rhs: E) -> Self::Output

Returns the symmetric difference of self and rhs as a new EnumSet<LENGTH, E>.

§Examples
use enumap::{Enum, EnumSet};

let set = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Grape]);

let set = set ^ Fruit::Banana;
assert_eq!(set, EnumSet::from([Fruit::Orange, Fruit::Grape]));

let set = set ^ Fruit::Banana;
assert_eq!(set, EnumSet::from([Fruit::Banana]));
§

type Output = EnumSet<LENGTH, E>

The resulting type after applying the ^ operator.
source§

impl<const LENGTH: usize, E: Clone + Enum<LENGTH>> Clone for EnumSet<LENGTH, E>

source§

fn clone(&self) -> EnumSet<LENGTH, E>

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<const LENGTH: usize, E> Debug for EnumSet<LENGTH, E>
where E: Debug + Enum<LENGTH>,

source§

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

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

impl<const LENGTH: usize, E: Enum<LENGTH>> Default for EnumSet<LENGTH, E>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'de, const LENGTH: usize, E> Deserialize<'de> for EnumSet<LENGTH, E>
where E: Deserialize<'de> + Enum<LENGTH>,

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<'a, const LENGTH: usize, E: Enum<LENGTH>> Extend<&'a E> for EnumSet<LENGTH, E>

source§

fn extend<T: IntoIterator<Item = &'a E>>(&mut self, iter: T)

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<const LENGTH: usize, E: Enum<LENGTH>> Extend<E> for EnumSet<LENGTH, E>

source§

fn extend<T: IntoIterator<Item = E>>(&mut self, iter: T)

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<const LENGTH: usize, E: Enum<LENGTH>, V> From<&EnumMap<LENGTH, E, V>> for EnumSet<LENGTH, E>

source§

fn from(value: &EnumMap<LENGTH, E, V>) -> Self

Converts an &EnumMap into an EnumSet containing all of the map’s keys.

§Examples
use enumap::{EnumMap, EnumSet};

let map = EnumMap::from([(Fruit::Banana, 100), (Fruit::Grape, 200)]);
let set = EnumSet::from(&map);

assert_eq!(set.len(), map.len());
for fruit in set {
    assert!(map.contains_key(fruit));
}
source§

impl<const LENGTH: usize, E: Enum<LENGTH>, const N: usize> From<[E; N]> for EnumSet<LENGTH, E>

source§

fn from(value: [E; N]) -> Self

Converts to this type from the input type.
source§

impl<const LENGTH: usize, E: Enum<LENGTH>, V> From<EnumMap<LENGTH, E, V>> for EnumSet<LENGTH, E>

source§

fn from(value: EnumMap<LENGTH, E, V>) -> Self

Converts an EnumMap into an EnumSet containing all of the map’s keys.

§Examples
use enumap::{EnumMap, EnumSet};

let map = EnumMap::from([(Fruit::Banana, 100), (Fruit::Grape, 200)]);
let set = EnumSet::from(map);

assert!(set.contains(Fruit::Banana));
assert!(set.contains(Fruit::Grape));
assert!(!set.contains(Fruit::Orange));
source§

impl<'a, const LENGTH: usize, E: Enum<LENGTH>> FromIterator<&'a E> for EnumSet<LENGTH, E>

source§

fn from_iter<T: IntoIterator<Item = &'a E>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<const LENGTH: usize, E: Enum<LENGTH>> FromIterator<E> for EnumSet<LENGTH, E>

source§

fn from_iter<T: IntoIterator<Item = E>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<'a, const LENGTH: usize, E: Enum<LENGTH>> IntoIterator for &'a EnumSet<LENGTH, E>

§

type Item = E

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, LENGTH, E>

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<const LENGTH: usize, E: Enum<LENGTH>> IntoIterator for EnumSet<LENGTH, E>

§

type Item = E

The type of the elements being iterated over.
§

type IntoIter = IntoIter<LENGTH, E>

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<const LENGTH: usize, E: PartialEq + Enum<LENGTH>> PartialEq for EnumSet<LENGTH, E>

source§

fn eq(&self, other: &EnumSet<LENGTH, E>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

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

impl<const LENGTH: usize, E> Serialize for EnumSet<LENGTH, E>
where E: Serialize + Enum<LENGTH>,

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<const LENGTH: usize, E: Enum<LENGTH>> Sub<&EnumSet<LENGTH, E>> for &EnumSet<LENGTH, E>

source§

fn sub(self, rhs: &EnumSet<LENGTH, E>) -> Self::Output

Returns the difference of self and rhs as a new EnumSet<LENGTH, E>.

§Examples
use enumap::{Enum, EnumSet};

let a = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Apple]);
let b = EnumSet::from([Fruit::Orange, Fruit::Banana, Fruit::Grape]);

let set = &a - &b;
assert_eq!(set, EnumSet::from([Fruit::Apple]));
§

type Output = EnumSet<LENGTH, E>

The resulting type after applying the - operator.
source§

impl<const LENGTH: usize, E: Copy + Enum<LENGTH>> Copy for EnumSet<LENGTH, E>

source§

impl<const LENGTH: usize, E: Eq + Enum<LENGTH>> Eq for EnumSet<LENGTH, E>

source§

impl<const LENGTH: usize, E: Enum<LENGTH>> StructuralPartialEq for EnumSet<LENGTH, E>

Auto Trait Implementations§

§

impl<const LENGTH: usize, E> RefUnwindSafe for EnumSet<LENGTH, E>
where E: RefUnwindSafe,

§

impl<const LENGTH: usize, E> Send for EnumSet<LENGTH, E>
where E: Send,

§

impl<const LENGTH: usize, E> Sync for EnumSet<LENGTH, E>
where E: Sync,

§

impl<const LENGTH: usize, E> Unpin for EnumSet<LENGTH, E>
where E: Unpin,

§

impl<const LENGTH: usize, E> UnwindSafe for EnumSet<LENGTH, E>
where E: 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> 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, 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>,