Skip to content

0.7.0

Compare
Choose a tag to compare
@wrenger wrenger released this 07 Jun 12:45
· 15 commits to main since this release

This release adds a new way to specify the memory layout of the bitfield via the repr attribute. This makes it possible to specify the endianness for the whole bitfield as shown below.

use endian_num::be16;

#[bitfield(u16, repr = be16, from = be16::from_ne, into = be16::to_ne)]
struct MyBeBitfield {
    #[bits(4)]
    first_nibble: u8,
    #[bits(12)]
    other: u16,
}

let my_be_bitfield = MyBeBitfield::new()
    .with_first_nibble(0x1)
    .with_other(0x234);
assert_eq!(my_be_bitfield.into_bits().to_be_bytes(), [0x23, 0x41]);

Here, the Bitfield is represented as an be16 integer in memory. All the conversion is done automatically by calling from and into.

Thanks to @mkroening for implementing and documenting this feature (#39, #41).