Crates.io | bitflag-attr |
lib.rs | bitflag-attr |
version | |
source | src |
created_at | 2024-05-15 10:06:19.940579 |
updated_at | 2024-11-25 08:07:02.207625 |
description | A macro to generate bitflags structures from C-like enums |
homepage | https://github.com/GrayJack/bitflag-attr |
repository | https://github.com/GrayJack/bitflag-attr |
max_upload_size | |
id | 1240857 |
Cargo.toml error: | TOML parse error at line 23, column 1 | 23 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This is a proc-macro Rust crate that allows to turn a C-like enum into a bitflag structures with an API similar to bitfields
crate.
You can use this crate to:
You can't use this crate to:
bitflag-attr
allows access to the underlying bits type so arbitrary bits may be set.bitflag-attr
only generates types where set bits denote the presence of some combination of flags.The macro will also implement some traits for bitwise operations and formatting.
no_auto_debug
)Besides the Debug
, Clone
and Copy
traits, all other standard derivable traits can be used together with the type.
The macro also generate iterator types to iterate over the set flags, and for convenience also implement the following traits:
There is a opt-in crate feature serde
that generate a parsing error type and implements the traits:
Note: This crate does not import/re-export serde traits, your project MUST have serde
as dependency.
Generate a flags structure:
use bitflag_attr::bitflag;
#[bitflag(u32)]
#[derive(PartialEq, PartialOrd, Eq, Ord, Hash)]
enum Flags {
/// The value `A`, at bit position `0`.
A = 0b00000001,
/// The value `B`, at bit position `1`.
B = 0b00000010,
/// The value `C`, at bit position `2`.
C = 0b00000100,
/// The combination of `A`, `B`, and `C`.
ABC = A | B | C,
}
fn main() {
let e1 = Flags::A | Flags::C;
let e2 = Flags::B | Flags::C;
assert_eq!((e1 | e2), Flags::ABC); // union
assert_eq!((e1 & e2), Flags::C); // intersection
assert_eq!((e1 - e2), Flags::A); // set difference
assert_eq!(!e2, Flags::A); // set complement
}
If you don't want Debug
trait to be generated, you can pass no_auto_debug
to the attribute.
use bitflag_attr::bitflag;
#[bitflag(u32, no_auto_debug)]
#[derive(PartialEq, PartialOrd, Eq, Ord, Hash)]
enum Flags {
/// The value `A`, at bit position `0`.
A = 0b00000001,
/// The value `B`, at bit position `1`.
B = 0b00000010,
/// The value `C`, at bit position `2`.
C = 0b00000100,
/// The combination of `A`, `B`, and `C`.
ABC = A | B | C,
}
The minimum supported Rust version is documented in the Cargo.toml
file.
This may be bumped in minor releases as necessary.