Derive Macro TryFromRepr

Source
#[derive(TryFromRepr)]
{
    // Attributes available to this derive:
    #[enumeration]
}
Expand description

Derives TryFrom<Repr> for a C-like enum, where Repr is a primitive representation specified in #[repr(...)].

§Examples

use std::convert::{TryFrom, TryInto};

#[derive(Debug, Clone, Copy, PartialEq, Eq, enum_utils::TryFromRepr)]
#[repr(u8)]
pub enum Direction {
    North = 1,
    East,
    South,
    West
}

use Direction::*;
assert_eq!(North, 1u8.try_into().unwrap());
assert_eq!(West,  4u8.try_into().unwrap());
assert_eq!(Err(()), Direction::try_from(0u8));
assert_eq!(Err(()), Direction::try_from(5u8));

This macro only works on C-like enums.

#[derive(Debug, Clone,  enum_utils::TryFromRepr)]
#[repr(u8)]
pub enum Letter {
    A,
    B,
    Other(u8),
}