inttype-enum

Crates.iointtype-enum
lib.rsinttype-enum
version0.1.4
sourcesrc
created_at2023-06-26 08:25:27.378174
updated_at2024-08-29 05:18:44.603255
descriptionConverts an [`enum`] into an [`inttype`], and try to convert it back
homepage
repositoryhttps://github.com/hangj/inttype-enum
max_upload_size
id900148
size5,991
hangj (hangj)

documentation

https://docs.rs/inttype-enum/

README

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));
Commit count: 6

cargo fmt