Lymia / enumset

A library for compact bit sets containing enums.
Apache License 2.0
91 stars 35 forks source link

Add `repr` derive option, to control `EnumSetTypePrivate::Repr` #37

Closed ahcodedthat closed 2 years ago

ahcodedthat commented 2 years ago

This PR adds the annotation #[enumset(repr = "u8")] to specify the in-memory representation of EnumSet.

This has several effects:

I expect this will be useful for FFI and serialization.

My use-case is using EnumSet with the prost library. prost can't generate structs with custom types like EnumSet, only standard types like u32. To use EnumSet with it, I have to convert the EnumSet to u32, then insert that into the struct field. With #[enumset(repr = "u32")], the derive macro will verify at compile time that all of my enum variants will actually fit into the struct field, instead of only finding out at run time when EnumSet::as_u32 panics.