Robbepop / modular-bitfield

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

Bitfields with algebraic data types #78

Open danielkeller opened 3 years ago

danielkeller commented 3 years ago

I've come up with a basic implementation of #[derive(BitfieldSpecifier)] that works on struct-like enum variants, that is, algebraic data types. It stores a discriminant, which determines the enum variant and the format of the rest of the data.

In terms of API, storing this kind of enum in a bitfield makes the bitfield's getter return the enum type as written. The [derive()] also allows the user to choose the bit pattern of the discriminants by supplying a second, plain enum with members of the same names (specifying them in original enum isn't stable syntax).

One limitation is that the discriminant bits are always before the data.

What do you think?

(I admit that the code isn't the cleanest, I'm pretty new to proc macros. I also had some issues with formatting differences in rustfmt and compiler errors in the test, I'm not sure if it's because of a different compiler version or what.)

vhdirk commented 1 year ago

This is pretty cool. I know I'm terribly late to the party, but this would be super handy for my use case.