Bitmask-Enum
A bitmask enum attribute macro, to turn an enum into a bitmask.
A bitmask can have (un)signed integer types, the default type is usize
.
First created because I wanted something simple, evolved with inspiration from the bitflags crate, which might be something you want to take a look at.
use bitmask;
// usize
// u8
Example
use bitmask;
// It is possible to impl on the bitmask and use its bits field
// bitmask has const bitwise operator methods
const CONST_BM: Bitmask = Flag2.or;
Custom Values
You can assign any flag a custom value.
use bitmask;
Bitmask Config
It is possible to add custom bitmask config options via the #[bitmask_config(...)]
macro. (Just add it below the #[bitmask]
macro)
use bitmask;
Available Config Options
inverted_flags
=> Adds an inverted flag for every non-inverted flag to the bitmask.vec_debug
=> Replaces the default Debug trait implementation with a custom one that prints the bitmask as a vec of all matching values.flags_iter
=> Adds a::flags()
method that returns an iterator over all flags of the bitmask represented as a tuple(name, flag)
.
If you need / can think of any other config option, feel free to suggest them and we can discuss implementing them.
Implemented Methods
// Returns the underlying bits of the bitmask.
const #type;
// Returns an iterator over all flags of the bitmask.
// Where each Item = (name, flag).
//
// This requires the `flags_iter` config option.
;
// Returns a bitmask that contains all values.
//
// This will include bits that do not have any associated flags.
// Use `::all_flags()` if you only want to use flags.
const ;
// Returns `true` if the bitmask contains all values.
//
// This will check for `bits == !0`,
// use `.is_all_flags()` if you only want to check for all flags
const ;
// Returns a bitmask that does not contain any values.
const ;
// Returns `true` if the bitmask does not contain any values.
const ;
// Returns a bitmask that contains all flags.
const ;
// Returns `true` if the bitmask contains all flags.
//
// This will fail if any unused bit is set,
// consider using `.truncate()` first.
const ;
// Returns a bitmask that only has bits corresponding to flags
const ;
// Returns `true` if `self` intersects with any value in `other`,
// or if `other` does not contain any values.
// This is equivalent to `(self & other) != 0 || other == 0`.
const ;
// Returns `true` if `self` contains all values of `other`.
// This is equivalent to `(self & other) == other`.
const ;
// Constant bitwise operations.
const ;
const ;
const ;
const ;
Implemented Traits