pizzamig / enumacro

Extend your Enum with those macros

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

enumacro

build-badge Dependency Status

Educational project to learn using macros (with enum, for now)

#[derive(EDefault)]

Inspired by the enum_default crate, this derive macro will automagically implement the Default trait for an Enum. A simple example, where the first variant is the default one:

    use enumacro::EDefault;

    #[derive(EDefault, PartialEq, Debug)]
    enum MyEnum {
        One,
        Two,
    };
    assert_eq!(MyEnum::default(), MyEnum::One);
    assert_ne!(MyEnum::default(), MyEnum::Two);

A more complex example, with generics, unnamed fields and the attribute edefault to specify the default variant:

    use enumacro::EDefault;

    #[derive(EDefault, PartialEq, Debug)]
    enum MyEnum<T, V, Z> {
        One(T, Z),
        #[edefault]
        Two(V),
    };
    assert_eq!(
        MyEnum::<u64, f64, String>::default(),
        MyEnum::Two(f64::default())
    );
    assert_ne!(
        MyEnum::<u64, f64, String>::default(),
        MyEnum::One(0, "".to_string())
    );

About

Extend your Enum with those macros

License:Apache License 2.0


Languages

Language:Rust 100.0%