| 1 | # `derive_more` |
| 2 | |
| 3 | [](https://github.com/JelteF/derive_more/actions) |
| 4 | [](https://crates.io/crates/derive_more) |
| 5 | [](https://docs.rs/derive_more) |
| 6 | [](https://raw.githubusercontent.com/JelteF/derive_more/master/LICENSE) |
| 7 | [](https://blog.rust-lang.org/2024/09/05/Rust-1.81.0) |
| 8 | [](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. [`TryFrom`] |
| 89 | 5. [`TryInto`] |
| 90 | 6. [`IntoIterator`] |
| 91 | 7. [`AsRef`], [`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 | 11. [`Eq`], [`PartialEq`] |
| 127 | |
| 128 | |
| 129 | ### Static methods |
| 130 | |
| 131 | These don't derive traits, but derive static methods instead. |
| 132 | |
| 133 | 1. [`Constructor`], this derives a `new` method that can be used as a constructor. |
| 134 | This is very basic if you need more customization for your constructor, check |
| 135 | out the [`derive-new`] crate. |
| 136 | 2. [`IsVariant`], for each variant `foo` of an enum type, derives a `is_foo` method. |
| 137 | 3. [`Unwrap`], for each variant `foo` of an enum type, derives an `unwrap_foo` method. |
| 138 | 4. [`TryUnwrap`], for each variant `foo` of an enum type, derives an `try_unwrap_foo` method. |
| 139 | |
| 140 | |
| 141 | ### Re-exports |
| 142 | |
| 143 | This crate also re-exports all the standard library traits, that it adds derives |
| 144 | for, in the `with_trait` module. So, both the `Display` derive and the `Display` |
| 145 | trait will be in scope when you add the following code: |
| 146 | ```rust |
| 147 | use derive_more::with_trait::Display; // also imports `core::fmt::Display` |
| 148 | ``` |
| 149 | |
| 150 | By default, derive macros only, without the corresponding traits, are imported from |
| 151 | the crate's root (or from the `derive` module): |
| 152 | ```rust |
| 153 | use derive_more::Display; // imports macro only |
| 154 | use derive_more::derive::*; // imports all macros only |
| 155 | ``` |
| 156 | |
| 157 | #### Hygiene |
| 158 | |
| 159 | For hygiene purposes, macros use `derive_more::*` absolute paths in their expansions. |
| 160 | This might introduce a trouble, if you want to re-export `derive_more` macros in your |
| 161 | own crate without using the `derive_more` as a direct dependency in downstream crates: |
| 162 | ```rust,ignore |
| 163 | use my_lib::Display; // re-exported in `my_lib` crate |
| 164 | |
| 165 | #[derive(Display)] // error: could not find `derive_more` in the list of imported crates |
| 166 | struct MyInt(i32); |
| 167 | ``` |
| 168 | In such case, you should re-export the `derive_more` module too: |
| 169 | ```rust,ignore |
| 170 | use my_lib::{derive_more, Display}; // re-exported in `my_lib` crate |
| 171 | |
| 172 | #[derive(Display)] // works fine now! |
| 173 | struct MyInt(i32); |
| 174 | ``` |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | ## Installation |
| 180 | |
| 181 | To avoid redundant compilation times, by default no derives are supported. |
| 182 | You have to enable each type of derive as a feature in `Cargo.toml`: |
| 183 | ```toml |
| 184 | [dependencies] |
| 185 | # You can specify the types of derives that you need for less time spent |
| 186 | # compiling. For the full list of features see this crate its `Cargo.toml`. |
| 187 | derive_more = { version = "2", features = ["from", "add", "into_iterator"] } |
| 188 | ``` |
| 189 | ```toml |
| 190 | [dependencies] |
| 191 | # If you don't care much about compilation times and simply want to have |
| 192 | # support for all the possible derives, you can use the "full" feature. |
| 193 | derive_more = { version = "2", features = ["full"] } |
| 194 | ``` |
| 195 | ```toml |
| 196 | [dependencies] |
| 197 | # If you run in a `no_std` environment you should disable the default features, |
| 198 | # because the only default feature is the "std" feature. |
| 199 | # NOTE: You can combine this with "full" feature to get support for all the |
| 200 | # possible derives in a `no_std` environment. |
| 201 | derive_more = { version = "2", default-features = false } |
| 202 | ``` |
| 203 | |
| 204 | And this to the top of your Rust file: |
| 205 | ```rust |
| 206 | // use the derives that you want in the file |
| 207 | use derive_more::{Add, Display, From}; |
| 208 | ``` |
| 209 | If you're still using Rust 2015, add this instead: |
| 210 | ```rust,edition2015 |
| 211 | extern crate core; |
| 212 | #[macro_use] |
| 213 | extern crate derive_more; |
| 214 | # fn main() {} // omit wrapping statements above into `main()` in tests |
| 215 | ``` |
| 216 | |
| 217 | |
| 218 | ## [MSRV] policy |
| 219 | |
| 220 | This library requires Rust 1.81 or higher. |
| 221 | |
| 222 | Changing [MSRV] (minimum supported Rust version) of this crate is treated as a **minor version change** in terms of [Semantic Versioning]. |
| 223 | - So, if [MSRV] changes are **NOT concerning** for your project, just use the default [caret requirement]: |
| 224 | ```toml |
| 225 | [dependencies] |
| 226 | derive_more = "2" # or "2.1", or "^2.1" |
| 227 | ``` |
| 228 | - However, if [MSRV] changes are concerning for your project, then use the [tilde requirement] to **pin to a specific minor version**: |
| 229 | ```toml |
| 230 | [dependencies] |
| 231 | derive_more = "~2.1" # or "~2.1.1" |
| 232 | ``` |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | [`cargo-expand`]: https://github.com/dtolnay/cargo-expand |
| 238 | [`derive-new`]: https://github.com/nrc/derive-new |
| 239 | |
| 240 | [`From`]: https://docs.rs/derive_more/latest/derive_more/derive.From.html |
| 241 | [`Into`]: https://docs.rs/derive_more/latest/derive_more/derive.Into.html |
| 242 | [`FromStr`]: https://docs.rs/derive_more/latest/derive_more/derive.FromStr.html |
| 243 | [`TryFrom`]: https://docs.rs/derive_more/latest/derive_more/derive.TryFrom.html |
| 244 | [`TryInto`]: https://docs.rs/derive_more/latest/derive_more/derive.TryInto.html |
| 245 | [`IntoIterator`]: https://docs.rs/derive_more/latest/derive_more/derive.IntoIterator.html |
| 246 | [`AsRef`]: https://docs.rs/derive_more/latest/derive_more/derive.AsRef.html |
| 247 | [`AsMut`]: https://docs.rs/derive_more/latest/derive_more/derive.AsMut.html |
| 248 | |
| 249 | [`Debug`]: https://docs.rs/derive_more/latest/derive_more/derive.Debug.html |
| 250 | [`Display`-like]: https://docs.rs/derive_more/latest/derive_more/derive.Display.html |
| 251 | |
| 252 | [`Error`]: https://docs.rs/derive_more/latest/derive_more/derive.Error.html |
| 253 | |
| 254 | [`Index`]: https://docs.rs/derive_more/latest/derive_more/derive.Index.html |
| 255 | [`Deref`]: https://docs.rs/derive_more/latest/derive_more/derive.Deref.html |
| 256 | [`Not`-like]: https://docs.rs/derive_more/latest/derive_more/derive.Not.html |
| 257 | [`Add`-like]: https://docs.rs/derive_more/latest/derive_more/derive.Add.html |
| 258 | [`Mul`-like]: https://docs.rs/derive_more/latest/derive_more/derive.Mul.html |
| 259 | [`Sum`-like]: https://docs.rs/derive_more/latest/derive_more/derive.Sum.html |
| 260 | [`IndexMut`]: https://docs.rs/derive_more/latest/derive_more/derive.IndexMut.html |
| 261 | [`DerefMut`]: https://docs.rs/derive_more/latest/derive_more/derive.DerefMut.html |
| 262 | [`AddAssign`-like]: https://docs.rs/derive_more/latest/derive_more/derive.AddAssign.html |
| 263 | [`MulAssign`-like]: https://docs.rs/derive_more/latest/derive_more/derive.MulAssign.html |
| 264 | [`Eq`]: https://docs.rs/derive_more/latest/derive_more/derive.Eq.html |
| 265 | [`PartialEq`]: https://docs.rs/derive_more/latest/derive_more/derive.PartialEq.html |
| 266 | |
| 267 | [`Constructor`]: https://docs.rs/derive_more/latest/derive_more/derive.Constructor.html |
| 268 | [`IsVariant`]: https://docs.rs/derive_more/latest/derive_more/derive.IsVariant.html |
| 269 | [`Unwrap`]: https://docs.rs/derive_more/latest/derive_more/derive.Unwrap.html |
| 270 | [`TryUnwrap`]: https://docs.rs/derive_more/latest/derive_more/derive.TryUnwrap.html |
| 271 | |
| 272 | [caret requirement]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#caret-requirements |
| 273 | [tilde requirement]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#tilde-requirements |
| 274 | [MSRV]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-rust-version-field |
| 275 | [Semantic Versioning]: http://semver.org |