5 releases

0.2.0 Jan 13, 2025
0.1.3 Jan 10, 2025
0.1.2 Jan 10, 2025
0.1.1 Jan 10, 2025
0.1.0 Jan 9, 2025

#113 in No standard library

Download history 279/week @ 2025-01-06 90/week @ 2025-01-13

369 downloads per month

MIT/Apache

7KB

fielder

fielder 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.

Example

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);

Dependencies

~220–660KB
~16K SLoC