Function enum_tryfrom_derive::from_primitive [] [src]

pub fn from_primitive(input: TokenStream) -> TokenStream

Generate TryFrom for each primitive type mentioned as a TryFromPrimitiveType attribute.

When combined with unwrap, this is as fast as a bounds check + transmute, at least for small enums. See the benches folder for benchmarks.

Examples

#![feature(try_from)]
use std::convert::TryFrom;

extern crate enum_tryfrom;

#[macro_use] extern crate enum_tryfrom_derive;
#[derive(PartialEq,Debug,TryFromPrimitive)]
#[TryFromPrimitiveType="u32"]
enum Foo {
    FirstFoo = 1,
    SecondFoo,
    ThirdFoo,
}

let v : u32 = 2;
assert_eq!(Foo::try_from(v).unwrap(), Foo::SecondFoo);