Short_default - simplify Default implementations

I noticed that there is no simple macro to define structs with default values that emulates the following pattern:

default_struct! Config {
    version: (u64, u64, u64) = (0, 1, 0),
    // Automatically assigned
    authors: Vec<String>,
}

Thus I created my own library crates.io/crates/short_default which comes close:

default! {
    struct Config {
        version: (u64, u64, u64) = (0, 1, 0),
        // Automatically assigned
        authors: Vec<String>,
    }
}

I suppose with the current state of macros in Rust it is not possible to create a macro which "replaces" the struct keyword. Please let me know what you think. Would you use this?

I am aware of the existence of the derivative crate which solves similar problems but find the ergonomics of having to resort to field attributes cumbersome (specifically for default values, not necessarily others such as formatting).

I'm waiting for this:

5 Likes

I did not know about this RFC. This makes me really happy :partying_face:

1 Like

Test it out on nightly! That's the best way 99% of people can help with getting something closer to stabilization.

5 Likes