enum-repr 0.1.2

Derive enum repr conversions compatible with type aliases.
Documentation

enum-repr

Build Status Crates.io Documentation

Derive enum repr conversions compatible with type aliases. Works on no_std.

EnumRepr proc macro takes an EnumReprType argument and defines two functions for the enum derived on: fn repr(&self) -> EnumReprType and fn from_repr(x: EnumReprType) -> Option<Self>. The real enum discriminant still remains isize.

#[macro_use] extern crate enum_repr;
extern crate libc;

use libc::*;

#[derive(Debug, PartialEq)]
#[derive(EnumRepr)]
#[EnumReprType = "c_int"]
pub enum IpProto {
    IP = IPPROTO_IP as isize,
    IPv6 = IPPROTO_IPV6 as isize,
    //}

fn main() {
    assert_eq!(IpProto::IP.repr(), IPPROTO_IP);
    assert_eq!(IpProto::from_repr(IPPROTO_IPV6), Some(IpProto::IPv6));
    assert!(IpProto::from_repr(12345).is_none());
}