| Crates.io | fielder |
| lib.rs | fielder |
| version | 0.2.0 |
| created_at | 2025-01-09 23:32:24.887684+00 |
| updated_at | 2025-01-13 03:41:06.321688+00 |
| description | Efficient and precise bitfields |
| homepage | |
| repository | https://github.com/jack-weilage/fielder |
| max_upload_size | |
| id | 1510731 |
| size | 11,981 |
fielderfielder provides a single macro (bitfield) which (like
bitflags) allows for defining complex structures
packed into only a few bytes. These structures differ from those generated by similar crates by
allowing significantly more complex fields to be defined. These fields can span multiple bits,
can act as a simple counter, and behave like you'd expect. Everything is no_std and no_alloc,
making fielder a perfect fit for embedded protocols.
use fielder::bitfield;
bitfield! {
// This bitfield uses a `u8` under the hood.
struct ComplexField: u8 {
// Fields only spanning a single bit will act like flags.
FirstFlag: 0;
// Fields can span multiple bits.
SomeField: 1..2 = 0;
SecondField: 1..2 = 1;
ThirdField: 1..2 = 2;
FourthField: 1..2 = 3;
// This field will be used in combination with `get_literal` to retrieve the literal
// value of the contained bits.
CounterField: 3..7 = 0;
}
}
// Fields are constructed from an integer via `from_bits`.
let field = ComplexField::from_bits(0b01010_11_0);
// The first bit (FirstFlag) isn't set.
assert!(!field.contains(ComplexField::FirstFlag));
// The second and third bits are set, so FourthField is set.
assert!(field.contains(ComplexField::FourthField));
// Importantly, even though its bit is set, SecondField isn't set.
assert!(!field.contains(ComplexField::SecondField));
// The current literal value of CounterField can be read.
assert_eq!(field.get_literal(ComplexField::CounterField), 0b01010);