Skip to content

Add Generic support for enum representations#252

Open
davidspies wants to merge 3 commits into
lloydmeta:masterfrom
davidspies:master
Open

Add Generic support for enum representations#252
davidspies wants to merge 3 commits into
lloydmeta:masterfrom
davidspies:master

Conversation

@davidspies

Copy link
Copy Markdown

Summary

This adds enum support to #[derive(Generic)].

Enum Generic representations now mirror the existing LabelledGeneric enum shape, but without labels: each variant maps to an HList payload, and the enum as a whole maps to a Coproduct of those payload HLists. Unit variants use HNil, tuple variants use positional payloads, and named variants use field-order payloads.

For example, this enum:

#[derive(Generic)]
enum Event {
    Ping,
    Login(String, bool),
    Logout { user_id: u64 },
}

gets a representation equivalent to:

type Repr = Coprod!(
    HList!(),
    HList!(String, bool),
    HList!(u64)
);

with values mapping by variant position:

Event::Ping
// => Coproduct::Inl(hlist![])

Event::Login(name, admin)
// => Coproduct::Inr(Coproduct::Inl(hlist![name, admin]))

Event::Logout { user_id }
// => Coproduct::Inr(Coproduct::Inr(Coproduct::Inl(hlist![user_id])))

So each variant payload becomes an HList, and the enum is represented as a Coproduct over those payload lists.

This also adds manual Generic and LabelledGeneric impls for:

  • Option<T>
  • Result<T, E>
  • bool

The std enum impls use the same representation conventions as derived enums. For LabelledGeneric, variant labels are None, Some, Ok, Err, false, and true, with unary tuple payloads using the existing _0 field label convention.

Changes

  • Adds plain enum support to #[derive(Generic)].
  • Reuses proc macro helper machinery for enum variant payload HLists.
  • Adds Generic impls for Option, Result, and bool.
  • Adds LabelledGeneric impls for Option, Result, and bool.
  • Adds tests for enum derive round trips and exact representation shapes.

Testing

Ran:

cargo test --test generic_tests
cargo test --test labelled_tests
cargo test --workspace
cargo check -p frunk_core --no-default-features

Copilot AI review requested due to automatic review settings June 23, 2026 20:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Generic derive by generating Coproduct-based representations.
  • Extend proc-macro helper machinery to build per-variant payload HList types/constructors.
  • Add Generic/LabelledGeneric impls and corresponding tests for Option, Result, and bool.

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.

Comment thread derives/src/lib.rs Outdated
Comment thread derives/src/derive_generic.rs Outdated
@lloydmeta

Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants