#constructor #macro #structs #macro-derive

macro constructor-macro

Derive constructor macros for structs

5 unstable releases

0.4.2 Jul 30, 2019
0.4.1 Jul 30, 2019
0.4.0 Jul 30, 2019
0.3.0 Jun 9, 2019
0.2.0 Jun 8, 2019

#50 in #constructor

MIT license

7KB
103 lines

constructor-macro

Cargo.toml

[dependencies]
constructor-macro = "0.4"

Example

Rust 2018

use constructor_macro::ConstructorMacro;

#[derive(ConstructorMacro)]
pub struct Thing {
    #[default(1)]
    pub field1: i32,
    pub field2: i32,
}

fn main() {
    let thing1 = Thing!();
    assert_eq!(thing1.field1, 1);
    assert_eq!(thing1.field2, 0);

    let thing2 = Thing! { field1: 2 };
    assert_eq!(thing2.field1, 2);
    assert_eq!(thing2.field2, 0);

    let thing3 = Thing! {
        field1: 0,
        field2: 100,
    };
    assert_eq!(thing3.field1, 0);
    assert_eq!(thing3.field2, 100);
}

Rust 2015

#[macro_use]
extern crate constructor_macro;

...

Dependencies

~2MB
~48K SLoC