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

..30-Mar-2022-

src/H30-Mar-2022-5,4244,701

tests/H30-Mar-2022-4,3553,600

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

CHANGELOG.mdH A D30-Mar-20226.2 KiB247148

Cargo.lockH A D30-Mar-20223.6 KiB8978

Cargo.tomlH A D30-Mar-20224.9 KiB201169

LICENSEH A D30-Mar-20221.1 KiB2217

README.mdH A D30-Mar-20226.5 KiB181139

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`, `MulSelf`,
106   `DivSelf`, `RemSelf`, `ShrSelf` and `ShlSelf`
1075. [`Mul`-like], contains `Mul`, `Div`, `Rem`, `Shr` and `Shl`
1083. [`Sum`-like], contains `Sum` and `Product`
1096. [`IndexMut`]
1107. [`DerefMut`]
1118. [`AddAssign`-like], contains `AddAssign`, `SubAssign`, `BitAndAssign`,
112   `BitOrAssign` and `BitXorAssign`
1139. [`MulAssign`-like], contains `MulAssign`, `DivAssign`, `RemAssign`,
114   `ShrAssign` and `ShlAssign`
115
116### Static methods
117
118These don't derive traits, but derive static methods instead.
119
1201. [`Constructor`], this derives a `new` method that can be used as a constructor.
121   This is very basic if you need more customization for your constructor, check
122   out the [`derive-new`] crate.
123
124## Generated code
125
126## Installation
127
128This library requires Rust 1.36 or higher and it supports `no_std` out of the box.
129Then add the following to `Cargo.toml`:
130
131```toml
132[dependencies]
133derive_more = "0.99.0"
134# You can specifiy the types of derives that you need for less time spent
135# compiling. For the full list of features see this crate its Cargo.toml.
136default-features = false
137features = ["from", "add", "iterator"]
138```
139
140And this to the top of your Rust file for Rust 2018:
141
142```rust
143extern crate derive_more;
144// use the derives that you want in the file
145use derive_more::{Add, Display, From};
146```
147If you're still using Rust 2015 you should add this instead:
148```rust
149extern crate core;
150#[macro_use]
151extern crate derive_more;
152```
153
154[`cargo-expand`]: https://github.com/dtolnay/cargo-expand
155[`derive-new`]: https://github.com/nrc/derive-new
156
157[`From`]: https://jeltef.github.io/derive_more/derive_more/from.html
158[`Into`]: https://jeltef.github.io/derive_more/derive_more/into.html
159[`FromStr`]: https://jeltef.github.io/derive_more/derive_more/from_str.html
160[`TryInto`]: https://jeltef.github.io/derive_more/derive_more/try_into.html
161[`IntoIterator`]: https://jeltef.github.io/derive_more/derive_more/into_iterator.html
162[`AsRef`]: https://jeltef.github.io/derive_more/derive_more/as_ref.html
163[`AsMut`]: https://jeltef.github.io/derive_more/derive_more/as_mut.html
164
165[`Display`-like]: https://jeltef.github.io/derive_more/derive_more/display.html
166
167[`Error`]: https://jeltef.github.io/derive_more/derive_more/error.html
168
169[`Index`]: https://jeltef.github.io/derive_more/derive_more/index_op.html
170[`Deref`]: https://jeltef.github.io/derive_more/derive_more/deref.html
171[`Not`-like]: https://jeltef.github.io/derive_more/derive_more/not.html
172[`Add`-like]: https://jeltef.github.io/derive_more/derive_more/add.html
173[`Mul`-like]: https://jeltef.github.io/derive_more/derive_more/mul.html
174[`Sum`-like]: https://jeltef.github.io/derive_more/derive_more/sum.html
175[`IndexMut`]: https://jeltef.github.io/derive_more/derive_more/index_mut.html
176[`DerefMut`]: https://jeltef.github.io/derive_more/derive_more/deref_mut.html
177[`AddAssign`-like]: https://jeltef.github.io/derive_more/derive_more/add_assign.html
178[`MulAssign`-like]: https://jeltef.github.io/derive_more/derive_more/mul_assign.html
179
180[`Constructor`]: https://jeltef.github.io/derive_more/derive_more/constructor.html
181