Debian Codegraph @

third_party/rust/derive_more/README.md

raw
1 # `derive_more`
2
3 [![Build Status](https://github.com/JelteF/derive_more/workflows/CI/badge.svg)](https://github.com/JelteF/derive_more/actions)
4 [![Latest Version](https://img.shields.io/crates/v/derive_more.svg)](https://crates.io/crates/derive_more)
5 [![Rust Documentation](https://docs.rs/derive_more/badge.svg)](https://docs.rs/derive_more)
6 [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/JelteF/derive_more/master/LICENSE)
7 [![Rust 1.65+](https://img.shields.io/badge/rustc-1.65+-lightgray.svg)](https://blog.rust-lang.org/2021/10/21/Rust-1.65.0.html)
8 [![Unsafe Forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance)
9
10 Rust has lots of builtin traits that are implemented for its basic types, such
11 as `Add`, `Not`, `From` or `Display`.
12 However, when wrapping these types inside your own structs or enums you lose the
13 implementations of these traits and are required to recreate them.
14 This is especially annoying when your own structures are very simple, such as
15 when using the commonly advised newtype pattern (e.g. `MyInt(i32)`).
16
17 This library tries to remove these annoyances and the corresponding boilerplate code.
18 It does this by allowing you to derive lots of commonly used traits for both structs and enums.
19
20
21
22
23 ## Example code
24
25 By using this library the following code just works:
26
27 ```rust
28 use derive_more::{Add, Display, From, Into};
29
30 #[derive(PartialEq, From, Add)]
31 struct MyInt(i32);
32
33 #[derive(PartialEq, From, Into)]
34 struct Point2D {
35 x: i32,
36 y: i32,
37 }
38
39 #[derive(PartialEq, From, Add, Display)]
40 enum MyEnum {
41 #[display("int: {_0}")]
42 Int(i32),
43 Uint(u32),
44 #[display("nothing")]
45 Nothing,
46 }
47
48 assert!(MyInt(11) == MyInt(5) + 6.into());
49 assert!((5, 6) == Point2D { x: 5, y: 6 }.into());
50 assert!(MyEnum::Int(15) == (MyEnum::Int(8) + 7.into()).unwrap());
51 assert!(MyEnum::Int(15).to_string() == "int: 15");
52 assert!(MyEnum::Uint(42).to_string() == "42");
53 assert!(MyEnum::Nothing.to_string() == "nothing");
54 ```
55
56
57
58
59 ## The derivable traits
60
61 Below are all the traits that you can derive using this library.
62 Some trait derivations are so similar that the further documentation will only show a single one
63 of them.
64 You can recognize these by the "-like" suffix in their name.
65 The trait name before that will be the only one that is used throughout the further
66 documentation.
67
68 It is important to understand what code gets generated when using one of the
69 derives from this crate.
70 That is why the links below explain what code gets generated for a trait for
71 each group from before.
72
73 You can use the [`cargo-expand`] utility to see the exact code that is generated
74 for your specific type.
75 This will show you your code with all macros and derives expanded.
76
77 **NOTE**: You still have to derive each trait separately. So `#[derive(Mul)]` doesn't
78 automatically derive `Div` as well. To derive both you should do `#[derive(Mul, Div)]`
79
80
81 ### Conversion traits
82
83 These are traits that are used to convert automatically between types.
84
85 1. [`From`]
86 2. [`Into`]
87 3. [`FromStr`]
88 4. [`TryInto`]
89 5. [`IntoIterator`]
90 6. [`AsRef`]
91 7. [`AsMut`]
92
93
94 ### Formatting traits
95
96 These traits are used for converting a struct to a string in different ways.
97
98 1. [`Debug`]
99 2. [`Display`-like], contains `Display`, `Binary`, `Octal`, `LowerHex`,
100 `UpperHex`, `LowerExp`, `UpperExp`, `Pointer`
101
102
103 ### Error-handling traits
104
105 These traits are used to define error-types.
106
107 1. [`Error`]
108
109
110 ### Operators
111
112 These are traits that can be used for operator overloading.
113
114 1. [`Index`]
115 2. [`Deref`]
116 3. [`Not`-like], contains `Not` and `Neg`
117 4. [`Add`-like], contains `Add`, `Sub`, `BitAnd`, `BitOr`, `BitXor`
118 5. [`Mul`-like], contains `Mul`, `Div`, `Rem`, `Shr` and `Shl`
119 6. [`Sum`-like], contains `Sum` and `Product`
120 7. [`IndexMut`]
121 8. [`DerefMut`]
122 9. [`AddAssign`-like], contains `AddAssign`, `SubAssign`, `BitAndAssign`,
123 `BitOrAssign` and `BitXorAssign`
124 10. [`MulAssign`-like], contains `MulAssign`, `DivAssign`, `RemAssign`,
125 `ShrAssign` and `ShlAssign`
126
127
128 ### Static methods
129
130 These don't derive traits, but derive static methods instead.
131
132 1. [`Constructor`], this derives a `new` method that can be used as a constructor.
133 This is very basic if you need more customization for your constructor, check
134 out the [`derive-new`] crate.
135 2. [`IsVariant`], for each variant `foo` of an enum type, derives a `is_foo` method.
136 3. [`Unwrap`], for each variant `foo` of an enum type, derives an `unwrap_foo` method.
137 4. [`TryUnwrap`], for each variant `foo` of an enum type, derives an `try_unwrap_foo` method.
138
139
140
141
142 ## Installation
143
144 This library requires Rust 1.65 or higher. To avoid redundant compilation times, by
145 default no derives are supported. You have to enable each type of derive as a feature
146 in `Cargo.toml`:
147
148 ```toml
149 [dependencies]
150 derive_more = "0.99.0"
151 # You can specify the types of derives that you need for less time spent
152 # compiling. For the full list of features see this crate its `Cargo.toml`.
153 features = ["from", "add", "iterator"]
154
155 # If you don't care much about compilation times and simply want to have
156 # support for all the possible derives, you can use the "full" feature.
157 features = ["full"]
158
159 # If you run in a `no_std` environment you should disable the default features,
160 # because the only default feature is the "std" feature.
161 # NOTE: You can combine this with "full" feature to get support for all the
162 # possible derives in a `no_std` environment.
163 default-features = false
164 ```
165
166 And this to the top of your Rust file:
167 ```rust
168 // use the derives that you want in the file
169 use derive_more::{Add, Display, From};
170 ```
171 If you're still using Rust 2015, add this instead:
172 ```rust,edition2015
173 extern crate core;
174 #[macro_use]
175 extern crate derive_more;
176 # fn main() {} // omit wrapping statements above into `main()` in tests
177 ```
178
179 ## Re-exports
180
181 This crate also re-exports all of the standard library traits that it adds
182 derives for. So both the `Display` derive and the `Display` trait will be in
183 scope when you add the following code:
184 ```rust
185 use derive_more::Display;
186 ```
187
188 [`cargo-expand`]: https://github.com/dtolnay/cargo-expand
189 [`derive-new`]: https://github.com/nrc/derive-new
190
191 [`From`]: https://jeltef.github.io/derive_more/derive_more/from.html
192 [`Into`]: https://jeltef.github.io/derive_more/derive_more/into.html
193 [`FromStr`]: https://jeltef.github.io/derive_more/derive_more/from_str.html
194 [`TryInto`]: https://jeltef.github.io/derive_more/derive_more/try_into.html
195 [`IntoIterator`]: https://jeltef.github.io/derive_more/derive_more/into_iterator.html
196 [`AsRef`]: https://jeltef.github.io/derive_more/derive_more/as_ref.html
197 [`AsMut`]: https://jeltef.github.io/derive_more/derive_more/as_mut.html
198
199 [`Debug`]: https://github.com/JelteF/derive_more/blob/master/impl/doc/debug.md
200 [`Display`-like]: https://jeltef.github.io/derive_more/derive_more/display.html
201
202 [`Error`]: https://jeltef.github.io/derive_more/derive_more/error.html
203
204 [`Index`]: https://jeltef.github.io/derive_more/derive_more/index_op.html
205 [`Deref`]: https://jeltef.github.io/derive_more/derive_more/deref.html
206 [`Not`-like]: https://jeltef.github.io/derive_more/derive_more/not.html
207 [`Add`-like]: https://jeltef.github.io/derive_more/derive_more/add.html
208 [`Mul`-like]: https://jeltef.github.io/derive_more/derive_more/mul.html
209 [`Sum`-like]: https://jeltef.github.io/derive_more/derive_more/sum.html
210 [`IndexMut`]: https://jeltef.github.io/derive_more/derive_more/index_mut.html
211 [`DerefMut`]: https://jeltef.github.io/derive_more/derive_more/deref_mut.html
212 [`AddAssign`-like]: https://jeltef.github.io/derive_more/derive_more/add_assign.html
213 [`MulAssign`-like]: https://jeltef.github.io/derive_more/derive_more/mul_assign.html
214
215 [`Constructor`]: https://jeltef.github.io/derive_more/derive_more/constructor.html
216 [`IsVariant`]: https://jeltef.github.io/derive_more/derive_more/is_variant.html
217 [`Unwrap`]: https://jeltef.github.io/derive_more/derive_more/unwrap.html
218 [`TryUnwrap`]: https://jeltef.github.io/derive_more/derive_more/try_unwrap.html