Crates.io | derive_more_preview |
lib.rs | derive_more_preview |
version | 0.1.0 |
source | src |
created_at | 2023-07-10 08:57:31.204283 |
updated_at | 2023-07-10 08:57:31.204283 |
description | Adds #[derive(x)] macros for more traits |
homepage | |
repository | https://github.com/dignifiedquire/derive_more |
max_upload_size | |
id | 912745 |
size | 295,298 |
derive_more_preview
Fork of
derive_more
until a new release is cut Tracking issue https://github.com/JelteF/derive_more/issues/259
Rust has lots of builtin traits that are implemented for its basic types, such
as Add
, Not
, From
or Display
.
However, when wrapping these types inside your own structs or enums you lose the
implementations of these traits and are required to recreate them.
This is especially annoying when your own structures are very simple, such as
when using the commonly advised newtype pattern (e.g. MyInt(i32)
).
This library tries to remove these annoyances and the corresponding boilerplate code. It does this by allowing you to derive lots of commonly used traits for both structs and enums.
By using this library the following code just works:
use derive_more::{Add, Display, From, Into};
#[derive(PartialEq, From, Add)]
struct MyInt(i32);
#[derive(PartialEq, From, Into)]
struct Point2D {
x: i32,
y: i32,
}
#[derive(PartialEq, From, Add, Display)]
enum MyEnum {
#[display("int: {_0}")]
Int(i32),
Uint(u32),
#[display("nothing")]
Nothing,
}
assert!(MyInt(11) == MyInt(5) + 6.into());
assert!((5, 6) == Point2D { x: 5, y: 6 }.into());
assert!(MyEnum::Int(15) == (MyEnum::Int(8) + 7.into()).unwrap());
assert!(MyEnum::Int(15).to_string() == "int: 15");
assert!(MyEnum::Uint(42).to_string() == "42");
assert!(MyEnum::Nothing.to_string() == "nothing");
Below are all the traits that you can derive using this library. Some trait derivations are so similar that the further documentation will only show a single one of them. You can recognize these by the "-like" suffix in their name. The trait name before that will be the only one that is used throughout the further documentation.
It is important to understand what code gets generated when using one of the derives from this crate. That is why the links below explain what code gets generated for a trait for each group from before.
You can use the cargo-expand
utility to see the exact code that is generated
for your specific type.
This will show you your code with all macros and derives expanded.
NOTE: You still have to derive each trait separately. So #[derive(Mul)]
doesn't
automatically derive Div
as well. To derive both you should do #[derive(Mul, Div)]
These are traits that are used to convert automatically between types.
These traits are used for converting a struct to a string in different ways.
Display
-like, contains Display
, Binary
, Octal
, LowerHex
,
UpperHex
, LowerExp
, UpperExp
, Pointer
These traits are used to define error-types.
These are traits that can be used for operator overloading.
Not
-like, contains Not
and Neg
Add
-like, contains Add
, Sub
, BitAnd
, BitOr
, BitXor
Mul
-like, contains Mul
, Div
, Rem
, Shr
and Shl
Sum
-like, contains Sum
and Product
AddAssign
-like, contains AddAssign
, SubAssign
, BitAndAssign
,
BitOrAssign
and BitXorAssign
MulAssign
-like, contains MulAssign
, DivAssign
, RemAssign
,
ShrAssign
and ShlAssign
These don't derive traits, but derive static methods instead.
Constructor
, this derives a new
method that can be used as a constructor.
This is very basic if you need more customization for your constructor, check
out the derive-new
crate.
IsVariant
, for each variant foo
of an enum type, derives a is_foo
method.
Unwrap
, for each variant foo
of an enum type, derives an unwrap_foo
method.
TryUnwrap
, for each variant foo
of an enum type, derives an try_unwrap_foo
method.
This library requires Rust 1.65 or higher. To avoid redundant compilation times, by
default no derives are supported. You have to enable each type of derive as a feature
in Cargo.toml
:
[dependencies]
derive_more = "0.99.0"
# You can specify the types of derives that you need for less time spent
# compiling. For the full list of features see this crate its `Cargo.toml`.
features = ["from", "add", "iterator"]
# If you don't care much about compilation times and simply want to have
# support for all the possible derives, you can use the "full" feature.
features = ["full"]
# If you run in a `no_std` environment you should disable the default features,
# because the only default feature is the "std" feature.
# NOTE: You can combine this with "full" feature to get support for all the
# possible derives in a `no_std` environment.
default-features = false
And this to the top of your Rust file:
// use the derives that you want in the file
use derive_more::{Add, Display, From};
If you're still using Rust 2015, add this instead:
extern crate core;
#[macro_use]
extern crate derive_more;
# fn main() {} // omit wrapping statements above into `main()` in tests