#enums #repr #discriminant #constant #conversion #proc-macro

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

#1258 in Rust patterns

Download history 14728/week @ 2024-07-21 12684/week @ 2024-07-28 12573/week @ 2024-08-04 13816/week @ 2024-08-11 13052/week @ 2024-08-18 11993/week @ 2024-08-25 11829/week @ 2024-09-01 13096/week @ 2024-09-08 14536/week @ 2024-09-15 14612/week @ 2024-09-22 15643/week @ 2024-09-29 13868/week @ 2024-10-06 13028/week @ 2024-10-13 15618/week @ 2024-10-20 11782/week @ 2024-10-27 17950/week @ 2024-11-03

58,809 downloads per month
Used in 18 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
~36K SLoC