Debian Codegraph @

third_party/rust/smart-default/README.md

raw
1 [![Build Status](https://github.com/idanarye/rust-smart-default/workflows/CI/badge.svg)](https://github.com/idanarye/rust-smart-default/actions)
2 [![Latest Version](https://img.shields.io/crates/v/smart-default.svg)](https://crates.io/crates/smart-default)
3 [![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://idanarye.github.io/rust-smart-default/)
4
5 # Rust SmartDefault
6
7 Custom derive for automatically implementing the `Default` trait with customized default values:
8
9 ```rust
10 use smart_default::SmartDefault;
11
12 #[derive(SmartDefault)]
13 enum Foo {
14 Bar,
15 #[default]
16 Baz {
17 #[default = 12]
18 a: i32,
19 b: i32,
20 #[default(Some(Default::default()))]
21 c: Option<i32>,
22 #[default(_code = "vec![1, 2, 3]")]
23 d: Vec<u32>,
24 #[default = "four"]
25 e: String,
26 },
27 Qux(i32),
28 }
29
30 assert!(Foo::default() == Foo::Baz {
31 a: 12,
32 b: 0,
33 c: Some(0),
34 d: vec![1, 2, 3],
35 e: "four".to_owned(),
36 });
37 ```
38
39 Requires Rust 1.30+ (for non-string values in attributes)