default2

Crates.iodefault2
lib.rsdefault2
version0.3.1
sourcesrc
created_at2023-05-31 02:18:54.421861
updated_at2023-07-01 22:05:46.53137
descriptionDefault implementation using macros
homepagehttps://github.com/yaa110/default2
repositoryhttps://github.com/yaa110/default2
max_upload_size
id878471
size16,537
embedded-nal (github:rust-embedded-community:embedded-nal)

documentation

https://docs.rs/default2

README

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(),
        }
    }
}
Commit count: 3

cargo fmt