derive-try-from-primitive

Crates.ioderive-try-from-primitive
lib.rsderive-try-from-primitive
version1.0.0
sourcesrc
created_at2017-07-12 03:11:41.159003
updated_at2020-03-30 01:26:48.044865
descriptionCustom derive a try_from method for primitive to enum conversions
homepagehttps://github.com/jeffbelgum/derive-try-from-primitive
repositoryhttps://github.com/jeffbelgum/derive-try-from-primitive
max_upload_size
id23024
size10,865
Jeff Belgum (JeffBelgum)

documentation

https://docs.rs/derive-try-from-primitive/

README

Example

use core::convert::TryFrom;
use derive_try_from_primitive::TryFromPrimitive;


#[derive(TryFromPrimitive)]
#[repr(u16)]
enum Foo {
    Bar,
    Baz = 100,
    Quix = 200,
}

// Generated Code:
impl core::convert::TryFrom<u16> for Foo {
    type Error = u16;

    fn try_from(n: 16) -> Result<Self, Self::Error> {
        match n {
            0 => Ok(Foo::Bar),
            100 => Ok(Foo::Baz),
            200 => Ok(Foo::Quix),
            _ => Err(n),
        }
    }
}

fn main() {
    let bar = Foo::try_from(0);
    let baz = Foo::try_from(100);
    let quix = Foo::try_from(200);
    let bad = Foo::try_from(300);
    assert_eq!(bar.unwrap() as u16, 0);
    assert_eq!(baz.unwrap() as u16, 100);
    assert_eq!(quix.unwrap() as u16, 200);
    if let Err(value) = bad {
        assert_eq!(value, 300, "Input is returned for convenience");
    }
}
Commit count: 13

cargo fmt