#repr #enums

macro no-std enum-repr

Derive enum repr conversions compatible with type aliases

10 releases

0.2.6 Feb 17, 2020
0.2.5 Oct 1, 2019
0.2.4 Mar 16, 2019
0.2.3 Jan 20, 2019
0.1.1 Jul 21, 2018

#429 in No standard library

Download history 30569/week @ 2024-06-19 31115/week @ 2024-06-26 16606/week @ 2024-07-03 16464/week @ 2024-07-10 13820/week @ 2024-07-17 14446/week @ 2024-07-24 10918/week @ 2024-07-31 14662/week @ 2024-08-07 13011/week @ 2024-08-14 12396/week @ 2024-08-21 10674/week @ 2024-08-28 14685/week @ 2024-09-04 12268/week @ 2024-09-11 15982/week @ 2024-09-18 13432/week @ 2024-09-25 13389/week @ 2024-10-02

58,223 downloads per month
Used in 19 crates (8 directly)

MIT/Apache

15KB
179 lines

enum-repr

Build Status Crates.io Documentation

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());
}

License

This project is licensed under either of

at your option.

Dependencies

~1.5MB
~35K SLoC