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>
impl<const LENGTH: usize, E: Enum<LENGTH>> EnumSet<LENGTH, E>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty EnumSet
.
With debug_assertions
enabled, the constructor verifies the implementation
of the Enum
trait.
sourcepub fn clear(&mut self)
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());
sourcepub fn contains(&self, value: E) -> bool
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));
sourcepub fn difference<'a>(
&'a self,
other: &'a EnumSet<LENGTH, E>
) -> Difference<'a, LENGTH, E> ⓘ
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]);
sourcepub fn insert(&mut self, value: E) -> bool
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);
sourcepub fn intersection<'a>(
&'a self,
other: &'a EnumSet<LENGTH, E>
) -> Intersection<'a, LENGTH, E> ⓘ
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]);
sourcepub fn is_disjoint(&self, other: &EnumSet<LENGTH, E>) -> bool
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);
sourcepub fn is_empty(&self) -> bool
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());
sourcepub fn is_subset(&self, other: &EnumSet<LENGTH, E>) -> bool
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);
sourcepub fn is_superset(&self, other: &EnumSet<LENGTH, E>) -> bool
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);
sourcepub fn iter(&self) -> Iter<'_, LENGTH, E> ⓘ
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:?}");
}
sourcepub fn len(&self) -> usize
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);
sourcepub fn remove(&mut self, value: E) -> bool
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());
sourcepub fn union<'a>(
&'a self,
other: &'a EnumSet<LENGTH, E>
) -> Union<'a, LENGTH, E> ⓘ
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]);
sourcepub fn symmetric_difference<'a>(
&'a self,
other: &'a EnumSet<LENGTH, E>
) -> SymmetricDifference<'a, LENGTH, E> ⓘ
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>
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
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]));
source§impl<const LENGTH: usize, E: Enum<LENGTH>> BitAnd<E> for EnumSet<LENGTH, E>
impl<const LENGTH: usize, E: Enum<LENGTH>> BitAnd<E> for EnumSet<LENGTH, E>
source§fn bitand(self, rhs: E) -> Self::Output
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());
source§impl<const LENGTH: usize, E: Enum<LENGTH>> BitOr<&EnumSet<LENGTH, E>> for &EnumSet<LENGTH, E>
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
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]));
source§impl<const LENGTH: usize, E: Enum<LENGTH>> BitOr<E> for EnumSet<LENGTH, E>
impl<const LENGTH: usize, E: Enum<LENGTH>> BitOr<E> for EnumSet<LENGTH, E>
source§fn bitor(self, rhs: E) -> Self::Output
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]));
source§impl<const LENGTH: usize, E: Enum<LENGTH>> BitXor<&EnumSet<LENGTH, E>> for &EnumSet<LENGTH, E>
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
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]));
source§impl<const LENGTH: usize, E: Enum<LENGTH>> BitXor<E> for EnumSet<LENGTH, E>
impl<const LENGTH: usize, E: Enum<LENGTH>> BitXor<E> for EnumSet<LENGTH, E>
source§fn bitxor(self, rhs: E) -> Self::Output
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]));
source§impl<'de, const LENGTH: usize, E> Deserialize<'de> for EnumSet<LENGTH, E>where
E: Deserialize<'de> + Enum<LENGTH>,
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>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl<'a, const LENGTH: usize, E: Enum<LENGTH>> Extend<&'a E> for EnumSet<LENGTH, E>
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)
fn extend<T: IntoIterator<Item = &'a E>>(&mut self, iter: T)
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<const LENGTH: usize, E: Enum<LENGTH>> Extend<E> for EnumSet<LENGTH, E>
impl<const LENGTH: usize, E: Enum<LENGTH>> Extend<E> for EnumSet<LENGTH, E>
source§fn extend<T: IntoIterator<Item = E>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = E>>(&mut self, iter: T)
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<const LENGTH: usize, E: Enum<LENGTH>, V> From<&EnumMap<LENGTH, E, V>> for EnumSet<LENGTH, E>
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
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>
impl<const LENGTH: usize, E: Enum<LENGTH>, const N: usize> From<[E; N]> for EnumSet<LENGTH, E>
source§impl<const LENGTH: usize, E: Enum<LENGTH>, V> From<EnumMap<LENGTH, E, V>> for EnumSet<LENGTH, E>
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
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<const LENGTH: usize, E: Enum<LENGTH>> FromIterator<E> for EnumSet<LENGTH, E>
impl<const LENGTH: usize, E: Enum<LENGTH>> FromIterator<E> for EnumSet<LENGTH, E>
source§fn from_iter<T: IntoIterator<Item = E>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = E>>(iter: T) -> Self
source§impl<const LENGTH: usize, E: PartialEq + Enum<LENGTH>> PartialEq for EnumSet<LENGTH, E>
impl<const LENGTH: usize, E: PartialEq + Enum<LENGTH>> PartialEq for EnumSet<LENGTH, E>
source§impl<const LENGTH: usize, E: Enum<LENGTH>> Sub<&EnumSet<LENGTH, E>> for &EnumSet<LENGTH, E>
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
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]));