enum2repr 0.1.9

EnumRepr is a rust derive macro that creates a try_from<T> implementation for an enum with only unit variants. All types supported by #[repr(T)] are supported by enum2repr.
Documentation
enum2repr-0.1.9 has been yanked.

EnumRepr

EnumRepr is a rust derive macro that creates conversion methods to map between a value and an enum.

All types supported by #[repr(T)] are supported by enum2repr.

Usage

Add this to your Cargo.toml:

enum2repr = "0.1.0"

Example:

use enum2repr::EnumRepr;

#[derive(EnumRepr, Debug, PartialEq, Copy, Clone)]
#[repr(u16)]
enum Color {
    Red = 0x04,
    Green = 0x15,
    Blue = 0x34,
}

#[test]
fn convert_variants() {
    assert_eq!(Ok(Color::Red), Color::try_from(0x04));
    assert_eq!(Ok(Color::Green), Color::try_from(0x15));
    assert_eq!(Ok(Color::Blue), Color::try_from(0x34));
}

#[test]
fn convert_variants_back() {
    assert_eq!(u16::try_from(Color::Red), Ok(0x04));
    assert_eq!(u16::try_from(Color::Green), Ok(0x15));
    assert_eq!(u16::try_from(Color::Blue), Ok(0x34));
}