gecko-dev/third_party/rust/derive_more
Bobby Holley c0182b02f9 Bug 1521187 - Add a dependency on derive_more. r=emilio
Differential Revision: https://phabricator.services.mozilla.com/D17028


--HG--
rename : third_party/rust/semver/.cargo-checksum.json => third_party/rust/semver-0.6.0/.cargo-checksum.json
rename : third_party/rust/semver/Cargo.toml => third_party/rust/semver-0.6.0/Cargo.toml
rename : third_party/rust/semver/README.md => third_party/rust/semver-0.6.0/README.md
rename : third_party/rust/semver/src/lib.rs => third_party/rust/semver-0.6.0/src/lib.rs
rename : third_party/rust/semver/src/version.rs => third_party/rust/semver-0.6.0/src/version.rs
rename : third_party/rust/semver/src/version_req.rs => third_party/rust/semver-0.6.0/src/version_req.rs
2019-01-22 12:19:22 -08:00
..
src
.cargo-checksum.json
CHANGELOG.md
Cargo.toml
LICENSE
README.md

README.md

derive_more

Build Status Latest Version Rust Documentation GitHub license

Rust has lots of builtin traits that are implemented for its basic types, such as Add, Not or From. 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.

Example code

By using this library the following code just works:

#[macro_use]
extern crate derive_more;

#[derive(Debug, Eq, PartialEq, From, Add)]
struct MyInt(i32);

#[derive(Debug, Eq, PartialEq, From, Into, Constructor, Mul)]
struct Point2D {
    x: i32,
    y: i32,
}

#[derive(Debug, Eq, PartialEq, From, Add)]
enum MyEnum {
    Int(i32),
    UnsignedInt(u32),
    Nothing,
}

fn main() {
    let my_11 = MyInt(5) + 6.into();
    assert_eq!(MyInt(11), MyInt(5) + 6.into());
    assert_eq!(Point2D { x: 5, y: 6 } * 10, (50, 60).into());
    assert_eq!((5, 6), Point2D { x: 5, y: 6 }.into());
    assert_eq!(Point2D { x: 5, y: 6 }, Point2D::new(5, 6));
    assert_eq!(MyEnum::Int(15), (MyEnum::Int(8) + 7.into()).unwrap())
}

The derivable traits

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.

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)]

Conversion traits

These are traits that are used to convert automatically between types.

  1. From
  2. Into
  3. FromStr
  4. TryInto (nightly-only as of writing)

Formatting traits

These traits are used for converting a struct to a string in different ways.

  1. Display-like, contains Display, Binary, Octal, LowerHex, UpperHex, LowerExp, UpperExp, Pointer

Operators

These are traits that can be used for operator overloading.

  1. Index
  2. Deref
  3. Not-like, contains Not and Neg
  4. Add-like, contains Add, Sub, BitAnd, BitOr and BitXor
  5. Mul-like, contains Mul, Div, Rem, Shr and Shl
  6. IndexMut
  7. DerefMut
  8. AddAssign-like, contains AddAssign, SubAssign, BitAndAssign, BitOrAssign and BitXorAssign
  9. MulAssign-like, contains MulAssign, DivAssign, RemAssign, ShrAssign and ShlAssign

Static methods

These don't derive traits, but derive static methods instead.

  1. 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.

Generated code

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.

  1. #[derive(From)]
  2. #[derive(Into)]
  3. #[derive(FromStr)]
  4. #[derive(TryInto)]
  5. #[derive(Display)]
  6. #[derive(Index)]
  7. #[derive(Deref)]
  8. #[derive(Not)]
  9. #[derive(Add)]
  10. #[derive(Mul)]
  11. #[derive(IndexMut)]
  12. #[derive(DerefMut)]
  13. #[derive(AddAssign)]
  14. #[derive(MulAssign)]
  15. #[derive(Constructor)]

If you want to be sure what code is generated for your specific type I recommend using the cargo-expand utility. This will show you your code with all macros and derives expanded.

Installation

This library requires Rust 1.15 or higher, so this needs to be installed. Then add the following to Cargo.toml:

[dependencies]
derive_more = "0.13.0"

And this to the top of your Rust file:

#[macro_use]
extern crate derive_more;