#default

macro smart-default

Rust custom-derive macro for Default with more control on the fields

8 releases (5 breaking)

Uses old Rust 2015

0.6.0 Dec 13, 2019
0.5.2 Apr 13, 2019
0.5.1 Mar 1, 2019
0.5.0 Feb 28, 2019
0.1.0 Aug 18, 2017

#73 in Rust patterns

Download history 4590/week @ 2020-08-13 4613/week @ 2020-08-20 4687/week @ 2020-08-27 4598/week @ 2020-09-03 5002/week @ 2020-09-10 4799/week @ 2020-09-17 5867/week @ 2020-09-24 5333/week @ 2020-10-01 4451/week @ 2020-10-08 4131/week @ 2020-10-15 6035/week @ 2020-10-22 7190/week @ 2020-10-29 6759/week @ 2020-11-05 9596/week @ 2020-11-12 9665/week @ 2020-11-19 6130/week @ 2020-11-26

16,238 downloads per month
Used in 44 crates (23 directly)

MIT license

16KB
274 lines

Build Status Latest Version Rust Documentation

Rust SmartDefault

Custom derive for automatically implementing the Default trait with customized default values:

#[macro_use]
extern crate smart_default;

#[derive(SmartDefault)]
enum Foo {
    Bar,
    #[default]
    Baz {
        #[default = 12]
        a: i32,
        b: i32,
        #[default(Some(Default::default()))]
        c: Option<i32>,
        #[default(_code = "vec![1, 2, 3]")]
        d: Vec<u32>,
        #[default = "four"]
        e: String,
    },
    Qux(i32),
}

assert!(Foo::default() == Foo::Baz {
    a: 12,
    b: 0,
    c: Some(0),
    d: vec![1, 2, 3],
    e: "four".to_owned(),
});

Requires Rust 1.30+ (for non-string values in attributes)

Dependencies

~0.4–0.8MB
~21K SLoC