Turn combinations of bit and byte fields into a structs, even if they represent Options and Lists.
Difference to bit-field crates
Let's jump directly into an example. You want to map a zigbee Link Status Command to a high level rust struct. The frame's shape changes depending on some bits.
This is the spec for the frame:
Bit: | 0 – 4 | 5 | 6 | 7 | 8 -
| List length | First frame | Last frame | Reserved | Link status list
Each link status is:
Bit: | 0 – 15 | 16-18 | 19 | 20-22 | 23
| Neighbor network address | Incoming cost | Reserved | Outgoing cost | Reserved
Its especially tricky that an earlier bitfield is determining the list length. Not even a hacky combination of serde and a bitfield crate can generate (de)-serialize code for us.
Which is why we now have abstract-bits!
use ;
Usage
With a struct
- Add
#[abstract-bits]above your struct and any derives. - Use
u<n>(na natural number larger than zero) for numeric fields. In the transformed struct these will transform to the smallest rust primitives that can represent them. For example anu7will become anu8. - Add padding (if needed) in between fields using
reserved = u<n>. - For each
Optionfield place#[abstract-bits(presence_of = <field_name>)]above thereserved: boolfields which controls whether theOptionisSomeorNone. - For each
Vecfield place#[abstract-bits(length_of = <field_name>)]above thereserved: u<n>fields which controls the length of theVec.
With an enum
- Add
#[abstract-bits(bits = <N>)]above your enum. ReplaceNwith the number of bits the enum should occupy when serialized. Make sure any derives follow after. - Explicitly assign every variant a value.
- Add a
#[repr(<Type>]attribute, for example#[repr(u8)].
Complex example
use ;
// The size of this is:
// - 4+1+5+2+2|0+n*18, with n in range 0..u5::MAX
// so this is at most 14 + 31*18 = 572 bits long
// note: derives follow after
/// This is: 4+3+1+10 = 18 bits long
Planned features
no-std&no-allocsupport (quite trivial)- Support algebraic data-types other than Option (already supported)
Possible features
HashMap/HashSet/BtreeMapsupport