• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H03-May-2022-5,6234,872

tests/H03-May-2022-4,5513,769

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D01-Jan-197074 65

CHANGELOG.mdH A D01-Jan-19706.2 KiB247148

Cargo.lockH A D01-Jan-19702.8 KiB114100

Cargo.tomlH A D01-Jan-19705.3 KiB218183

Cargo.toml.orig-cargoH A D01-Jan-19705.4 KiB312276

LICENSEH A D01-Jan-19701.1 KiB2217

README.mdH A D01-Jan-19706.8 KiB184142

README.md

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://img.shields.io/badge/api-rustdoc-blue.svg)](https://jeltef.github.io/derive_more/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.36+](https://img.shields.io/badge/rustc-1.36+-lightgray.svg)](https://blog.rust-lang.org/2019/07/04/Rust-1.36.0.html)
8
9Rust has lots of builtin traits that are implemented for its basic types, such
10as `Add`, `Not`, `From` or `Display`.
11However, when wrapping these types inside your own structs or enums you lose the
12implementations of these traits and are required to recreate them.
13This is especially annoying when your own structures are very simple, such as
14when using the commonly advised newtype pattern (e.g. `MyInt(i32)`).
15
16This library tries to remove these annoyances and the corresponding boilerplate code.
17It does this by allowing you to derive lots of commonly used traits for both structs and enums.
18
19## Example code
20
21By using this library the following code just works:
22
23```rust
24extern crate derive_more;
25use derive_more::{Add, Display, From, Into};
26
27#[derive(PartialEq, From, Add)]
28struct MyInt(i32);
29
30#[derive(PartialEq, From, Into)]
31struct Point2D {
32    x: i32,
33    y: i32,
34}
35
36#[derive(PartialEq, From, Add, Display)]
37enum MyEnum {
38    #[display(fmt = "int: {}", _0)]
39    Int(i32),
40    Uint(u32),
41    #[display(fmt = "nothing")]
42    Nothing,
43}
44
45assert!(MyInt(11) == MyInt(5) + 6.into());
46assert!((5, 6) == Point2D { x: 5, y: 6 }.into());
47assert!(MyEnum::Int(15) == (MyEnum::Int(8) + 7.into()).unwrap());
48assert!(MyEnum::Int(15).to_string() == "int: 15");
49assert!(MyEnum::Uint(42).to_string() == "42");
50assert!(MyEnum::Nothing.to_string() == "nothing");
51```
52
53## The derivable traits
54
55Below are all the traits that you can derive using this library.
56Some trait derivations are so similar that the further documentation will only show a single one
57of them.
58You can recognize these by the "-like" suffix in their name.
59The trait name before that will be the only one that is used throughout the further
60documentation.
61
62It is important to understand what code gets generated when using one of the
63derives from this crate.
64That is why the links below explain what code gets generated for a trait for
65each group from before.
66
67You can use the [`cargo-expand`] utility to see the exact code that is generated
68for your specific type.
69This will show you your code with all macros and derives expanded.
70
71**NOTE**: You still have to derive each trait separately. So `#[derive(Mul)]` doesn't
72automatically derive `Div` as well. To derive both you should do `#[derive(Mul, Div)]`
73
74### Conversion traits
75
76These are traits that are used to convert automatically between types.
77
781. [`From`]
792. [`Into`]
803. [`FromStr`]
814. [`TryInto`]
825. [`IntoIterator`]
836. [`AsRef`]
847. [`AsMut`]
85
86### Formatting traits
87
88These traits are used for converting a struct to a string in different ways.
89
901. [`Display`-like], contains `Display`, `Binary`, `Octal`, `LowerHex`,
91   `UpperHex`, `LowerExp`, `UpperExp`, `Pointer`
92
93### Error-handling traits
94These traits are used to define error-types.
95
961. [`Error`]
97
98### Operators
99
100These are traits that can be used for operator overloading.
101
1021. [`Index`]
1032. [`Deref`]
1043. [`Not`-like], contains `Not` and `Neg`
1054. [`Add`-like], contains `Add`, `Sub`, `BitAnd`, `BitOr`, `BitXor`
1065. [`Mul`-like], contains `Mul`, `Div`, `Rem`, `Shr` and `Shl`
1073. [`Sum`-like], contains `Sum` and `Product`
1086. [`IndexMut`]
1097. [`DerefMut`]
1108. [`AddAssign`-like], contains `AddAssign`, `SubAssign`, `BitAndAssign`,
111   `BitOrAssign` and `BitXorAssign`
1129. [`MulAssign`-like], contains `MulAssign`, `DivAssign`, `RemAssign`,
113   `ShrAssign` and `ShlAssign`
114
115### Static methods
116
117These don't derive traits, but derive static methods instead.
118
1191. [`Constructor`], this derives a `new` method that can be used as a constructor.
120   This is very basic if you need more customization for your constructor, check
121   out the [`derive-new`] crate.
1222. [`IsVariant`], for each variant `foo` of an enum type, derives a `is_foo` method.
1233. [`Unwrap`], for each variant `foo` of an enum type, derives an `unwrap_foo` method.
124
125## Generated code
126
127## Installation
128
129This library requires Rust 1.36 or higher and it supports `no_std` out of the box.
130Then add the following to `Cargo.toml`:
131
132```toml
133[dependencies]
134derive_more = "0.99.0"
135# You can specifiy the types of derives that you need for less time spent
136# compiling. For the full list of features see this crate its Cargo.toml.
137default-features = false
138features = ["from", "add", "iterator"]
139```
140
141And this to the top of your Rust file for Rust 2018:
142
143```rust
144extern crate derive_more;
145// use the derives that you want in the file
146use derive_more::{Add, Display, From};
147```
148If you're still using Rust 2015 you should add this instead:
149```rust
150extern crate core;
151#[macro_use]
152extern crate derive_more;
153```
154
155[`cargo-expand`]: https://github.com/dtolnay/cargo-expand
156[`derive-new`]: https://github.com/nrc/derive-new
157
158[`From`]: https://jeltef.github.io/derive_more/derive_more/from.html
159[`Into`]: https://jeltef.github.io/derive_more/derive_more/into.html
160[`FromStr`]: https://jeltef.github.io/derive_more/derive_more/from_str.html
161[`TryInto`]: https://jeltef.github.io/derive_more/derive_more/try_into.html
162[`IntoIterator`]: https://jeltef.github.io/derive_more/derive_more/into_iterator.html
163[`AsRef`]: https://jeltef.github.io/derive_more/derive_more/as_ref.html
164[`AsMut`]: https://jeltef.github.io/derive_more/derive_more/as_mut.html
165
166[`Display`-like]: https://jeltef.github.io/derive_more/derive_more/display.html
167
168[`Error`]: https://jeltef.github.io/derive_more/derive_more/error.html
169
170[`Index`]: https://jeltef.github.io/derive_more/derive_more/index_op.html
171[`Deref`]: https://jeltef.github.io/derive_more/derive_more/deref.html
172[`Not`-like]: https://jeltef.github.io/derive_more/derive_more/not.html
173[`Add`-like]: https://jeltef.github.io/derive_more/derive_more/add.html
174[`Mul`-like]: https://jeltef.github.io/derive_more/derive_more/mul.html
175[`Sum`-like]: https://jeltef.github.io/derive_more/derive_more/sum.html
176[`IndexMut`]: https://jeltef.github.io/derive_more/derive_more/index_mut.html
177[`DerefMut`]: https://jeltef.github.io/derive_more/derive_more/deref_mut.html
178[`AddAssign`-like]: https://jeltef.github.io/derive_more/derive_more/add_assign.html
179[`MulAssign`-like]: https://jeltef.github.io/derive_more/derive_more/mul_assign.html
180
181[`Constructor`]: https://jeltef.github.io/derive_more/derive_more/constructor.html
182[`IsVariant`]: https://jeltef.github.io/derive_more/derive_more/is_variant.html
183[`Unwrap`]: https://jeltef.github.io/derive_more/derive_more/unwrap.html
184