Lymia / enumset

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

Add support for enums where variants are directly bit masks for FFI purposes. #32

Open Lymia opened 2 years ago

rbtcollins commented 3 months ago

This would also be very useful for wire protocol formats - and relatedly LSB/MSB could also be useful (though probably lower layers of protocol stacks would take care of that concern)

It would be nice to be able to do something like

#[derive(EnumSetType, Debug)]
#[enumset(repr = "u16", serialize_repr = "u16")]
pub enum EventFeatures {
    Reserved = 0x08,
    EndOfFile = 0x10,
    // ...
}

etc.

Right now, such a definition will error with

`repr` is too small to contain the largest discriminant.
`repr` is too small to contain the largest discriminant.

even though u8, let alone u16, should fit for both in-memory and serde serialisation.

The benefit of this is that other code which isn't operating off of the set - e.g. something detecting end of file could directly use the discriminant:

flags = EventFeatures::EndOfFile as u16;

rather than today:

flags = enum_set! {EventFeatures::EndOfFile}.as_repr();