]> git.proxmox.com Git - rustc.git/blame - vendor/derive-new/README.md
New upstream version 1.52.1+dfsg1
[rustc.git] / vendor / derive-new / README.md
CommitLineData
f20569fa
XL
1# A custom derive implementation for `#[derive(new)]`
2
3A `derive(new)` attribute creates a `new` constructor function for the annotated
4type. That function takes an argument for each field in the type giving a
5trivial constructor. This is useful since as your type evolves you can make the
6constructor non-trivial (and add or remove fields) without changing client code
7(i.e., without breaking backwards compatibility). It is also the most succinct
8way to initialise a struct or an enum.
9
10Implementation uses macros 1.1 custom derive (which works in stable Rust from
111.15 onwards).
12
13`#[no_std]` is fully supported if you switch off the default feature `"std"`.
14
15## Examples
16
17Cargo.toml:
18
19```toml
20[dependencies]
21derive-new = "0.5"
22```
23
24Include the macro:
25
26```rust
27#[macro_use]
28extern crate derive_new;
29```
30
31Generating constructor for a simple struct:
32
33```rust
34#[derive(new)]
35struct Bar {
36 a: i32,
37 b: String,
38}
39
40let _ = Bar::new(42, "Hello".to_owned());
41```
42
43Default values can be specified either via `#[new(default)]` attribute which removes
44the argument from the constructor and populates the field with `Default::default()`,
45or via `#[new(value = "..")]` which initializes the field with a given expression:
46
47```rust
48#[derive(new)]
49struct Foo {
50 x: bool,
51 #[new(value = "42")]
52 y: i32,
53 #[new(default)]
54 z: Vec<String>,
55}
56
57let _ = Foo::new(true);
58```
59
60Generic types are supported; in particular, `PhantomData<T>` fields will be not
61included in the argument list and will be intialized automatically:
62
63```rust
64use std::marker::PhantomData;
65
66#[derive(new)]
67struct Generic<'a, T: Default, P> {
68 x: &'a str,
69 y: PhantomData<P>,
70 #[new(default)]
71 z: T,
72}
73
74let _ = Generic::<i32, u8>::new("Hello");
75```
76
77For enums, one constructor method is generated for each variant, with the type
78name being converted to snake case; otherwise, all features supported for
79structs work for enum variants as well:
80
81```rust
82#[derive(new)]
83struct Enum {
84 FirstVariant,
85 SecondVariant(bool, #[new(default)] u8),
86 ThirdVariant { x: i32, #[new(value = "vec![1]")] y: Vec<u8> }
87}
88
89let _ = Enum::new_first_variant();
90let _ = Enum::new_second_variant(true);
91let _ = Enum::new_third_variant(42);
92```