#[repr(transparent)]pub struct EnumSet<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, EnumSet
s 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, EnumSet
s 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
EnumSet
s 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
sourceimpl<T: EnumSetType> EnumSet<T>
impl<T: EnumSetType> EnumSet<T>
sourcepub fn empty() -> Self
pub fn empty() -> Self
Creates an empty EnumSet
.
This is an alias for EnumSet::new
.
sourcepub fn bit_width() -> u32
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 }
)
sourcepub fn variant_count() -> u32
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 }
)
sourcepub fn is_disjoint(&self, other: Self) -> bool
pub fn is_disjoint(&self, other: Self) -> bool
Returns true
if self
has no elements in common with other
. This is equivalent to
checking for an empty intersection.
sourcepub fn is_superset(&self, other: Self) -> bool
pub fn is_superset(&self, other: Self) -> bool
Returns true
if the set is a superset of another, i.e., self
contains at least all the
values in other
.
sourcepub fn is_subset(&self, other: Self) -> bool
pub fn is_subset(&self, other: Self) -> bool
Returns true
if the set is a subset of another, i.e., other
contains at least all
the values in self
.
sourcepub fn union(&self, other: Self) -> Self
pub fn union(&self, other: Self) -> Self
Returns a set containing any elements present in either set.
sourcepub fn intersection(&self, other: Self) -> Self
pub fn intersection(&self, other: Self) -> Self
Returns a set containing every element present in both sets.
sourcepub fn difference(&self, other: Self) -> Self
pub fn difference(&self, other: Self) -> Self
Returns a set containing element present in self
but not in other
.
sourcepub fn symmetrical_difference(&self, other: Self) -> Self
pub fn symmetrical_difference(&self, other: Self) -> Self
Returns a set containing every element present in either self
or other
, but is not
present in both.
sourcepub fn complement(&self) -> Self
pub fn complement(&self) -> Self
Returns a set containing all enum variants not in this set.
sourcepub fn insert(&mut self, value: T) -> bool
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.
sourcepub fn remove(&mut self, value: T) -> bool
pub fn remove(&mut self, value: T) -> bool
Removes a value from this set. Returns whether the value was present in the set.
sourcepub fn insert_all(&mut self, other: Self)
pub fn insert_all(&mut self, other: Self)
Adds all elements in another set to this one.
sourcepub fn remove_all(&mut self, other: Self)
pub fn remove_all(&mut self, other: Self)
Removes all values in another set from this one.
sourcepub fn iter(&self) -> EnumSetIter<T>ⓘNotable traits for EnumSetIter<T>impl<T: EnumSetType> Iterator for EnumSetIter<T> type Item = T;
pub fn iter(&self) -> EnumSetIter<T>ⓘNotable traits for EnumSetIter<T>impl<T: EnumSetType> Iterator for EnumSetIter<T> type Item = 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.
sourceimpl<T: EnumSetType> EnumSet<T>
impl<T: EnumSetType> EnumSet<T>
sourcepub fn as_u8(&self) -> u8
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.
sourcepub fn try_as_u8(&self) -> Option<u8>
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
.
sourcepub fn as_u8_truncated(&self) -> u8
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.
sourcepub fn from_u8(bits: u8) -> Self
pub fn from_u8(bits: u8) -> Self
Constructs a bitset from a u8
.
If a bit that doesn’t correspond to an enum variant is set, this method will panic.
sourcepub fn try_from_u8(bits: u8) -> Option<Self>
pub fn try_from_u8(bits: u8) -> Option<Self>
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
.
sourcepub fn from_u8_truncated(bits: u8) -> Self
pub fn from_u8_truncated(bits: u8) -> Self
Constructs a bitset from a u8
, ignoring invalid variants.
sourcepub fn as_u16(&self) -> u16
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.
sourcepub fn try_as_u16(&self) -> Option<u16>
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
.
sourcepub fn as_u16_truncated(&self) -> u16
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.
sourcepub fn from_u16(bits: u16) -> Self
pub fn from_u16(bits: u16) -> Self
Constructs a bitset from a u16
.
If a bit that doesn’t correspond to an enum variant is set, this method will panic.
sourcepub fn try_from_u16(bits: u16) -> Option<Self>
pub fn try_from_u16(bits: u16) -> Option<Self>
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
.
sourcepub fn from_u16_truncated(bits: u16) -> Self
pub fn from_u16_truncated(bits: u16) -> Self
Constructs a bitset from a u16
, ignoring invalid variants.
sourcepub fn as_u32(&self) -> u32
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.
sourcepub fn try_as_u32(&self) -> Option<u32>
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
.
sourcepub fn as_u32_truncated(&self) -> u32
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.
sourcepub fn from_u32(bits: u32) -> Self
pub fn from_u32(bits: u32) -> Self
Constructs a bitset from a u32
.
If a bit that doesn’t correspond to an enum variant is set, this method will panic.
sourcepub fn try_from_u32(bits: u32) -> Option<Self>
pub fn try_from_u32(bits: u32) -> Option<Self>
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
.
sourcepub fn from_u32_truncated(bits: u32) -> Self
pub fn from_u32_truncated(bits: u32) -> Self
Constructs a bitset from a u32
, ignoring invalid variants.
sourcepub fn as_u64(&self) -> u64
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.
sourcepub fn try_as_u64(&self) -> Option<u64>
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
.
sourcepub fn as_u64_truncated(&self) -> u64
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.
sourcepub fn from_u64(bits: u64) -> Self
pub fn from_u64(bits: u64) -> Self
Constructs a bitset from a u64
.
If a bit that doesn’t correspond to an enum variant is set, this method will panic.
sourcepub fn try_from_u64(bits: u64) -> Option<Self>
pub fn try_from_u64(bits: u64) -> Option<Self>
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
.
sourcepub fn from_u64_truncated(bits: u64) -> Self
pub fn from_u64_truncated(bits: u64) -> Self
Constructs a bitset from a u64
, ignoring invalid variants.
sourcepub fn as_u128(&self) -> u128
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.
sourcepub fn try_as_u128(&self) -> Option<u128>
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
.
sourcepub fn as_u128_truncated(&self) -> u128
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.
sourcepub fn from_u128(bits: u128) -> Self
pub fn from_u128(bits: u128) -> Self
Constructs a bitset from a u128
.
If a bit that doesn’t correspond to an enum variant is set, this method will panic.
sourcepub fn try_from_u128(bits: u128) -> Option<Self>
pub fn try_from_u128(bits: u128) -> Option<Self>
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
.
sourcepub fn from_u128_truncated(bits: u128) -> Self
pub fn from_u128_truncated(bits: u128) -> Self
Constructs a bitset from a u128
, ignoring invalid variants.
sourcepub fn as_usize(&self) -> usize
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.
sourcepub fn try_as_usize(&self) -> Option<usize>
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
.
sourcepub fn as_usize_truncated(&self) -> usize
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.
sourcepub fn from_usize(bits: usize) -> Self
pub fn from_usize(bits: usize) -> Self
Constructs a bitset from a usize
.
If a bit that doesn’t correspond to an enum variant is set, this method will panic.
sourcepub fn try_from_usize(bits: usize) -> Option<Self>
pub fn try_from_usize(bits: usize) -> Option<Self>
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
.
sourcepub fn from_usize_truncated(bits: usize) -> Self
pub fn from_usize_truncated(bits: usize) -> Self
Constructs a bitset from a usize
, ignoring invalid variants.
Trait Implementations
sourceimpl<T: EnumSetType, O: Into<EnumSet<T>>> BitAndAssign<O> for EnumSet<T>
impl<T: EnumSetType, O: Into<EnumSet<T>>> BitAndAssign<O> for EnumSet<T>
sourcefn bitand_assign(&mut self, rhs: O)
fn bitand_assign(&mut self, rhs: O)
Performs the &=
operation. Read more
sourceimpl<T: EnumSetType, O: Into<EnumSet<T>>> BitOrAssign<O> for EnumSet<T>
impl<T: EnumSetType, O: Into<EnumSet<T>>> BitOrAssign<O> for EnumSet<T>
sourcefn bitor_assign(&mut self, rhs: O)
fn bitor_assign(&mut self, rhs: O)
Performs the |=
operation. Read more
sourceimpl<T: EnumSetType, O: Into<EnumSet<T>>> BitXorAssign<O> for EnumSet<T>
impl<T: EnumSetType, O: Into<EnumSet<T>>> BitXorAssign<O> for EnumSet<T>
sourcefn bitxor_assign(&mut self, rhs: O)
fn bitxor_assign(&mut self, rhs: O)
Performs the ^=
operation. Read more
sourceimpl<T: EnumSetType + Debug> Debug for EnumSet<T>
impl<T: EnumSetType + Debug> Debug for EnumSet<T>
sourceimpl<T: EnumSetType> Extend<EnumSet<T>> for EnumSet<T>
impl<T: EnumSetType> Extend<EnumSet<T>> for EnumSet<T>
sourcefn extend<I: IntoIterator<Item = EnumSet<T>>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = EnumSet<T>>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<T: EnumSetType> Extend<T> for EnumSet<T>
impl<T: EnumSetType> Extend<T> for EnumSet<T>
sourcefn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<T: EnumSetType> From<T> for EnumSet<T>
impl<T: EnumSetType> From<T> for EnumSet<T>
sourceimpl<T: EnumSetType> FromIterator<EnumSet<T>> for EnumSet<T>
impl<T: EnumSetType> FromIterator<EnumSet<T>> for EnumSet<T>
sourcefn from_iter<I: IntoIterator<Item = EnumSet<T>>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = EnumSet<T>>>(iter: I) -> Self
Creates a value from an iterator. Read more
sourceimpl<T: EnumSetType> FromIterator<T> for EnumSet<T>
impl<T: EnumSetType> FromIterator<T> for EnumSet<T>
sourcefn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Creates a value from an iterator. Read more
sourceimpl<T: EnumSetType> Hash for EnumSet<T>
impl<T: EnumSetType> Hash for EnumSet<T>
sourceimpl<T: EnumSetType> IntoIterator for EnumSet<T>
impl<T: EnumSetType> IntoIterator for EnumSet<T>
sourceimpl<T: EnumSetType> Not for EnumSet<T>
impl<T: EnumSetType> Not for EnumSet<T>
sourceimpl<T: EnumSetType> Ord for EnumSet<T>
impl<T: EnumSetType> Ord for EnumSet<T>
sourceimpl<T: PartialEq + EnumSetType> PartialEq<EnumSet<T>> for EnumSet<T> where
T::Repr: PartialEq,
impl<T: PartialEq + EnumSetType> PartialEq<EnumSet<T>> for EnumSet<T> where
T::Repr: PartialEq,
sourceimpl<T: EnumSetType> PartialEq<T> for EnumSet<T>
impl<T: EnumSetType> PartialEq<T> for EnumSet<T>
sourceimpl<T: EnumSetType> PartialOrd<EnumSet<T>> for EnumSet<T>
impl<T: EnumSetType> PartialOrd<EnumSet<T>> for EnumSet<T>
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
sourceimpl<T: EnumSetType, O: Into<EnumSet<T>>> SubAssign<O> for EnumSet<T>
impl<T: EnumSetType, O: Into<EnumSet<T>>> SubAssign<O> for EnumSet<T>
sourcefn sub_assign(&mut self, rhs: O)
fn sub_assign(&mut self, rhs: O)
Performs the -=
operation. Read more
sourceimpl<'a, T: EnumSetType> Sum<&'a EnumSet<T>> for EnumSet<T>
impl<'a, T: EnumSetType> Sum<&'a EnumSet<T>> for EnumSet<T>
sourceimpl<'a, T: EnumSetType> Sum<&'a T> for EnumSet<T>
impl<'a, T: EnumSetType> Sum<&'a T> for EnumSet<T>
sourceimpl<T: EnumSetType> Sum<EnumSet<T>> for EnumSet<T>
impl<T: EnumSetType> Sum<EnumSet<T>> for EnumSet<T>
sourceimpl<T: EnumSetType> Sum<T> for EnumSet<T>
impl<T: EnumSetType> Sum<T> for EnumSet<T>
impl<T: Copy + EnumSetType> Copy for EnumSet<T> where
T::Repr: Copy,
impl<T: Eq + EnumSetType> Eq for EnumSet<T> where
T::Repr: Eq,
impl<T: EnumSetType> StructuralEq for EnumSet<T>
impl<T: EnumSetType> StructuralPartialEq for EnumSet<T>
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more