bitf

Rust procedural macro to quickly generate bitfield from a structure.

Usage

The macro can be used as following: ```text

[bitf(size, order)]

Where size can be: u8 u16 u32 u64 u128

And order can be lsb or msb ``` The size parameter will constrain the total size of the bitfield. The order parameter will alter the order in which the fields are declared. When setting the order parameter to msb, the first declared field of the struct will be set on the most significant bit, and the other way around when using the lsb mode.

Thus, the size and position of the field is based on the field declaration : ```rust use bitf::bitf;

[bitf(u8,lsb)]

struct Example { anycasename2: (), // () is used as a bogus type // will be replaced in the near future // by a more flexible syntax nameB_2: (), }

// The internal, full value of the field can be accessed as :

let e = Example::default(); println!("{}", e.raw);

```

Example

Considering the following bitfield:

```text 7 0 0 0 0 0 0 0 0 0 | | | | | | | |_ fielda - Size 1 | | | | | | | fieldB - Size 1 | | | | | |_ fieldC - Size 1 | | \|/_ reserved - Size 3 \ /___ fieldD - Size 2

```
It can be achieved with the following declaration and macro usage

```rust use bitf::bitf;

[bitf(u8, lsb)]

struct MyStruct { fielda1: (), fieldB1: (), FieldC1: (), reserved3: (), FieldD_2: (), } ```

This will generate the following structure and associated methods

```rust struct MyStruct { pub raw: u8, }

impl MyStruct { pub fn fielda(self: &Self) -> u8 { /* bitwise logic */ 0 } pub fn setfielda(self: &Self, val: u8) { /* bitwise logic */ } pub fn fieldB(self: &Self) -> u8 { /* bitwise logic */ 0 } pub fn setfieldB(self: &Self, val: u8) { /* bitwise logic / } / * And so on... */

}

impl Default for MyStruct { fn default() -> Self { MyStruct { raw: 0x0 } } }

//So you can easily set and read values of each defined bitfield:

let mut bf = MyStruct::default();

bf.setfielda(1); bf.setfieldB(1); println!("{:#010b}", bf.fielda());

```

TODO