Expand description
A library for defining enums that can be used in compact bit sets. It supports arbitrarily large enums, and has very basic support for using them in constants.
§Cargo Features
The following cargo features are available for this crate:
serde
: Allows serialization and deserialization of the types in this crate.alloc
: Enables the use of functions that requires an allocator.proc-macro-crate
: Enable the use of theproc-macro-crate
crate to allow the renaming of theenumset
crate in your user crate. This feature increases the MSRV to 1.69.0
§Defining enums for use with EnumSet
Enums to be used with EnumSet
should be defined using #[derive(EnumSetType)]
:
#[derive(EnumSetType, Debug)]
pub enum Enum {
A, B, C, D, E, F, G,
}
For more information on more advanced use cases, see the documentation for
#[derive(EnumSetType)]
.
§Working with EnumSets
EnumSets can be constructed via EnumSet::new()
like a normal set. In addition,
#[derive(EnumSetType)]
creates operator overloads that allow you to create EnumSets like so:
let new_set = Enum::A | Enum::C | Enum::G;
assert_eq!(new_set.len(), 3);
All bitwise operations you would expect to work on bitsets also work on both EnumSets and
enums with #[derive(EnumSetType)]
:
// Intersection of sets
assert_eq!((Enum::A | Enum::B) & Enum::C, EnumSet::empty());
assert_eq!((Enum::A | Enum::B) & Enum::A, Enum::A);
assert_eq!(Enum::A & Enum::B, EnumSet::empty());
// Symmetric difference of sets
assert_eq!((Enum::A | Enum::B) ^ (Enum::B | Enum::C), Enum::A | Enum::C);
assert_eq!(Enum::A ^ Enum::C, Enum::A | Enum::C);
// Difference of sets
assert_eq!((Enum::A | Enum::B | Enum::C) - Enum::B, Enum::A | Enum::C);
// Complement of sets
assert_eq!(!(Enum::E | Enum::G), Enum::A | Enum::B | Enum::C | Enum::D | Enum::F);
The enum_set!
macro allows you to create EnumSets in constant contexts:
const CONST_SET: EnumSet<Enum> = enum_set!(Enum::A | Enum::B);
assert_eq!(CONST_SET, Enum::A | Enum::B);
Mutable operations on the EnumSet
otherwise similarly to Rust’s builtin sets:
let mut set = EnumSet::new();
set.insert(Enum::A);
set.insert_all(Enum::E | Enum::G);
assert!(set.contains(Enum::A));
assert!(!set.contains(Enum::B));
assert_eq!(set, Enum::A | Enum::E | Enum::G);
Macros§
- enum_
set - Creates a EnumSet literal, which can be used in const contexts.
- enum_
set_ complement - Computes the complement of an enums or constants enumset at compile time.
- enum_
set_ difference - Computes the difference of multiple enums or constants enumset at compile time.
- enum_
set_ intersection - Computes the intersection of multiple enums or constants enumset at compile time.
- enum_
set_ symmetric_ difference - Computes the symmetric difference of multiple enums or constants enumset at compile time.
- enum_
set_ union - Computes the union of multiple enums or constants enumset at compile time.
Structs§
- EnumSet
- An efficient set type for enums.
- Enum
SetIter - The iterator used by
EnumSet
s.
Traits§
- Enum
SetType - The trait used to define enum types that may be used with
EnumSet
. - Enum
SetType With Repr - An
EnumSetType
for whichEnumSet
s have a guaranteed in-memory representation.
Derive Macros§
- Enum
SetType - The procedural macro used to derive
EnumSetType
, and allow enums to be used withEnumSet
.