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§
- Float
- Derives
num_traits::Floatfor newtypes. The inner type must already implementFloat. - From
Primitive - Derives
num_traits::FromPrimitivefor simple enums and newtypes. - Num
- Derives
num_traits::Numfor newtypes. The inner type must already implementNum. - NumCast
- Derives
num_traits::NumCastfor newtypes. The inner type must already implementNumCast. - NumOps
- Derives
num_traits::NumOpsfor newtypes. The inner type must already implementNumOps. - One
- Derives
num_traits::Onefor newtypes. The inner type must already implementOne. - Signed
- Derives
num_traits::Signedfor newtypes. The inner type must already implementSigned. - ToPrimitive
- Derives
num_traits::ToPrimitivefor simple enums and newtypes. - Unsigned
- Derives
num_traits::Unsigned. The inner type must already implementUnsigned. - Zero
- Derives
num_traits::Zerofor newtypes. The inner type must already implementZero.