Struct EnumSet

Source
#[repr(transparent)]
pub struct EnumSet<T>
where T: EnumSetType,
{ /* private fields */ }
Expand description

An efficient set type for enums.

It is implemented using a bitset stored using the smallest integer that can fit all bits in the underlying enum. In general, an enum variant with a numeric value of n is stored in the nth least significant bit (corresponding with a mask of, e.g. 1 << enum as u32).

§Serialization

When the serde feature is enabled, EnumSets 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, EnumSets serialize by directly writing out the underlying bitset as an integer of the smallest type that can fit in the underlying enum. You can add a #[enumset(serialize_repr = "u8")] attribute to your enum to control the integer type used for serialization. This can be important for avoiding unintentional breaking changes when EnumSets 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 #[enumset(serialize_deny_unknown)] attribute. This will cause deserialization to fail if an invalid bit is set.

In addition, the #[enumset(serialize_as_list)] attribute causes the EnumSet 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> EnumSet<T>
where T: EnumSetType,

Source

pub fn new() -> EnumSet<T>

Creates an empty EnumSet.

Source

pub fn only(t: T) -> EnumSet<T>

Returns an EnumSet containing a single element.

Source

pub fn empty() -> EnumSet<T>

Creates an empty EnumSet.

This is an alias for EnumSet::new.

Source

pub fn all() -> EnumSet<T>

Returns an EnumSet 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 integer type (u8, u16, u32, u64, or u128).

This is the same as EnumSet::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 that this type can contain.

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

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(&self, other: EnumSet<T>) -> 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(&self, other: EnumSet<T>) -> bool

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

Source

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

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

Source

pub fn union(&self, other: EnumSet<T>) -> EnumSet<T>

Returns a set containing any elements present in either set.

Source

pub fn intersection(&self, other: EnumSet<T>) -> EnumSet<T>

Returns a set containing every element present in both sets.

Source

pub fn difference(&self, other: EnumSet<T>) -> EnumSet<T>

Returns a set containing element present in self but not in other.

Source

pub fn symmetrical_difference(&self, other: EnumSet<T>) -> EnumSet<T>

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

Source

pub fn complement(&self) -> EnumSet<T>

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

Source

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

Checks whether this set contains a 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(&mut self, other: EnumSet<T>)

Adds all elements in another set to this one.

Source

pub fn remove_all(&mut self, other: EnumSet<T>)

Removes all values in another set from this one.

Source

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

Creates an iterator over the values in this set.

Note that iterator invalidation is impossible as the iterator contains a copy of this type, rather than holding a reference to it.

Source§

impl<T> EnumSet<T>
where T: EnumSetType,

Source

pub fn as_u8(&self) -> u8

Returns a u8 representing the elements of this set.

If the underlying bitset will not fit in a u8, this method will panic.

Source

pub fn try_as_u8(&self) -> Option<u8>

Tries to return a u8 representing the elements of this set.

If the underlying bitset will not fit in a u8, this method will instead return None.

Source

pub fn as_u8_truncated(&self) -> u8

Returns a truncated u8 representing the elements of this set.

If the underlying bitset will not fit in a u8, this method will truncate any bits that don’t fit.

Source

pub fn from_u8(bits: u8) -> EnumSet<T>

Constructs a bitset from a u8.

If a bit that doesn’t correspond to an enum variant is set, this method will panic.

Source

pub fn try_from_u8(bits: u8) -> Option<EnumSet<T>>

Attempts to constructs a bitset from a u8.

If a bit that doesn’t correspond to an enum variant is set, this method will return None.

Source

pub fn from_u8_truncated(bits: u8) -> EnumSet<T>

Constructs a bitset from a u8, ignoring invalid variants.

Source

pub fn as_u16(&self) -> u16

Returns a u16 representing the elements of this set.

If the underlying bitset will not fit in a u16, this method will panic.

Source

pub fn try_as_u16(&self) -> Option<u16>

Tries to return a u16 representing the elements of this set.

If the underlying bitset will not fit in a u16, this method will instead return None.

Source

pub fn as_u16_truncated(&self) -> u16

Returns a truncated u16 representing the elements of this set.

If the underlying bitset will not fit in a u16, this method will truncate any bits that don’t fit.

Source

pub fn from_u16(bits: u16) -> EnumSet<T>

Constructs a bitset from a u16.

If a bit that doesn’t correspond to an enum variant is set, this method will panic.

Source

pub fn try_from_u16(bits: u16) -> Option<EnumSet<T>>

Attempts to constructs a bitset from a u16.

If a bit that doesn’t correspond to an enum variant is set, this method will return None.

Source

pub fn from_u16_truncated(bits: u16) -> EnumSet<T>

Constructs a bitset from a u16, ignoring invalid variants.

Source

pub fn as_u32(&self) -> u32

Returns a u32 representing the elements of this set.

If the underlying bitset will not fit in a u32, this method will panic.

Source

pub fn try_as_u32(&self) -> Option<u32>

Tries to return a u32 representing the elements of this set.

If the underlying bitset will not fit in a u32, this method will instead return None.

Source

pub fn as_u32_truncated(&self) -> u32

Returns a truncated u32 representing the elements of this set.

If the underlying bitset will not fit in a u32, this method will truncate any bits that don’t fit.

Source

pub fn from_u32(bits: u32) -> EnumSet<T>

Constructs a bitset from a u32.

If a bit that doesn’t correspond to an enum variant is set, this method will panic.

Source

pub fn try_from_u32(bits: u32) -> Option<EnumSet<T>>

Attempts to constructs a bitset from a u32.

If a bit that doesn’t correspond to an enum variant is set, this method will return None.

Source

pub fn from_u32_truncated(bits: u32) -> EnumSet<T>

Constructs a bitset from a u32, ignoring invalid variants.

Source

pub fn as_u64(&self) -> u64

Returns a u64 representing the elements of this set.

If the underlying bitset will not fit in a u64, this method will panic.

Source

pub fn try_as_u64(&self) -> Option<u64>

Tries to return a u64 representing the elements of this set.

If the underlying bitset will not fit in a u64, this method will instead return None.

Source

pub fn as_u64_truncated(&self) -> u64

Returns a truncated u64 representing the elements of this set.

If the underlying bitset will not fit in a u64, this method will truncate any bits that don’t fit.

Source

pub fn from_u64(bits: u64) -> EnumSet<T>

Constructs a bitset from a u64.

If a bit that doesn’t correspond to an enum variant is set, this method will panic.

Source

pub fn try_from_u64(bits: u64) -> Option<EnumSet<T>>

Attempts to constructs a bitset from a u64.

If a bit that doesn’t correspond to an enum variant is set, this method will return None.

Source

pub fn from_u64_truncated(bits: u64) -> EnumSet<T>

Constructs a bitset from a u64, ignoring invalid variants.

Source

pub fn as_u128(&self) -> u128

Returns a u128 representing the elements of this set.

If the underlying bitset will not fit in a u128, this method will panic.

Source

pub fn try_as_u128(&self) -> Option<u128>

Tries to return a u128 representing the elements of this set.

If the underlying bitset will not fit in a u128, this method will instead return None.

Source

pub fn as_u128_truncated(&self) -> u128

Returns a truncated u128 representing the elements of this set.

If the underlying bitset will not fit in a u128, this method will truncate any bits that don’t fit.

Source

pub fn from_u128(bits: u128) -> EnumSet<T>

Constructs a bitset from a u128.

If a bit that doesn’t correspond to an enum variant is set, this method will panic.

Source

pub fn try_from_u128(bits: u128) -> Option<EnumSet<T>>

Attempts to constructs a bitset from a u128.

If a bit that doesn’t correspond to an enum variant is set, this method will return None.

Source

pub fn from_u128_truncated(bits: u128) -> EnumSet<T>

Constructs a bitset from a u128, ignoring invalid variants.

Source

pub fn as_usize(&self) -> usize

Returns a usize representing the elements of this set.

If the underlying bitset will not fit in a usize, this method will panic.

Source

pub fn try_as_usize(&self) -> Option<usize>

Tries to return a usize representing the elements of this set.

If the underlying bitset will not fit in a usize, this method will instead return None.

Source

pub fn as_usize_truncated(&self) -> usize

Returns a truncated usize representing the elements of this set.

If the underlying bitset will not fit in a usize, this method will truncate any bits that don’t fit.

Source

pub fn from_usize(bits: usize) -> EnumSet<T>

Constructs a bitset from a usize.

If a bit that doesn’t correspond to an enum variant is set, this method will panic.

Source

pub fn try_from_usize(bits: usize) -> Option<EnumSet<T>>

Attempts to constructs a bitset from a usize.

If a bit that doesn’t correspond to an enum variant is set, this method will return None.

Source

pub fn from_usize_truncated(bits: usize) -> EnumSet<T>

Constructs a bitset from a usize, ignoring invalid variants.

Trait Implementations§

Source§

impl<T, O> BitAnd<O> for EnumSet<T>
where T: EnumSetType, O: Into<EnumSet<T>>,

Source§

type Output = EnumSet<T>

The resulting type after applying the & operator.
Source§

fn bitand(self, other: O) -> <EnumSet<T> as BitAnd<O>>::Output

Performs the & operation. Read more
Source§

impl<T, O> BitAndAssign<O> for EnumSet<T>
where T: EnumSetType, O: Into<EnumSet<T>>,

Source§

fn bitand_assign(&mut self, rhs: O)

Performs the &= operation. Read more
Source§

impl<T, O> BitOr<O> for EnumSet<T>
where T: EnumSetType, O: Into<EnumSet<T>>,

Source§

type Output = EnumSet<T>

The resulting type after applying the | operator.
Source§

fn bitor(self, other: O) -> <EnumSet<T> as BitOr<O>>::Output

Performs the | operation. Read more
Source§

impl<T, O> BitOrAssign<O> for EnumSet<T>
where T: EnumSetType, O: Into<EnumSet<T>>,

Source§

fn bitor_assign(&mut self, rhs: O)

Performs the |= operation. Read more
Source§

impl<T, O> BitXor<O> for EnumSet<T>
where T: EnumSetType, O: Into<EnumSet<T>>,

Source§

type Output = EnumSet<T>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: O) -> <EnumSet<T> as BitXor<O>>::Output

Performs the ^ operation. Read more
Source§

impl<T, O> BitXorAssign<O> for EnumSet<T>
where T: EnumSetType, O: Into<EnumSet<T>>,

Source§

fn bitxor_assign(&mut self, rhs: O)

Performs the ^= operation. Read more
Source§

impl<T> Clone for EnumSet<T>
where T: Clone + EnumSetType, <T as EnumSetTypePrivate>::Repr: Clone,

Source§

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

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 for EnumSet<T>
where T: EnumSetType + Debug,

Source§

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

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

impl<T> Default for EnumSet<T>
where T: EnumSetType,

Source§

fn default() -> EnumSet<T>

Returns an empty set.

Source§

impl<T> Extend<EnumSet<T>> for EnumSet<T>
where T: EnumSetType,

Source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = EnumSet<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<T> Extend<T> for EnumSet<T>
where T: EnumSetType,

Source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = 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<T> From<T> for EnumSet<T>
where T: EnumSetType,

Source§

fn from(t: T) -> EnumSet<T>

Converts to this type from the input type.
Source§

impl<T> FromIterator<EnumSet<T>> for EnumSet<T>
where T: EnumSetType,

Source§

fn from_iter<I>(iter: I) -> EnumSet<T>
where I: IntoIterator<Item = EnumSet<T>>,

Creates a value from an iterator. Read more
Source§

impl<T> FromIterator<T> for EnumSet<T>
where T: EnumSetType,

Source§

fn from_iter<I>(iter: I) -> EnumSet<T>
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
Source§

impl<T> Hash for EnumSet<T>
where T: EnumSetType,

Source§

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

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> IntoIterator for EnumSet<T>
where T: EnumSetType,

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = EnumSetIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> <EnumSet<T> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> Not for EnumSet<T>
where T: EnumSetType,

Source§

type Output = EnumSet<T>

The resulting type after applying the ! operator.
Source§

fn not(self) -> <EnumSet<T> as Not>::Output

Performs the unary ! operation. Read more
Source§

impl<T> Ord for EnumSet<T>
where T: EnumSetType,

Source§

fn cmp(&self, other: &EnumSet<T>) -> Ordering

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

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

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

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

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

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

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

impl PartialEq<EnumSet<LockType>> for LockType

Source§

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

Tests for self and other values to be equal, and is used by ==.
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> PartialEq<T> for EnumSet<T>
where T: EnumSetType,

Source§

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

Tests for self and other values to be equal, and is used by ==.
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> PartialEq for EnumSet<T>
where T: PartialEq + EnumSetType, <T as EnumSetTypePrivate>::Repr: PartialEq,

Source§

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

Tests for self and other values to be equal, and is used by ==.
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> PartialOrd for EnumSet<T>
where T: EnumSetType,

Source§

fn partial_cmp(&self, other: &EnumSet<T>) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T, O> Sub<O> for EnumSet<T>
where T: EnumSetType, O: Into<EnumSet<T>>,

Source§

type Output = EnumSet<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: O) -> <EnumSet<T> as Sub<O>>::Output

Performs the - operation. Read more
Source§

impl<T, O> SubAssign<O> for EnumSet<T>
where T: EnumSetType, O: Into<EnumSet<T>>,

Source§

fn sub_assign(&mut self, rhs: O)

Performs the -= operation. Read more
Source§

impl<'a, T> Sum<&'a EnumSet<T>> for EnumSet<T>
where T: EnumSetType,

Source§

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

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl<'a, T> Sum<&'a T> for EnumSet<T>
where T: EnumSetType,

Source§

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

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl<T> Sum<T> for EnumSet<T>
where T: EnumSetType,

Source§

fn sum<I>(iter: I) -> EnumSet<T>
where I: Iterator<Item = T>,

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl<T> Sum for EnumSet<T>
where T: EnumSetType,

Source§

fn sum<I>(iter: I) -> EnumSet<T>
where I: Iterator<Item = EnumSet<T>>,

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl<T> Copy for EnumSet<T>
where T: Copy + EnumSetType, <T as EnumSetTypePrivate>::Repr: Copy,

Source§

impl<T> Eq for EnumSet<T>
where T: Eq + EnumSetType, <T as EnumSetTypePrivate>::Repr: Eq,

Source§

impl<T> StructuralPartialEq for EnumSet<T>
where T: EnumSetType,

Auto Trait Implementations§

§

impl<T> Freeze for EnumSet<T>
where <T as EnumSetTypePrivate>::Repr: Freeze,

§

impl<T> RefUnwindSafe for EnumSet<T>
where <T as EnumSetTypePrivate>::Repr: RefUnwindSafe,

§

impl<T> Send for EnumSet<T>
where <T as EnumSetTypePrivate>::Repr: Send,

§

impl<T> Sync for EnumSet<T>
where <T as EnumSetTypePrivate>::Repr: Sync,

§

impl<T> Unpin for EnumSet<T>
where <T as EnumSetTypePrivate>::Repr: Unpin,

§

impl<T> UnwindSafe for EnumSet<T>
where <T as EnumSetTypePrivate>::Repr: 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<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> 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.