All Projects → JelteF → Derive_more

JelteF / Derive_more

Licence: mit
Some more derive(Trait) options

Programming Languages

rust
11053 projects
macros
77 projects

Projects that are alternatives of or similar to Derive more

Saws
A supercharged AWS command line interface (CLI).
Stars: ✭ 4,886 (+655.18%)
Mutual labels:  developer-tools
React Cdk
under development - React Component Development Kit with Storybook
Stars: ✭ 583 (-9.89%)
Mutual labels:  developer-tools
Codeface
Typefaces for source code beautification
Stars: ✭ 5,612 (+767.39%)
Mutual labels:  developer-tools
Lighthouse
Automated auditing, performance metrics, and best practices for the web.
Stars: ✭ 23,903 (+3594.44%)
Mutual labels:  developer-tools
Laravel Ban
Laravel Ban simplify blocking and banning Eloquent models.
Stars: ✭ 572 (-11.59%)
Mutual labels:  trait
Graphqldesigner.com
A developer web-app tool to rapidly prototype a full stack implementation of GraphQL with React.
Stars: ✭ 587 (-9.27%)
Mutual labels:  developer-tools
Playalways
Create Xcode playgrounds from your menu bar
Stars: ✭ 515 (-20.4%)
Mutual labels:  developer-tools
Apicurio Studio
Open Source API Design
Stars: ✭ 638 (-1.39%)
Mutual labels:  developer-tools
Pyment
Format and convert Python docstrings and generates patches
Stars: ✭ 577 (-10.82%)
Mutual labels:  developer-tools
Wsta
A CLI development tool for WebSocket APIs
Stars: ✭ 611 (-5.56%)
Mutual labels:  developer-tools
Shellcheck
ShellCheck, a static analysis tool for shell scripts
Stars: ✭ 27,211 (+4105.72%)
Mutual labels:  developer-tools
Fastcore
Python supercharged for the fastai library
Stars: ✭ 565 (-12.67%)
Mutual labels:  developer-tools
Num
A collection of numeric types and traits for Rust.
Stars: ✭ 592 (-8.5%)
Mutual labels:  trait
Terragrunt
Terragrunt is a thin wrapper for Terraform that provides extra tools for working with multiple Terraform modules.
Stars: ✭ 5,446 (+741.73%)
Mutual labels:  developer-tools
Lunar Unity Console
High-performance Unity iOS/Android logger built with native platform UI
Stars: ✭ 628 (-2.94%)
Mutual labels:  developer-tools
Ggtags
Emacs frontend to GNU Global source code tagging system.
Stars: ✭ 515 (-20.4%)
Mutual labels:  developer-tools
Ram
⚛️ React Application Manager: create and run React (and other) applications – no command line or build setup required
Stars: ✭ 585 (-9.58%)
Mutual labels:  developer-tools
Hostctl
Your dev tool to manage /etc/hosts like a pro!
Stars: ✭ 642 (-0.77%)
Mutual labels:  developer-tools
Mist
A distributed, tag-based pub-sub service for modern web applications and container-driven cloud.
Stars: ✭ 634 (-2.01%)
Mutual labels:  developer-tools
Befriended
Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.
Stars: ✭ 596 (-7.88%)
Mutual labels:  trait

derive_more

Build Status Latest Version Rust Documentation GitHub license Rust 1.36+

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.

Example code

By using this library the following code just works:

extern crate derive_more;
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(fmt = "int: {}", _0)]
    Int(i32),
    Uint(u32),
    #[display(fmt = "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");

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.

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

Conversion traits

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

  1. From
  2. Into
  3. FromStr
  4. TryInto
  5. IntoIterator
  6. AsRef
  7. AsMut

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

Error-handling traits

These traits are used to define error-types.

  1. Error

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, BitXor, MulSelf, DivSelf, RemSelf, ShrSelf and ShlSelf
  5. Mul-like, contains Mul, Div, Rem, Shr and Shl
  6. Sum-like, contains Sum and Product
  7. IndexMut
  8. DerefMut
  9. AddAssign-like, contains AddAssign, SubAssign, BitAndAssign, BitOrAssign and BitXorAssign
  10. 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

Installation

This library requires Rust 1.36 or higher and it supports no_std out of the box. Then add the following to Cargo.toml:

[dependencies]
derive_more = "0.99.0"
# You can specifiy the types of derives that you need for less time spent
# compiling. For the full list of features see this crate its Cargo.toml.
default-features = false
features = ["from", "add", "iterator"]

And this to the top of your Rust file for Rust 2018:

extern crate derive_more;
// use the derives that you want in the file
use derive_more::{Add, Display, From};

If you're still using Rust 2015 you should add this instead:

extern crate core;
#[macro_use]
extern crate derive_more;
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].