This crate provides derive to generate struct with bitfield access.
```rust use bitfield_derive::BitFields;
struct Foo { #[bitfield(bar : [3:0] as u8 "The bar flags")] #[bitfield(baz : [7:4] as u8 "The baz flags")] #[bitfield(ro, _ : [8] as bool)] // Read only #[bitfield(_ , setwr : [9] as bool)] // Write only #[bitfield(stuff : [31:16] as u16)] #[bitfield(allbits : [31:0])] _bi1: u32, other: usize, }
// Instance a struct with bitfields. let mut foo = Foo::default();
// Initial states. asserteq!(foo.bar(), 0); asserteq!(foo.baz(), 0);
foo.setbar(7); foo.setbaz(3); asserteq!(foo.bar(), 7); asserteq!(foo.baz(), 3);
// Overflowing tests. foo.setbar(0x13); foo.setbaz(0x17); asserteq!(foo.bar(), 3); asserteq!(foo.baz(), 7);
foo.setbar(0x0f); foo.setbaz(0x0f); asserteq!(foo.bar(), 0x0f); asserteq!(foo.baz(), 0x0f);
asserteq!(foo.ro(), false); // Compile fail if uncomment the follow line. // foo.setro(false);
// Compile fail if uncomment the follow line. // asserteq!(foo.wr(), 0); foo.setwr(true);
asserteq!(foo.stuff(), 0); foo.setstuff(0xffff); assert_eq!(foo.stuff(), 0xffff);
// All bits in the container field. asserteq!(foo.allbits(), 0xffff02ff); foo.setallbits(0); asserteq!(foo.all_bits(), 0); ```
The visibility of the bitfield follows the container field.
Examples:
```rust use bitfield_derive::BitFields;
pub struct Foo { #[bitfield(bar : [3:0] as u8 "The bar flags")] pub _bi1: u32; #[bitfield(baz : [3:0] as u8 "The baz flags")] _bi2: u32; } ```
The container field Foo::_bi1
is pub
, so the Foo::bar() and Foo::set_bar()
are implements as:
rust
impl Foo {
pub fn bar(&self) -> u8 { ... }
pub fn set_bar(&mut self, value: u8) { ... }
}
The container field Foo::_bi2
is private
, so the Foo::baz() and Foo::set_baz()
are implements as:
rust
impl Foo {
fn baz(&self) -> u8 { ... }
fn set_baz(&mut self, value: u8) { ... }
}
The last parameter of the bitfield is the description of the getter
and setter
.
The document generated by the above example is as follows:
```rust [-] pub(crate) fn bar(&self) -> u8 The bar flags
[-] pub(crate) fn set_bar(&mut self, value: u8) See also: bar
[-] pub(crate) fn baz(&self) -> u8 The baz flags
[-] pub(crate) fn set_baz(&mut self, value: u8) See also: baz
... ```