Add Generic support for enum representations#252
Open
davidspies wants to merge 3 commits into
Open
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.
Owner
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: