lloydmeta / frunk

Funktional generic type-level programming in Rust: HList, Coproduct, Generic, LabelledGeneric, Validated, Monoid and friends.
https://beachape.com/frunk/
MIT License
1.29k stars 58 forks source link

Support deriving LabelledGeneric on enums #158

Closed Diggsey closed 5 years ago

Diggsey commented 5 years ago

(Based off #157)

I reused the Field type to represent named enum variants.

This is just the first step - to make enums actually useful we'll need to:

Another issue I found is the field! macro - the way it generates the field name is inconsistent due to limitations of the macro system (eg. field!((_, _0), ...) has the name __0 when it should be _0). It should be possible to implement it as a procedural macro, so that one can just write field!("_0", ...) and not have to worry about the encoding.

Diggsey commented 5 years ago

(Rebased and reformatted)

lloydmeta commented 5 years ago

Sorry for the delay in reviewing this a bunch of life things came up and I'm only currently catching up with work and other things. Just took an overall look at this PR, and it looks really good ! Will try to find some time over the weekend to give it a detailed review.

Diggsey commented 5 years ago

Yeah, for my own use case, the names of the variants are essential, as I'm using frunk to implement conversions between "similar" but separate data structures, and it allows me to generically convert from one enum to another.

The reason I did this was for two reasons:

However, if you think it would be worth adding all four combinations:

Then that would also work.

My eventual goal is to implement something like "transmogrify" in that it uses the field names, but that can be "driven" by the return type. So instead of saying "map X to whatever F(X) happened to return", you could say "convert X to Y" and it would recursively:

I got very close, (got it working at the struct level) but haven't been able to quite get the inference working for coproducts yet 😢

lloydmeta commented 5 years ago

Awesome stuff ! Thanks for the contribution @Diggsey!