summaryrefslogtreecommitdiffstats
path: root/third_party/rust/smart-default/README.md
blob: 1ece6950c4a236546aedf1757a285c273fb30383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[![Build Status](https://github.com/idanarye/rust-smart-default/workflows/CI/badge.svg)](https://github.com/idanarye/rust-smart-default/actions)
[![Latest Version](https://img.shields.io/crates/v/smart-default.svg)](https://crates.io/crates/smart-default)
[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://idanarye.github.io/rust-smart-default/)

# Rust SmartDefault

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

```rust
use smart_default::SmartDefault;

#[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)