EnumBitFlags
A proc-macro crate for Rust that allows creating bit flags enums.
How to use:
- First you need to this crate to your
cargo.toml
file:
[]
= "0.1.0"
- Then, you can use it in your Rust project like this:
Arguments
EnumBitFlags supports various arguments that provide additional information on how to build the enum. Arguments are specified in the EnumBitFlags
arguments with the following format: key=value,key=value,...
. Alternativelly, you can use :
instead of =
(key:value, key:value....
)
bits
In-memory representation of the bitfield. It could be one of the following:8
,16
,32
,64
or128
. If not specified the default value is32
. Exampleempty
The name of the empty variant. An empty variant is the case where not bits are being set up. If not specified, it willNone
will be generated. The name of the empty variant must NOT be present in the enum variants and must start with a letter or underline character and can contain letters, numbers and the underline character. Example
Methods
Every EnumBitFlags has several methods that can be used to easily manipulate and chek bits status:
Method | Description |
---|---|
obj.contains(mask) | Returns true if all set bits from the mask are present in the object, or false otherwise |
obj.contains_one(mask) | Returns true if at least one bit from the mask is present in the object, or false otherwise |
obj.clear() | Clears all bits from the current object |
obj.is_empty() | Returns true if not bits are set, false otherwise |
obj.remove(mask) | Removes all set flags from the mask |
obj.set(mask) | Set all bits from the mask |
-
contains
Checks if an exact bitflag mask is presentThe obj must not be empty (at least one bit has to be set) and all bits from the object must be present. Example:
-
contains_one
Checks if at least one bit from the mask is present in the objectThe obj must not be empty (at least one bit has to be set) . Example:
-
clear
Clears all bits from the enumExample: