#枚举 # #字节 #宏derive

bytenum

Bytenum是一个Rust derive宏,为只有单元变体的枚举创建try_from实现。Bytenum支持所有#[repr(T)]支持的类型。

10个版本

0.1.9 2022年12月12日
0.1.8 2022年12月12日

#107 in #枚举

Download history · Rust 包仓库 76/week @ 2024-03-09 · Rust 包仓库 82/week @ 2024-03-16 · Rust 包仓库 135/week @ 2024-03-23 · Rust 包仓库 297/week @ 2024-03-30 · Rust 包仓库 58/week @ 2024-04-06 · Rust 包仓库 53/week @ 2024-04-13 · Rust 包仓库 47/week @ 2024-04-20 · Rust 包仓库 23/week @ 2024-04-27 · Rust 包仓库 74/week @ 2024-05-04 · Rust 包仓库 96/week @ 2024-05-11 · Rust 包仓库 82/week @ 2024-05-18 · Rust 包仓库 40/week @ 2024-05-25 · Rust 包仓库 52/week @ 2024-06-01 · Rust 包仓库 71/week @ 2024-06-08 · Rust 包仓库 38/week @ 2024-06-15 · Rust 包仓库 95/week @ 2024-06-22 · Rust 包仓库

264 个月下载量

MIT 许可证

6KB
64

Bytenum

Bytenum是一个Rust derive宏,为只有单元变体的枚举创建try_from<T>实现。Bytenum支持所有#[repr(T)]支持的类型。

使用方法

将以下内容添加到您的Cargo.toml

bytenum = "0.1.9"

示例

use bytenum::Bytenum;

#[derive(Bytenum, Debug, PartialEq, Copy, Clone)]
#[repr(u16)]
enum Color {
    Red = 0x04,
    Green = 0x15,
    Blue = 0x34,
}

fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
    assert_eq!(Color::Red, Color::try_from(0x04)?);
    assert_eq!(Color::Green, Color::try_from(0x15)?);
    assert_eq!(Color::Blue, Color::try_from(0x34)?);
    Ok(())
}

依赖关系

~1.5MB
~35K SLoC