#derive #library #rust

stupid-from-num

自动为枚举实现 From 特性

1 个不稳定版本

0.1.0 2023年11月8日

#316 in #library


用于 dbeel

Apache-2.0 OR MIT

5KB
69

一个 derive 宏,为任何枚举添加 impl From<u8>

如果项目数量大于 u8::MAX,它将使用 u16,然后如果大于 u16::MAX,则使用 u32

示例

use stupid_from_num::FromNum;

#[derive(FromNum)]
enum Example {
    Zero,
    One,
    Two,
}

assert_eq!(Example::Zero.into(), 0);
assert_eq!(Example::One.into(), 1);
assert_eq!(Example::Two.into(), 2);

依赖项

~300–760KB
~18K SLoC