Robbepop / modular-bitfield

Macro to generate bitfields for structs that allow for modular use of enums.
Apache License 2.0
159 stars 41 forks source link

Add `filled: bool` parameter to #[bitfield] proc. macro #35

Closed Robbepop closed 3 years ago

Robbepop commented 3 years ago

The proposed filled: bool parameter for the #[bitfield] proc. macro allows to have bitfields with bit widths that are not divisible by 8 and therefore might contain invalid bit patterns that are inaccessible from the outside. E.g. creating a #[bitfield(filled = false)] type with only 5 bits will leave 3 bits completely undefined.

For those #[bitfield] structs we no longer can define the byte conversion routines as infallible operations. Instead they need to return Result and check whether the undefined bits are populated.

Not break the world

In this vein we will declare #[bitfield(specifier = true)] structs to be implicitly filled = false with the same effects. Having #[bitfield(specifier = true, filled = true)] is only valid in case the bitfield struct has a bit width that is divisible by 8.

For a future release we might rethink this approach and instead no longer make #[bitfield(specifier = true)] implicitly be filled = false with all the implied consequences.

Robbepop commented 3 years ago

Implemented in https://github.com/Robbepop/modular-bitfield/pull/37. Closed.