🚧 Warning: Experimental Crate! 🚧
This crate is currently in beta and experimental. It is subject to breaking changes in future releases. Use it at your own risk, and keep in mind that the API may change in future versions.
Hexga BitFlags
A bitflags crate, mainly inspired by enumflags2.
Provide a #[bitindex] attribute macro that can be applied to enum X to automatically generate a corresponding bitflags struct XFlags and implement various bitwise operations, conversions, and utility methods for working with sets of enum variants as bitflags.
The #[bitindex] macro interprets each enum variant as defining the position of a bit in the generated flags type.
Features
-
Automatic Bitflag Generation: Annotate your enum
Xwith#[bitindex]to generate a correspondingXFlagsstructure with the associated constant. -
Custom Discriminant Value: Supports enums with explicit discriminants and non-contiguous values.
-
Type Safe Flags: The
Flagsstructure contains only valid bit. Unusedenumbit index will always be zero, when when calling not!flags. -
Common Binary Operations: Use
|,&,^, and!operators directly on enum variants and the bitflags structure. -
Iteration: iterate over the enabled bits or retrieve enum variants from a flag set.
-
Serde Support: Optional serialization/deserialization via the
serdefeature. -
Simple API: Includes methods for insertion, remove, toggling bits...
Example
use *;
The generated code behind #[bitindex] will look like:
// The derive are also generated by `#[bitindex]`
+ some other methods and trait implementations.
Inspiration & Motivation
This crate was mainly inspired by
-
enumflags2 while also wanting a way to defined all the constant and constant combination inside the flags structure.
-
Also check bitflags if you are looking for a popular crate for defining bitflags (without enum).