enumset icon indicating copy to clipboard operation
enumset copied to clipboard

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

Open Lymia opened this issue 3 years ago • 1 comments

Lymia avatar Apr 04 '22 09:04 Lymia

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();

rbtcollins avatar Jul 14 '24 11:07 rbtcollins

This has been implemented in main via the #[enumset(map = "mask")] attribute. Also added #[enumset(map = "msb")] since it's fairly easy with the same code.

Lymia avatar Aug 16 '25 11:08 Lymia