A rust crate which tries to create an experience as close to traditional bit-flag/bitfield value enum ergonomics with as little boilerplate as possible.
u8, i128, etc.)READ | WRITE & !EXECUTE) as well as with the underlying representation
type (i.e. READ & !1).with()) and filtering
(.without()).fmt::Debug implementationAdd the crate as a dependency: ~~~console cargo add classic-bitfield ~~~
```rust
pub(crate) enum Permissions { /// Permission to run executables or list directories EXECUTE, /// Permssion to write to the file WRITE, /// Permission to read to the file READ, /// COMBO #[repr(0o6)] READANDWRITE, }
fn main() {
let value = Permissions::allset();
assert!(value.hasexecute());
assert!(!value.hasreadandwrite());
let value = value.without(Permissions::EXECUTE);
assert!(!value.hasexecute());
assert!(value.haswrite());
}
``
With--features=serde(requiresserde; example requiresserdejsonand
serde's"derive"` feature)
```rust use std::io::stdout;
use classicbitfield::bitfieldenum; use serde::{Deserialize, Serialize};
pub(crate) enum Permissions { /// Permission to run executables or list directories EXECUTE, /// Permssion to write to the file WRITE, /// Permission to read to the file READ, /// COMBO #[repr(0o6)] READANDWRITE, }
use permissionsserde::numericrepresentation;
struct FileMetadata { #[serde(with = "numeric_representation")] permissions: Permissions, }
fn main() {
let stdout = stdout().lock();
let example = FileMetadata {
permissions: Permissions::READANDWRITE,
};
serdejson::towriter_pretty(stdout, &example).unwrap();
println!();
}
The output from the above example is:
json
{
"permissions": 6
}
```
To get an idea of what features will be available on your generated type, take a look at the tests.
CamelCase like you would a regular enum. This is appropriate and desirable
— bitfield variants are not distinct types, they are constants, and
styling them this way ensures that fact is kept in mind.