Robbepop / modular-bitfield

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

Complex enums #70

Open ethindp opened 2 years ago

ethindp commented 2 years ago

I am writing a crate to define data structures for the USB specifications and the device class definitions that go along with them. USB contains requests, which are 8-byte structures stored in host memory and transmitted to the bus via a host controller interface (HCI) like xHCI. The fields for the requests are:

The problem is that request codes overlap with each other depending on the type of request (e.g. if its a standard request, then its any of the requests defined in table 9-4 of the USB spec; if its class-specific then its a class-specific code; etc.). An example is a CLEAR_FEATURE request (a standard request with code 0x01) and SET_MIN (a USB audio class-specific request with the same code). Rust does not allow me to define two enumeration discriminants with the same value, but this crate requires me to not use discriminants with variants, so I can't simply define an enumeration variant per request type, and then have sub-variants for each class, for example, to overcome this limitation. Is there an alternative to solve this problem? I could define multiple request types (one per class for example) but that seems wasteful.

Qyriad commented 2 years ago

As someone who's familiar with USB: I recommend you make enums for different kinds of requests (standard versus class) different types. In fact I actually recommend you make a different enum for every class.

ethindp commented 2 years ago

@Qyriad This was also what I was thinking. However, I cannot then encapsulate them as a central "request" type. My only alternative would be to make separate request types per class, which violates the DRY principal and is something I hesitate to do because I don't want to deal with duplicate code. As such I am seeking an alternative solution that will allow me the freedom of this crate but will also make it possible to centralize the master "request" type, and only make custom request types when a class calls for it.

hecatia-elegua commented 1 year ago

See #89 + https://github.com/illicitonion/num_enum/issues/81 and now I'm also working on this.