2 个版本
0.1.2 | 2023年6月27日 |
---|---|
0.1.1 | 2023年6月26日 |
0.1.0 |
|
1292 在 过程宏 中
6KB
88 行
inttype-enum
自动实现 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