pub struct BigEnumSet<T: BigEnumSetType> { /* private fields */ }
Expand description

An efficient set type for enums.

It is implemented using a bitset stored as [usize; N], where N is the smallest number that such that the array can fit all the bits of the underlying enum. An enum with discriminant n is stored in the n / WORD_SIZE word at the n % WORD_SIZE least significant bit (corresponding with as bit mask of 1 << (n % WORD_SIZE). WORD_SIZE is mem::size_of::<usize>().

Serialization

When the serde feature is enabled, BigEnumSets can be serialized and deserialized using the serde crate. The exact serialization format can be controlled with additional attributes on the enum type. These attributes are valid regardless of whether the serde feature is enabled.

By default BigEnumSets are serialized as [u8; N], where N is smallest such that the array can fit all bits that are part of the underlying enum. An enum with discriminant n is serialized as n % 8th least significant bit in the n / 8 byte. You can add a #[big_enum_set(serialize_bytes = N)] attribute to your enum to control the number of bytes in the serialization. This can be important for avoiding unintentional breaking changes when BigEnumSets are serialized with formats like bincode.

By default, unknown bits are ignored and silently removed from the bitset. To override this behavior, you can add a #[big_enum_set(serialize_deny_unknown)] attribute. This will cause deserialization to fail if an invalid bit is set.

In addition, the #[big_enum_set(serialize_as_list)] attribute causes the BigEnumSet to be instead serialized as a list of enum variants. This requires your enum type implement Serialize and Deserialize. Note that this is a breaking change.

Implementations§

source§

impl<T: BigEnumSetType> BigEnumSet<T>

source

pub const EMPTY: BigEnumSet<T> = _

Empty set.

source

pub fn new() -> Self

Creates an empty BigEnumSet.

source

pub fn only(t: T) -> Self

Returns a BigEnumSet containing a single element.

source

pub fn empty() -> Self

Creates an empty BigEnumSet.

This is an alias for BigEnumSet::new.

source

pub fn all() -> Self

Returns a BigEnumSet containing all valid variants of the enum.

source

pub fn bit_width() -> u32

Total number of bits used by this type. Note that the actual amount of space used is rounded up to the next highest usize.

This is the same as BigEnumSet::variant_count except in enums with “sparse” variants. (e.g. enum Foo { A = 10, B = 20 })

source

pub fn variant_count() -> u32

The number of valid variants this type may contain.

This is the same as BigEnumSet::bit_width except in enums with “sparse” variants. (e.g. enum Foo { A = 10, B = 20 })

source

pub fn as_bits(&self) -> &[usize]

Returns the raw bits of this set.

source

pub fn try_from_bits(bits: &[usize]) -> Option<Self>

Constructs a BigEnumSet from raw bits.

Returns None if there are any invalid bits set in bits. The size of bits need not match the underlying representation.

source

pub fn from_bits_truncated(bits: &[usize]) -> Self

Constructs a BigEnumSet from raw bits, ignoring any unknown variants.

The size of bits need not match the underlying representation.

source

pub fn len(&self) -> usize

Returns the number of elements in this set.

source

pub fn is_empty(&self) -> bool

Returns true if the set contains no elements.

source

pub fn clear(&mut self)

Removes all elements from the set.

source

pub fn is_disjoint<O: Borrow<Self>>(&self, other: O) -> bool

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

source

pub fn is_superset<O: Borrow<Self>>(&self, other: O) -> bool

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

source

pub fn is_subset<O: Borrow<Self>>(&self, other: O) -> bool

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

source

pub fn union<O: Borrow<Self>>(&self, other: O) -> Self

Returns a set containing all elements present in either set.

source

pub fn intersection<O: Borrow<Self>>(&self, other: O) -> Self

Returns a set containing all elements present in both sets.

source

pub fn difference<O: Borrow<Self>>(&self, other: O) -> Self

Returns a set containing all elements present in self but not in other.

source

pub fn symmetrical_difference<O: Borrow<Self>>(&self, other: O) -> Self

Returns a set containing all elements present in either self or other, but is not present in both.

source

pub fn complement(&self) -> Self

Returns a set containing all enum variants not present in this set.

source

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

Checks whether this set contains value.

source

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

Adds a value to this set.

If the set did not have this value present, true is returned.

If the set did have this value present, false is returned.

source

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

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

source

pub fn insert_all<O: Borrow<Self>>(&mut self, other: O)

Adds all elements in another set to this one.

source

pub fn remove_all<O: Borrow<Self>>(&mut self, other: O)

Removes all values in another set from this one.

source

pub fn iter(&self) -> EnumSetIter<&BigEnumSet<T>, T>

Creates an iterator over the values in this set.

Trait Implementations§

source§

impl<T: BigEnumSetType> BitAnd<&BigEnumSet<T>> for &BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the & operator.
source§

fn bitand(self, other: &BigEnumSet<T>) -> Self::Output

Performs the & operation. Read more
source§

impl<T: BigEnumSetType> BitAnd<&BigEnumSet<T>> for BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the & operator.
source§

fn bitand(self, other: &BigEnumSet<T>) -> Self::Output

Performs the & operation. Read more
source§

impl<T: BigEnumSetType> BitAnd<BigEnumSet<T>> for &BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the & operator.
source§

fn bitand(self, other: BigEnumSet<T>) -> Self::Output

Performs the & operation. Read more
source§

impl<T: BigEnumSetType> BitAnd<BigEnumSet<T>> for BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the & operator.
source§

fn bitand(self, other: BigEnumSet<T>) -> Self::Output

Performs the & operation. Read more
source§

impl<T: BigEnumSetType> BitAnd<T> for &BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the & operator.
source§

fn bitand(self, value: T) -> Self::Output

Performs the & operation. Read more
source§

impl<T: BigEnumSetType> BitAnd<T> for BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the & operator.
source§

fn bitand(self, value: T) -> Self::Output

Performs the & operation. Read more
source§

impl<T: BigEnumSetType> BitAndAssign<&BigEnumSet<T>> for BigEnumSet<T>

source§

fn bitand_assign(&mut self, other: &BigEnumSet<T>)

Performs the &= operation. Read more
source§

impl<T: BigEnumSetType> BitAndAssign<BigEnumSet<T>> for BigEnumSet<T>

source§

fn bitand_assign(&mut self, other: BigEnumSet<T>)

Performs the &= operation. Read more
source§

impl<T: BigEnumSetType> BitAndAssign<T> for BigEnumSet<T>

source§

fn bitand_assign(&mut self, value: T)

Performs the &= operation. Read more
source§

impl<T: BigEnumSetType> BitOr<&BigEnumSet<T>> for &BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the | operator.
source§

fn bitor(self, other: &BigEnumSet<T>) -> Self::Output

Performs the | operation. Read more
source§

impl<T: BigEnumSetType> BitOr<&BigEnumSet<T>> for BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the | operator.
source§

fn bitor(self, other: &BigEnumSet<T>) -> Self::Output

Performs the | operation. Read more
source§

impl<T: BigEnumSetType> BitOr<BigEnumSet<T>> for &BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the | operator.
source§

fn bitor(self, other: BigEnumSet<T>) -> Self::Output

Performs the | operation. Read more
source§

impl<T: BigEnumSetType> BitOr<BigEnumSet<T>> for BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the | operator.
source§

fn bitor(self, other: BigEnumSet<T>) -> Self::Output

Performs the | operation. Read more
source§

impl<T: BigEnumSetType> BitOr<T> for &BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the | operator.
source§

fn bitor(self, value: T) -> Self::Output

Performs the | operation. Read more
source§

impl<T: BigEnumSetType> BitOr<T> for BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the | operator.
source§

fn bitor(self, value: T) -> Self::Output

Performs the | operation. Read more
source§

impl<T: BigEnumSetType> BitOrAssign<&BigEnumSet<T>> for BigEnumSet<T>

source§

fn bitor_assign(&mut self, other: &BigEnumSet<T>)

Performs the |= operation. Read more
source§

impl<T: BigEnumSetType> BitOrAssign<BigEnumSet<T>> for BigEnumSet<T>

source§

fn bitor_assign(&mut self, other: BigEnumSet<T>)

Performs the |= operation. Read more
source§

impl<T: BigEnumSetType> BitOrAssign<T> for BigEnumSet<T>

source§

fn bitor_assign(&mut self, value: T)

Performs the |= operation. Read more
source§

impl<T: BigEnumSetType> BitXor<&BigEnumSet<T>> for &BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the ^ operator.
source§

fn bitxor(self, other: &BigEnumSet<T>) -> Self::Output

Performs the ^ operation. Read more
source§

impl<T: BigEnumSetType> BitXor<&BigEnumSet<T>> for BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the ^ operator.
source§

fn bitxor(self, other: &BigEnumSet<T>) -> Self::Output

Performs the ^ operation. Read more
source§

impl<T: BigEnumSetType> BitXor<BigEnumSet<T>> for &BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the ^ operator.
source§

fn bitxor(self, other: BigEnumSet<T>) -> Self::Output

Performs the ^ operation. Read more
source§

impl<T: BigEnumSetType> BitXor<BigEnumSet<T>> for BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the ^ operator.
source§

fn bitxor(self, other: BigEnumSet<T>) -> Self::Output

Performs the ^ operation. Read more
source§

impl<T: BigEnumSetType> BitXor<T> for &BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the ^ operator.
source§

fn bitxor(self, value: T) -> Self::Output

Performs the ^ operation. Read more
source§

impl<T: BigEnumSetType> BitXor<T> for BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the ^ operator.
source§

fn bitxor(self, value: T) -> Self::Output

Performs the ^ operation. Read more
source§

impl<T: BigEnumSetType> BitXorAssign<&BigEnumSet<T>> for BigEnumSet<T>

source§

fn bitxor_assign(&mut self, other: &BigEnumSet<T>)

Performs the ^= operation. Read more
source§

impl<T: BigEnumSetType> BitXorAssign<BigEnumSet<T>> for BigEnumSet<T>

source§

fn bitxor_assign(&mut self, other: BigEnumSet<T>)

Performs the ^= operation. Read more
source§

impl<T: BigEnumSetType> BitXorAssign<T> for BigEnumSet<T>

source§

fn bitxor_assign(&mut self, value: T)

Performs the ^= operation. Read more
source§

impl<T: Clone + BigEnumSetType> Clone for BigEnumSet<T>where T::Repr: Clone,

source§

fn clone(&self) -> BigEnumSet<T>

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<T: BigEnumSetType + Debug> Debug for BigEnumSet<T>

source§

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

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

impl<T: BigEnumSetType> Default for BigEnumSet<T>

source§

fn default() -> Self

Returns an empty set.

source§

impl<T: BigEnumSetType> Extend<T> for BigEnumSet<T>

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: BigEnumSetType> From<T> for BigEnumSet<T>

source§

fn from(t: T) -> Self

Converts to this type from the input type.
source§

impl<T: BigEnumSetType> FromIterator<T> for BigEnumSet<T>

source§

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

Creates a value from an iterator. Read more
source§

impl<T: BigEnumSetType> Hash for BigEnumSet<T>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<T: BigEnumSetType> IntoIterator for BigEnumSet<T>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = EnumSetIter<BigEnumSet<T>, 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: BigEnumSetType> Not for &BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the ! operator.
source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
source§

impl<T: BigEnumSetType> Not for BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the ! operator.
source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
source§

impl<T: BigEnumSetType> Ord for BigEnumSet<T>

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl<T: PartialEq + BigEnumSetType> PartialEq<BigEnumSet<T>> for BigEnumSet<T>where T::Repr: PartialEq,

source§

fn eq(&self, other: &BigEnumSet<T>) -> 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<T: BigEnumSetType> PartialEq<T> for BigEnumSet<T>

source§

fn eq(&self, other: &T) -> 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<T: BigEnumSetType> PartialOrd<BigEnumSet<T>> for BigEnumSet<T>

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: BigEnumSetType> Sub<&BigEnumSet<T>> for &BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the - operator.
source§

fn sub(self, other: &BigEnumSet<T>) -> Self::Output

Performs the - operation. Read more
source§

impl<T: BigEnumSetType> Sub<&BigEnumSet<T>> for BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the - operator.
source§

fn sub(self, other: &BigEnumSet<T>) -> Self::Output

Performs the - operation. Read more
source§

impl<T: BigEnumSetType> Sub<BigEnumSet<T>> for &BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the - operator.
source§

fn sub(self, other: BigEnumSet<T>) -> Self::Output

Performs the - operation. Read more
source§

impl<T: BigEnumSetType> Sub<BigEnumSet<T>> for BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the - operator.
source§

fn sub(self, other: BigEnumSet<T>) -> Self::Output

Performs the - operation. Read more
source§

impl<T: BigEnumSetType> Sub<T> for &BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the - operator.
source§

fn sub(self, value: T) -> Self::Output

Performs the - operation. Read more
source§

impl<T: BigEnumSetType> Sub<T> for BigEnumSet<T>

§

type Output = BigEnumSet<T>

The resulting type after applying the - operator.
source§

fn sub(self, value: T) -> Self::Output

Performs the - operation. Read more
source§

impl<T: BigEnumSetType> SubAssign<&BigEnumSet<T>> for BigEnumSet<T>

source§

fn sub_assign(&mut self, other: &BigEnumSet<T>)

Performs the -= operation. Read more
source§

impl<T: BigEnumSetType> SubAssign<BigEnumSet<T>> for BigEnumSet<T>

source§

fn sub_assign(&mut self, other: BigEnumSet<T>)

Performs the -= operation. Read more
source§

impl<T: BigEnumSetType> SubAssign<T> for BigEnumSet<T>

source§

fn sub_assign(&mut self, value: T)

Performs the -= operation. Read more
source§

impl<'a, T: 'a + BigEnumSetType> Sum<&'a BigEnumSet<T>> for BigEnumSet<T>

source§

fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self

Method which takes an iterator and generates Self from the elements by “summing up” the items.
source§

impl<'a, T: 'a + BigEnumSetType> Sum<&'a T> for BigEnumSet<T>

source§

fn sum<I: Iterator<Item = &'a T>>(iter: I) -> Self

Method which takes an iterator and generates Self from the elements by “summing up” the items.
source§

impl<T: BigEnumSetType> Sum<BigEnumSet<T>> for BigEnumSet<T>

source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Method which takes an iterator and generates Self from the elements by “summing up” the items.
source§

impl<T: BigEnumSetType> Sum<T> for BigEnumSet<T>

source§

fn sum<I: Iterator<Item = T>>(iter: I) -> Self

Method which takes an iterator and generates Self from the elements by “summing up” the items.
source§

impl<T: Copy + BigEnumSetType> Copy for BigEnumSet<T>where T::Repr: Copy,

source§

impl<T: Eq + BigEnumSetType> Eq for BigEnumSet<T>where T::Repr: Eq,

source§

impl<T: BigEnumSetType> StructuralEq for BigEnumSet<T>

source§

impl<T: BigEnumSetType> StructuralPartialEq for BigEnumSet<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for BigEnumSet<T>where <T as BigEnumSetTypePrivate>::Repr: RefUnwindSafe,

§

impl<T> Send for BigEnumSet<T>where <T as BigEnumSetTypePrivate>::Repr: Send,

§

impl<T> Sync for BigEnumSet<T>where <T as BigEnumSetTypePrivate>::Repr: Sync,

§

impl<T> Unpin for BigEnumSet<T>where <T as BigEnumSetTypePrivate>::Repr: Unpin,

§

impl<T> UnwindSafe for BigEnumSet<T>where <T as BigEnumSetTypePrivate>::Repr: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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 Twhere 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 Twhere 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.