4 个版本

使用旧的 Rust 2015

0.1.1 2017年1月9日
0.1.0 2015年8月13日
0.0.2 2015年4月8日
0.0.1 2015年4月7日

2169Rust 模式

Download history · Rust 包仓库 20942/week @ 2024-03-14 · Rust 包仓库 21785/week @ 2024-03-21 · Rust 包仓库 19757/week @ 2024-03-28 · Rust 包仓库 24361/week @ 2024-04-04 · Rust 包仓库 26988/week @ 2024-04-11 · Rust 包仓库 30389/week @ 2024-04-18 · Rust 包仓库 29925/week @ 2024-04-25 · Rust 包仓库 31549/week @ 2024-05-02 · Rust 包仓库 26282/week @ 2024-05-09 · Rust 包仓库 45538/week @ 2024-05-16 · Rust 包仓库 42455/week @ 2024-05-23 · Rust 包仓库 48608/week @ 2024-05-30 · Rust 包仓库 46933/week @ 2024-06-06 · Rust 包仓库 49244/week @ 2024-06-13 · Rust 包仓库 47226/week @ 2024-06-20 · Rust 包仓库 38403/week @ 2024-06-27 · Rust 包仓库

191,599 每月下载量
不到 92 crates 中使用

MIT 许可证

9KB
126

enum_primitive 构建状态

此包导出宏 enum_from_primitive!,它封装了 enum 声明,并自动添加 num::FromPrimitive(在此处重新导出)的实现,以便将原始整数转换为枚举。因此,它提供了内置的 #[derive(FromPrimitive)] 的替代方案,该方案需要不稳定版本的 std::num::FromPrimitive,并且在 Rust 1.0 中已被禁用。

文档

https://andersk.github.io/enum_primitive-rs/enum_primitive/

使用方法

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

[dependencies]
enum_primitive = "*"

使用 #[macro_use] extern crate enum_primitive 导入此包,并在 enum_from_primitive! 宏内封装您的 enum 声明。

示例

#[macro_use] extern crate enum_primitive;
extern crate num;
use num::FromPrimitive;

enum_from_primitive! {
#[derive(Debug, PartialEq)]
enum FooBar {
    Foo = 17,
    Bar = 42,
    Baz,
}
}

fn main() {
    assert_eq!(FooBar::from_i32(17), Some(FooBar::Foo));
    assert_eq!(FooBar::from_i32(42), Some(FooBar::Bar));
    assert_eq!(FooBar::from_i32(43), Some(FooBar::Baz));
    assert_eq!(FooBar::from_i32(91), None);
}

依赖项

~240KB