7 releases (1 stable)

1.0.0 Nov 13, 2024
0.3.1 Jul 1, 2023
0.2.1 May 31, 2023
0.1.1 May 31, 2023

#304 in Procedural macros

Download history 3/week @ 2024-09-20 3/week @ 2024-11-01 92/week @ 2024-11-08 33/week @ 2024-11-15 4/week @ 2024-11-22 2/week @ 2024-12-06

86 downloads per month

MIT/Apache

7KB

Default Derive

Test crates.io

Default implementation using macros

Example

Use default2::Default to set default value of each field using a macro:

#[derive(default2::Default)]
struct Process {
    #[default(10)]
    id: i32,

    #[default("main".into())]
    name: String,

    #[default(num_cpus::get())]
    cpus: usize,

    #[default(vec![1, 2, 3])]
    vector: Vec<u64>,

    payload: u64,
}

The following code will be generated:

struct Process {
    id: i32,
    name: String,
    cpus: usize,
    payload: u64,
}

impl Default for Process {
    fn default() -> Self {
        Process {
            id: 10,
            name: "main".into(),
            cpus: num_cpus::get(),
            vector: vec![1, 2, 3],
            payload: Default::default(),
        }
    }
}

Dependencies

~220–660KB
~16K SLoC