#枚举 #转换 #反向 #尝试 #inttype

inttype-enum

将一个[枚举]转换为[inttype],并尝试将其转换回

2 个版本

0.1.2 2023年6月27日
0.1.1 2023年6月26日
0.1.0 2023年6月26日

1292过程宏

MIT 许可证

6KB
88

inttype-enum

将一个 枚举 转换为 inttype,并尝试将其转换回

自动实现 From<枚举> for inttype,以及 TryFrom<inttype> for 枚举。如果一个(只有一个)变体被标记为 #[default],则将实现 From<inttype> for 枚举

使用示例

[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));

依赖项

~270–720KB
~17K SLoC