#macro #enum #derive #proc-macro #deriving

macro enum-kinds

Generate enums with matching variants but without any of the associated data

4 releases

0.4.1 Aug 22, 2018
0.4.0 Mar 10, 2018
0.3.1 Mar 7, 2018
0.3.0 Mar 6, 2018

#373 in Rust patterns

Download history 29/week @ 2019-12-02 68/week @ 2019-12-09 30/week @ 2019-12-16 40/week @ 2019-12-23 48/week @ 2019-12-30 71/week @ 2020-01-06 42/week @ 2020-01-13 16/week @ 2020-01-20 10/week @ 2020-01-27 11/week @ 2020-02-03 21/week @ 2020-02-10 18/week @ 2020-02-17 30/week @ 2020-02-24 92/week @ 2020-03-02 341/week @ 2020-03-09 69/week @ 2020-03-16

140 downloads per month
Used in 3 crates

MIT license

12KB
167 lines

enum-kinds

Build Status Latest Version Rust Documentation

Custom derive for generating enums with matching variants but without any of the data.

In other words, enum-kinds automatically generates enums that have the same set of variants as the original enum, but with all the embedded data stripped away (that is, all the variants of the newly generated enum are unit variants). Additionally, enum-kinds implements From trait for going from the original enum to the unit variant version.

The crate is compatible with stable Rust releases. This crate replaces earlier enum_kinds_macros and enum_kinds_traits crates.

Example

#[macro_use]
extern crate enum_kinds;

#[derive(EnumKind)]
#[enum_kind(SomeEnumKind)]
enum SomeEnum {
    First(String, u32),
    Second(char),
    Third
}

#[test]
fn test_enum_kind() {
    let first = SomeEnum::First("Example".to_owned(), 32);
    assert_eq!(SomeEnumKind::from(&first), SomeEnumKind::First);
}

The #[derive(EnumKind)] attribute automatically creates another enum named SomeEnumKind that contains matching unit variant for each of the variants in SomeEnum.

Additional Traits for Generated Enums

By default, derived kind enums implement Debug, Clone, Copy, PartialEq and Eq traits. Additional derives can be specified by passing derive specifier to the enum_kind attribute: #[enum_kind(NAME, derive(TRAIT, ...))]. For example, to implement Serde's Serialize and Deserialize traits:

#[macro_use]
extern crate enum_kinds;

#[macro_use]
extern crate serde_derive;
extern crate serde;

#[derive(EnumKind)]
#[enum_kind(AdditionalDerivesKind, derive(Serialize, Deserialize))]
enum AdditionalDerives {
    Variant(String, u32),
    Another(String)
}

no_std support

enum-kinds can be used without the standard library by enabling no-stdlib feature.

Issues

While the enum-kinds is fairly simple, issues are still possible. If you encounter any problems using the crate, please report them at the issue tracker.

License

The crate is available under the terms of MIT license.

Dependencies

~1.5MB
~30K SLoC