#enums #frunk #generics #labelled-generic #data #extension

frunk-enum-core

Implemenation of genericized enums for use with frunk

3 unstable releases

0.3.0 Nov 15, 2023
0.2.1 Mar 3, 2021
0.2.0 May 13, 2019

#1039 in Data structures

Download history 449/week @ 2024-07-28 513/week @ 2024-08-04 440/week @ 2024-08-11 544/week @ 2024-08-18 505/week @ 2024-08-25 524/week @ 2024-09-01 639/week @ 2024-09-08 497/week @ 2024-09-15 710/week @ 2024-09-22 607/week @ 2024-09-29 332/week @ 2024-10-06 394/week @ 2024-10-13 431/week @ 2024-10-20 565/week @ 2024-10-27 358/week @ 2024-11-03 378/week @ 2024-11-10

1,746 downloads per month
Used in 19 crates

MIT/Apache

5KB
55 lines

Frunk Enum Support

Crates.io - frunk-enum License: MIT/APACHE-2.0

Usage

These crates augment the frunk crate to allow transmogrification of enums as well as structs. For more information on transmogrification and the LabelledGeneric trait it's based around, see https://docs.rs/frunk/latest/frunk/#transmogrifying and https://docs.rs/frunk/latest/frunk/labelled/trait.LabelledGeneric.html.

To take advantage of this feature for your own enum you need to:

  • Add the frunk-enum-derive as a [dev-dependency] to your Cargo.toml

  • Mark the enum with the custom derive:

    #[derive(LabelledGenericEnum)]
    enum Foo {
        Bar(String),
        Baz { name: String, id: Identity },
    }
    
  • Add the frunk-enum-core and frunk-core crates as [dependency] entries to your Cargo.toml

  • Then (assuming there's a NewFoo enum with the same structure as Foo) you can write:

    let foo = Foo::Baz { name: "Andy".into(), id: Identity };
    let new_foo: NewFoo = foo.transmogrify();
    

This works by deriving an implementation of LabelledGeneric for Foo which allows conversion to and from an instance of a generic sum-type. The core crate provides tools for converting between these generic sum-types where the bodies of the variants are recursively transmogrifiable. This allows for arbitrarily deep conversion between types, especially useful where the two types in question are autogenerated from some common input file, but are deemed to be different types by the rust compiler (e.g. because they're in separate crates).

Contributing

Thank you for your interest in frunk-enum, all contributions are welcome whether they be reports of issues, feature requests or code contributions.

Issue Contribution

When opening issues please provide a description of the issue, the version of frunk and frunk-enum plus example code which exhibits the issue.

Code Contribution

If you wish to contribute code to frunk-enum, you are welcome to do so using a github pull request, however you will need to sign each commit, to indicate that you are abiding to the terms of the DCO.


lib.rs:

This crate augments the frunk crate with extra functionallity to allow transmogrification between enums with similar structures.

This crate specifically defines the data structures used in the generic representation of an enum, the frunk-enum-derive crate adds a proc-macro to auto-generate the per-type code needed to take advantage of this core function.

Dependencies

~0.5–1MB
~21K SLoC