#enums #repr #discriminant #constant

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

#23 in #discriminant

Download history 4432/week @ 2023-11-16 3164/week @ 2023-11-23 4293/week @ 2023-11-30 4355/week @ 2023-12-07 3166/week @ 2023-12-14 1004/week @ 2023-12-21 1269/week @ 2023-12-28 2657/week @ 2024-01-04 3232/week @ 2024-01-11 3711/week @ 2024-01-18 3411/week @ 2024-01-25 3566/week @ 2024-02-01 3882/week @ 2024-02-08 5444/week @ 2024-02-15 5532/week @ 2024-02-22 6738/week @ 2024-02-29

22,261 downloads per month
Used in 9 crates (7 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
~33K SLoC