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§
- Derives
num_traits::Float
for newtypes. The inner type must already implementFloat
. - Derives
num_traits::FromPrimitive
for simple enums and newtypes. - Derives
num_traits::Num
for newtypes. The inner type must already implementNum
. - Derives
num_traits::NumCast
for newtypes. The inner type must already implementNumCast
. - Derives
num_traits::NumOps
for newtypes. The inner type must already implementNumOps
. - Derives
num_traits::One
for newtypes. The inner type must already implementOne
. - Derives
num_traits::Signed
for newtypes. The inner type must already implementSigned
. - Derives
num_traits::ToPrimitive
for simple enums and newtypes. - Derives
num_traits::Unsigned
. The inner type must already implementUnsigned
. - Derives
num_traits::Zero
for newtypes. The inner type must already implementZero
.