Struct big_enum_set::BigEnumSet
source · 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, BigEnumSet
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 BigEnumSet
s 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 % 8
th 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
BigEnumSet
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 #[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>
impl<T: BigEnumSetType> BigEnumSet<T>
sourcepub const EMPTY: BigEnumSet<T> = _
pub const EMPTY: BigEnumSet<T> = _
Empty set.
sourcepub fn empty() -> Self
pub fn empty() -> Self
Creates an empty BigEnumSet
.
This is an alias for BigEnumSet::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 usize
.
This is the same as BigEnumSet::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 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 }
)
sourcepub fn try_from_bits(bits: &[usize]) -> Option<Self>
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.
sourcepub fn from_bits_truncated(bits: &[usize]) -> Self
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.
sourcepub fn is_disjoint<O: Borrow<Self>>(&self, other: O) -> bool
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.
sourcepub fn is_superset<O: Borrow<Self>>(&self, other: O) -> bool
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
.
sourcepub fn is_subset<O: Borrow<Self>>(&self, other: O) -> bool
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
.
sourcepub fn union<O: Borrow<Self>>(&self, other: O) -> Self
pub fn union<O: Borrow<Self>>(&self, other: O) -> Self
Returns a set containing all elements present in either set.
sourcepub fn intersection<O: Borrow<Self>>(&self, other: O) -> Self
pub fn intersection<O: Borrow<Self>>(&self, other: O) -> Self
Returns a set containing all elements present in both sets.
sourcepub fn difference<O: Borrow<Self>>(&self, other: O) -> Self
pub fn difference<O: Borrow<Self>>(&self, other: O) -> Self
Returns a set containing all elements present in self
but not in other
.
sourcepub fn symmetrical_difference<O: Borrow<Self>>(&self, other: O) -> Self
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.
sourcepub fn complement(&self) -> Self
pub fn complement(&self) -> Self
Returns a set containing all enum variants not present 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<O: Borrow<Self>>(&mut self, other: O)
pub fn insert_all<O: Borrow<Self>>(&mut self, other: O)
Adds all elements in another set to this one.
sourcepub fn remove_all<O: Borrow<Self>>(&mut self, other: O)
pub fn remove_all<O: Borrow<Self>>(&mut self, other: O)
Removes all values in another set from this one.
sourcepub fn iter(&self) -> EnumSetIter<&BigEnumSet<T>, T> ⓘ
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>
impl<T: BigEnumSetType> BitAnd<&BigEnumSet<T>> for &BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
&
operator.source§impl<T: BigEnumSetType> BitAnd<&BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> BitAnd<&BigEnumSet<T>> for BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
&
operator.source§impl<T: BigEnumSetType> BitAnd<BigEnumSet<T>> for &BigEnumSet<T>
impl<T: BigEnumSetType> BitAnd<BigEnumSet<T>> for &BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
&
operator.source§impl<T: BigEnumSetType> BitAnd<BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> BitAnd<BigEnumSet<T>> for BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
&
operator.source§impl<T: BigEnumSetType> BitAnd<T> for &BigEnumSet<T>
impl<T: BigEnumSetType> BitAnd<T> for &BigEnumSet<T>
source§impl<T: BigEnumSetType> BitAnd<T> for BigEnumSet<T>
impl<T: BigEnumSetType> BitAnd<T> for BigEnumSet<T>
source§impl<T: BigEnumSetType> BitAndAssign<&BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> BitAndAssign<&BigEnumSet<T>> for BigEnumSet<T>
source§fn bitand_assign(&mut self, other: &BigEnumSet<T>)
fn bitand_assign(&mut self, other: &BigEnumSet<T>)
&=
operation. Read moresource§impl<T: BigEnumSetType> BitAndAssign<BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> BitAndAssign<BigEnumSet<T>> for BigEnumSet<T>
source§fn bitand_assign(&mut self, other: BigEnumSet<T>)
fn bitand_assign(&mut self, other: BigEnumSet<T>)
&=
operation. Read moresource§impl<T: BigEnumSetType> BitAndAssign<T> for BigEnumSet<T>
impl<T: BigEnumSetType> BitAndAssign<T> for BigEnumSet<T>
source§fn bitand_assign(&mut self, value: T)
fn bitand_assign(&mut self, value: T)
&=
operation. Read moresource§impl<T: BigEnumSetType> BitOr<&BigEnumSet<T>> for &BigEnumSet<T>
impl<T: BigEnumSetType> BitOr<&BigEnumSet<T>> for &BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
|
operator.source§impl<T: BigEnumSetType> BitOr<&BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> BitOr<&BigEnumSet<T>> for BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
|
operator.source§impl<T: BigEnumSetType> BitOr<BigEnumSet<T>> for &BigEnumSet<T>
impl<T: BigEnumSetType> BitOr<BigEnumSet<T>> for &BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
|
operator.source§impl<T: BigEnumSetType> BitOr<BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> BitOr<BigEnumSet<T>> for BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
|
operator.source§impl<T: BigEnumSetType> BitOr<T> for &BigEnumSet<T>
impl<T: BigEnumSetType> BitOr<T> for &BigEnumSet<T>
source§impl<T: BigEnumSetType> BitOr<T> for BigEnumSet<T>
impl<T: BigEnumSetType> BitOr<T> for BigEnumSet<T>
source§impl<T: BigEnumSetType> BitOrAssign<&BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> BitOrAssign<&BigEnumSet<T>> for BigEnumSet<T>
source§fn bitor_assign(&mut self, other: &BigEnumSet<T>)
fn bitor_assign(&mut self, other: &BigEnumSet<T>)
|=
operation. Read moresource§impl<T: BigEnumSetType> BitOrAssign<BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> BitOrAssign<BigEnumSet<T>> for BigEnumSet<T>
source§fn bitor_assign(&mut self, other: BigEnumSet<T>)
fn bitor_assign(&mut self, other: BigEnumSet<T>)
|=
operation. Read moresource§impl<T: BigEnumSetType> BitOrAssign<T> for BigEnumSet<T>
impl<T: BigEnumSetType> BitOrAssign<T> for BigEnumSet<T>
source§fn bitor_assign(&mut self, value: T)
fn bitor_assign(&mut self, value: T)
|=
operation. Read moresource§impl<T: BigEnumSetType> BitXor<&BigEnumSet<T>> for &BigEnumSet<T>
impl<T: BigEnumSetType> BitXor<&BigEnumSet<T>> for &BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
^
operator.source§impl<T: BigEnumSetType> BitXor<&BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> BitXor<&BigEnumSet<T>> for BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
^
operator.source§impl<T: BigEnumSetType> BitXor<BigEnumSet<T>> for &BigEnumSet<T>
impl<T: BigEnumSetType> BitXor<BigEnumSet<T>> for &BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
^
operator.source§impl<T: BigEnumSetType> BitXor<BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> BitXor<BigEnumSet<T>> for BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
^
operator.source§impl<T: BigEnumSetType> BitXor<T> for &BigEnumSet<T>
impl<T: BigEnumSetType> BitXor<T> for &BigEnumSet<T>
source§impl<T: BigEnumSetType> BitXor<T> for BigEnumSet<T>
impl<T: BigEnumSetType> BitXor<T> for BigEnumSet<T>
source§impl<T: BigEnumSetType> BitXorAssign<&BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> BitXorAssign<&BigEnumSet<T>> for BigEnumSet<T>
source§fn bitxor_assign(&mut self, other: &BigEnumSet<T>)
fn bitxor_assign(&mut self, other: &BigEnumSet<T>)
^=
operation. Read moresource§impl<T: BigEnumSetType> BitXorAssign<BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> BitXorAssign<BigEnumSet<T>> for BigEnumSet<T>
source§fn bitxor_assign(&mut self, other: BigEnumSet<T>)
fn bitxor_assign(&mut self, other: BigEnumSet<T>)
^=
operation. Read moresource§impl<T: BigEnumSetType> BitXorAssign<T> for BigEnumSet<T>
impl<T: BigEnumSetType> BitXorAssign<T> for BigEnumSet<T>
source§fn bitxor_assign(&mut self, value: T)
fn bitxor_assign(&mut self, value: T)
^=
operation. Read moresource§impl<T: Clone + BigEnumSetType> Clone for BigEnumSet<T>where
T::Repr: Clone,
impl<T: Clone + BigEnumSetType> Clone for BigEnumSet<T>where T::Repr: Clone,
source§fn clone(&self) -> BigEnumSet<T>
fn clone(&self) -> BigEnumSet<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T: BigEnumSetType + Debug> Debug for BigEnumSet<T>
impl<T: BigEnumSetType + Debug> Debug for BigEnumSet<T>
source§impl<T: BigEnumSetType> Default for BigEnumSet<T>
impl<T: BigEnumSetType> Default for BigEnumSet<T>
source§impl<T: BigEnumSetType> Extend<T> for BigEnumSet<T>
impl<T: BigEnumSetType> Extend<T> for BigEnumSet<T>
source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
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<T: BigEnumSetType> From<T> for BigEnumSet<T>
impl<T: BigEnumSetType> From<T> for BigEnumSet<T>
source§impl<T: BigEnumSetType> FromIterator<T> for BigEnumSet<T>
impl<T: BigEnumSetType> FromIterator<T> for BigEnumSet<T>
source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
source§impl<T: BigEnumSetType> Hash for BigEnumSet<T>
impl<T: BigEnumSetType> Hash for BigEnumSet<T>
source§impl<T: BigEnumSetType> IntoIterator for BigEnumSet<T>
impl<T: BigEnumSetType> IntoIterator for BigEnumSet<T>
source§impl<T: BigEnumSetType> Not for &BigEnumSet<T>
impl<T: BigEnumSetType> Not for &BigEnumSet<T>
source§impl<T: BigEnumSetType> Not for BigEnumSet<T>
impl<T: BigEnumSetType> Not for BigEnumSet<T>
source§impl<T: BigEnumSetType> Ord for BigEnumSet<T>
impl<T: BigEnumSetType> Ord for BigEnumSet<T>
source§impl<T: PartialEq + BigEnumSetType> PartialEq<BigEnumSet<T>> for BigEnumSet<T>where
T::Repr: PartialEq,
impl<T: PartialEq + BigEnumSetType> PartialEq<BigEnumSet<T>> for BigEnumSet<T>where T::Repr: PartialEq,
source§fn eq(&self, other: &BigEnumSet<T>) -> bool
fn eq(&self, other: &BigEnumSet<T>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<T: BigEnumSetType> PartialEq<T> for BigEnumSet<T>
impl<T: BigEnumSetType> PartialEq<T> for BigEnumSet<T>
source§impl<T: BigEnumSetType> PartialOrd<BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> PartialOrd<BigEnumSet<T>> for BigEnumSet<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<T: BigEnumSetType> Sub<&BigEnumSet<T>> for &BigEnumSet<T>
impl<T: BigEnumSetType> Sub<&BigEnumSet<T>> for &BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
-
operator.source§impl<T: BigEnumSetType> Sub<&BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> Sub<&BigEnumSet<T>> for BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
-
operator.source§impl<T: BigEnumSetType> Sub<BigEnumSet<T>> for &BigEnumSet<T>
impl<T: BigEnumSetType> Sub<BigEnumSet<T>> for &BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
-
operator.source§impl<T: BigEnumSetType> Sub<BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> Sub<BigEnumSet<T>> for BigEnumSet<T>
§type Output = BigEnumSet<T>
type Output = BigEnumSet<T>
-
operator.source§impl<T: BigEnumSetType> Sub<T> for &BigEnumSet<T>
impl<T: BigEnumSetType> Sub<T> for &BigEnumSet<T>
source§impl<T: BigEnumSetType> Sub<T> for BigEnumSet<T>
impl<T: BigEnumSetType> Sub<T> for BigEnumSet<T>
source§impl<T: BigEnumSetType> SubAssign<&BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> SubAssign<&BigEnumSet<T>> for BigEnumSet<T>
source§fn sub_assign(&mut self, other: &BigEnumSet<T>)
fn sub_assign(&mut self, other: &BigEnumSet<T>)
-=
operation. Read moresource§impl<T: BigEnumSetType> SubAssign<BigEnumSet<T>> for BigEnumSet<T>
impl<T: BigEnumSetType> SubAssign<BigEnumSet<T>> for BigEnumSet<T>
source§fn sub_assign(&mut self, other: BigEnumSet<T>)
fn sub_assign(&mut self, other: BigEnumSet<T>)
-=
operation. Read moresource§impl<T: BigEnumSetType> SubAssign<T> for BigEnumSet<T>
impl<T: BigEnumSetType> SubAssign<T> for BigEnumSet<T>
source§fn sub_assign(&mut self, value: T)
fn sub_assign(&mut self, value: T)
-=
operation. Read more