Add Generic support for enum representations#252
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends #[derive(Generic)] to support enums by representing each variant payload as an HList and the enum as a Coproduct of those payload lists. It also adds built-in Generic/LabelledGeneric impls for common std enums (Option, Result) and for bool, along with tests that validate both round-trips and exact representation shapes.
Changes:
- Add enum support to the
Genericderive by generatingCoproduct-based representations. - Extend proc-macro helper machinery to build per-variant payload
HListtypes/constructors. - Add
Generic/LabelledGenericimpls and corresponding tests forOption,Result, andbool.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
tests/labelled_tests.rs |
Adds LabelledGeneric tests for Option, Result, and bool representations and round-trips. |
tests/generic_tests.rs |
Adds enum Generic tests plus Generic tests for Option, Result, and bool. |
tests/common/mod.rs |
Introduces a GenericEnum test type used by the new enum Generic tests. |
proc-macro-helpers/src/lib.rs |
Adds helper methods to generate per-variant payload HList types/constructors for enum support. |
derives/src/lib.rs |
Updates the Generic derive doc comment to include enums (but wording needs a small correction). |
derives/src/derive_generic.rs |
Implements enum codegen for Generic derive using Coproduct of payload HLists (doc wording needs a small correction). |
core/src/labelled.rs |
Adds LabelledGeneric impls for Option, Result, and bool following the variant-as-field conventions. |
core/src/generic.rs |
Adds Generic impls for Option, Result, and bool using Coproduct representations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Very neat. Thanks for the PR. On first glance, my gut is saying this makes sense. Will have a closer look later when I have some more time hopefully within 2 weeks. |
lloydmeta
left a comment
There was a problem hiding this comment.
LGTM. I'll bump the minor version when releasing this just to be safe around the new manual implementations.
Summary
This adds enum support to
#[derive(Generic)].Enum
Genericrepresentations now mirror the existingLabelledGenericenum shape, but without labels: each variant maps to anHListpayload, and the enum as a whole maps to aCoproductof those payloadHLists. Unit variants useHNil, tuple variants use positional payloads, and named variants use field-order payloads.For example, this enum:
gets a representation equivalent to:
with values mapping by variant position:
So each variant payload becomes an
HList, and the enum is represented as aCoproductover those payload lists.This also adds manual
GenericandLabelledGenericimpls for:Option<T>Result<T, E>boolThe std enum impls use the same representation conventions as derived enums. For
LabelledGeneric, variant labels areNone,Some,Ok,Err,false, andtrue, with unary tuple payloads using the existing_0field label convention.Changes
#[derive(Generic)].HLists.Genericimpls forOption,Result, andbool.LabelledGenericimpls forOption,Result, andbool.Testing
Ran: