Crates.io | enum-repr |
lib.rs | enum-repr |
version | 0.2.6 |
source | src |
created_at | 2018-07-19 17:10:15.730248 |
updated_at | 2020-02-17 16:06:08.495281 |
description | Derive enum repr conversions compatible with type aliases. |
homepage | |
repository | https://github.com/dmnsafonov/enum-repr |
max_upload_size | |
id | 75098 |
size | 27,855 |
Generate enum repr conversions compatible with type aliases. Works on no_std
.
EnumRepr
proc macro takes an type
argument and defines two functions
for the enum used on:
fn repr(&self) -> EnumReprType
fn from_repr(x: EnumReprType) -> Option<Self>
The real enum discriminant still remains isize
.
extern crate enum_repr;
extern crate libc;
use libc::*;
use enum_repr::EnumRepr;
#[EnumRepr(type = "c_int")]
#[derive(Debug, PartialEq)]
pub enum IpProto {
IP = IPPROTO_IP,
IPv6 = IPPROTO_IPV6,
// …
}
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());
}
This project is licensed under either of
at your option.