2 unstable releases

0.2.0 Dec 9, 2019
0.1.0 Dec 7, 2019

#1159 in Procedural macros

Download history 71/week @ 2024-04-12 57/week @ 2024-04-19 120/week @ 2024-04-26 88/week @ 2024-05-03 99/week @ 2024-05-10 50/week @ 2024-05-17 68/week @ 2024-05-24 331/week @ 2024-05-31 58/week @ 2024-06-07 52/week @ 2024-06-14 61/week @ 2024-06-21 39/week @ 2024-06-28 54/week @ 2024-07-05 139/week @ 2024-07-12 128/week @ 2024-07-19 58/week @ 2024-07-26

379 downloads per month
Used in 3 crates

Custom license

11KB
149 lines

#[derive(Defaults)]

A better Default derive macro.

Crate

Examples:

You can provide default value for a non-default field.

#[derive(Defaults)]
pub struct Event {
    id: usize,
    name: String,
    #[def = "Instant::now()"]
    time: Instant,
}

You can also provide a different default value of an already default field.

#[derive(Defaults)]
pub struct Event {
    #[def = "4"]
    id: usize,
    name: String,
    #[def = "Instant::now()"]
    time: Instant,
}

You can even provide default value of enums:

#[derive(Defaults)]
#[def = "A"]
pub struct Classroom {
    A,
    B,
    C
}

But what about variants with data?

#[derive(Defaults)]
#[def = "A(100)"]
pub struct SomeEnum {
    A(usize),
    B { x: usize, y: usize },
    C
}

Dependencies

~1.5MB
~35K SLoC