defaults

Crates.iodefaults
lib.rsdefaults
version0.2.0
sourcesrc
created_at2019-12-07 16:39:32.399612
updated_at2019-12-09 05:08:18.876183
descriptionA better `Default` derive macro
homepagehttps://github.com/95th/defaults
repositoryhttps://github.com/95th/defaults
max_upload_size
id187113
size32,772
Gurwinder Singh (95th)

documentation

https://docs.rs/defaults

README

#[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
}
Commit count: 16

cargo fmt