#enums #convert #back #try #inttype

macro inttype-enum

Converts an [enum] into an [inttype], and try to convert it back

5 releases

new 0.1.5 Dec 11, 2024
0.1.4 Aug 29, 2024
0.1.2 Jun 27, 2023

#309 in Procedural macros

Download history 279/week @ 2024-08-26 1/week @ 2024-09-09 24/week @ 2024-09-16 14/week @ 2024-09-23 5/week @ 2024-09-30 2/week @ 2024-10-07 209/week @ 2024-12-09

209 downloads per month

MIT license

6KB
83 lines

inttype-enum

Converts an enum into an inttype, and try to convert it back

Auto implement From<enum> for inttype, and TryFrom<inttype> for enum. if one(only one) variant is tagged with #[default], then From<inttype> for enum will be implemented

Usage examples:

[dependencies]
inttype-enum = "0.1"
#[derive(IntType)]
#[repr(u8)]
enum Cmd {
    Connect = 1,
    Bind = 2,
    Udp = 3,
}

let conn: u8 = Cmd::Connect.into();
assert!(matches!(Cmd::try_from(conn), Ok(Cmd::Connect)));
assert!(matches!(Cmd::try_from(0), Err(_)));
#[derive(IntType)]
#[repr(u8)]
enum Method {
    A = 1,
    B = 2,
    #[default]
    C = 3,
}
assert!(matches!(1.into(), Method::A));
assert!(matches!(0.into(), Method::C));

Dependencies

~220–660KB
~16K SLoC