kupiakos / open-enum

Opens up a Rust enum to allow for non-specified values
Apache License 2.0
16 stars 6 forks source link

Use custom `derive` macros for all derive functionality #22

Open kupiakos opened 2 months ago

kupiakos commented 2 months ago

This would replace #11 and #12.

The current detection of Debug can be kept as an ergonomic bonus, since std/3rd party derives using the variants of an open enum are unlikely to work correctly.

Design

The #[open_enum] macro declares an #[open_enum_variants = { Name0 = 0, Name1 = VALUE1, ... }] inert attribute on the emitted struct. This attribute is consumed by the derive macros declared in open_enum, so most will work whether the derive macro is interpreted before or after #[open_enum].

As done today, PartialEq and Eq are always implemented on open enums so they work in match.

These common derive macros work without interception:

derive macros to consider, including additional nice-to-haves:

Size optimization

Performing the above operations at const time if possible is strongly preferred since it can perform these optimization with nonliteral discriminants.

kupiakos commented 2 months ago

Alternative design: #[open_enum] implements an EnumInfo trait that contains the necessary info to implement everything.