#constructor #enums #derive #naming #name #construct

macro derive_constructor

Construct enums without naming them

2 releases

0.1.1 Sep 26, 2021
0.1.0 Sep 26, 2021

#1341 in Procedural macros

MIT/Apache

7KB
118 lines

derive-constructor

This crate provides a derive macro Constructor, which creates a silly trait that mirrors an enum's constructors. It allows constructing an enum without explicitly naming its type. The generated trait has the same name as the enum but with the suffix Constructor.

Example

use derive_constructor::Constructor;

#[derive(Constructor, Debug)]
enum Foo {
    A,
    B(i32)
}

fn main() {
    print_foo(<_>::A);
    print_foo(<_>::B(42));
}

fn print_foo(foo: Foo) {
    println!("{:?}", foo);
}

Dependencies

~1.5MB
~36K SLoC