#[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 discriminator of n is stored in the nth least significant bit (corresponding with a mask of, e.g. 1 << enum as u32).

Numeric representation

EnumSet is internally implemented using integer types, and as such can be easily converted from and to numbers.

Each bit of the underlying integer corresponds to at most one particular enum variant. If the corresponding bit for a variant is set, it present in the set. Bits that do not correspond to any variant are always unset.

By default, each enum variant is stored in a bit corresponding to its discriminator. An enum variant with a discriminator of n is stored in the n + 1th least significant bit (corresponding to 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 thris 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.

FFI, Safety and repr

If an enum type T is annotated with #[enumset(repr = "R")], then several things happen:

  • T will implement EnumSetTypeWithRepr<Repr = R> in addition to EnumSetType.
  • The EnumSet methods with repr in their name, such as as_repr and from_repr, will be available for EnumSet<T>.
  • The in-memory representation of EnumSet<T> is guaranteed to be R.

That last guarantee makes it sound to send EnumSet<T> across an FFI boundary. For example:

extern "C" {
    // This function is written in C like:
    // uint32_t some_foreign_function(uint32_t set) { … }
    fn some_foreign_function(set: EnumSet<MyEnum>) -> EnumSet<MyEnum>;
}

#[derive(Debug, EnumSetType)]
#[enumset(repr = "u32")]
enum MyEnum { A, B, C }

let set: EnumSet<MyEnum> = enum_set!(MyEnum::A | MyEnum::C);

let new_set: EnumSet<MyEnum> = unsafe { some_foreign_function(set) };
assert_eq!(new_set, enum_set!(MyEnum::C));

When an EnumSet<T> is received via FFI, all bits that don’t correspond to an enum variant of T must be set to 0. Behavior is undefined if any of these bits are set to 1.

Implementations

Creates an empty EnumSet.

Returns an EnumSet containing a single element.

Creates an empty EnumSet.

This is an alias for EnumSet::new.

Returns an EnumSet containing all valid variants of the enum.

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 })

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 })

Returns the number of elements in this set.

Returns true if the set contains no elements.

Removes all elements from the set.

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

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

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

Returns a set containing any elements present in either set.

Returns a set containing every element present in both sets.

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

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

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

Checks whether this set contains a value.

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.

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

Adds all elements in another set to this one.

Removes all values in another set from this one.

Iterates the contents of the set in order from the least significant bit to the most significant bit.

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

Returns a T::Repr representing the elements of this set.

Unlike the other as_* methods, this method is zero-cost and guaranteed not to fail, panic or truncate any bits.

In order to use this method, the definition of T must have the #[enumset(repr = "…")] annotation.

Constructs a bitset from a T::Repr without checking for invalid bits.

Unlike the other from_* methods, this method is zero-cost and guaranteed not to fail, panic or truncate any bits, provided the conditions under “Safety” are upheld.

In order to use this method, the definition of T must have the #[enumset(repr = "…")] annotation.

Safety

All bits in the provided parameter bits that don’t correspond to an enum variant of T must be set to 0. Behavior is undefined if any of these bits are set to 1.

Constructs a bitset from a T::Repr.

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

In order to use this method, the definition of T must have the #[enumset(repr = "…")] annotation.

Attempts to constructs a bitset from a T::Repr.

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

In order to use this method, the definition of T must have the #[enumset(repr = "…")] annotation.

Constructs a bitset from a T::Repr, ignoring invalid variants.

In order to use this method, the definition of T must have the #[enumset(repr = "…")] annotation.

Returns a u8 representing the elements of this set.

If the underlying bitset will not fit in a u8 or contains bits that do not correspond to an enum variant, this method will panic.

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

If the underlying bitset will not fit in a u8 or contains bits that do not correspond to an enum variant, this method will instead return None.

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 or do not correspond to an enum variant.

Constructs a bitset from a u8.

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

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.

Constructs a bitset from a u8, ignoring invalid variants.

Constructs a bitset from a u8, without checking for invalid bits.

Safety

All bits in the provided parameter bits that don’t correspond to an enum variant of T must be set to 0. Behavior is undefined if any of these bits are set to 1.

Returns a u16 representing the elements of this set.

If the underlying bitset will not fit in a u16 or contains bits that do not correspond to an enum variant, this method will panic.

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

If the underlying bitset will not fit in a u16 or contains bits that do not correspond to an enum variant, this method will instead return None.

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 or do not correspond to an enum variant.

Constructs a bitset from a u16.

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

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.

Constructs a bitset from a u16, ignoring invalid variants.

Constructs a bitset from a u16, without checking for invalid bits.

Safety

All bits in the provided parameter bits that don’t correspond to an enum variant of T must be set to 0. Behavior is undefined if any of these bits are set to 1.

Returns a u32 representing the elements of this set.

If the underlying bitset will not fit in a u32 or contains bits that do not correspond to an enum variant, this method will panic.

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

If the underlying bitset will not fit in a u32 or contains bits that do not correspond to an enum variant, this method will instead return None.

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 or do not correspond to an enum variant.

Constructs a bitset from a u32.

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

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.

Constructs a bitset from a u32, ignoring invalid variants.

Constructs a bitset from a u32, without checking for invalid bits.

Safety

All bits in the provided parameter bits that don’t correspond to an enum variant of T must be set to 0. Behavior is undefined if any of these bits are set to 1.

Returns a u64 representing the elements of this set.

If the underlying bitset will not fit in a u64 or contains bits that do not correspond to an enum variant, this method will panic.

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

If the underlying bitset will not fit in a u64 or contains bits that do not correspond to an enum variant, this method will instead return None.

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 or do not correspond to an enum variant.

Constructs a bitset from a u64.

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

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.

Constructs a bitset from a u64, ignoring invalid variants.

Constructs a bitset from a u64, without checking for invalid bits.

Safety

All bits in the provided parameter bits that don’t correspond to an enum variant of T must be set to 0. Behavior is undefined if any of these bits are set to 1.

Returns a u128 representing the elements of this set.

If the underlying bitset will not fit in a u128 or contains bits that do not correspond to an enum variant, this method will panic.

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

If the underlying bitset will not fit in a u128 or contains bits that do not correspond to an enum variant, this method will instead return None.

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 or do not correspond to an enum variant.

Constructs a bitset from a u128.

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

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.

Constructs a bitset from a u128, ignoring invalid variants.

Constructs a bitset from a u128, without checking for invalid bits.

Safety

All bits in the provided parameter bits that don’t correspond to an enum variant of T must be set to 0. Behavior is undefined if any of these bits are set to 1.

Returns a usize representing the elements of this set.

If the underlying bitset will not fit in a usize or contains bits that do not correspond to an enum variant, this method will panic.

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

If the underlying bitset will not fit in a usize or contains bits that do not correspond to an enum variant, this method will instead return None.

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 or do not correspond to an enum variant.

Constructs a bitset from a usize.

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

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.

Constructs a bitset from a usize, ignoring invalid variants.

Constructs a bitset from a usize, without checking for invalid bits.

Safety

All bits in the provided parameter bits that don’t correspond to an enum variant of T must be set to 0. Behavior is undefined if any of these bits are set to 1.

Trait Implementations

The resulting type after applying the & operator.

Performs the & operation. Read more

Performs the &= operation. Read more

The resulting type after applying the | operator.

Performs the | operation. Read more

Performs the |= operation. Read more

The resulting type after applying the ^ operator.

Performs the ^ operation. Read more

Performs the ^= operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns an empty set.

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Converts to this type from the input type.

Creates a value from an iterator. Read more

Creates a value from an iterator. Read more

Feeds this value into the given Hasher. Read more

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The resulting type after applying the ! operator.

Performs the unary ! operation. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

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

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

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

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

The resulting type after applying the - operator.

Performs the - operation. Read more

Performs the -= operation. Read more

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

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

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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.