Crate num_derive

source ·
Expand description

Procedural macros to derive numeric traits in Rust.

§Usage

Add this to your Cargo.toml:

[dependencies]
num-traits = "0.2"
num-derive = "0.3"

Then you can derive traits on your own types:

#[macro_use]
extern crate num_derive;

#[derive(FromPrimitive, ToPrimitive)]
enum Color {
    Red,
    Blue,
    Green,
}

§Explicit import

By default the num_derive procedural macros assume that the num_traits crate is a direct dependency. If num_traits is instead a transitive dependency, the num_traits helper attribute can be used to tell num_derive to use a specific identifier for its imports.

#[macro_use]
extern crate num_derive;
// Lets pretend this is a transitive dependency from another crate
// reexported as `some_other_ident`.
extern crate num_traits as some_other_ident;

#[derive(FromPrimitive, ToPrimitive)]
#[num_traits = "some_other_ident"]
enum Color {
    Red,
    Blue,
    Green,
}

Derive Macros§