10 个稳定版本
1.1.9 | 2023 年 2 月 5 日 |
---|---|
1.1.8 | 2023 年 1 月 28 日 |
#462 在 Rust 模式
每月 78 次下载
在 warlocks-cauldron 中使用
9KB
153 代码行
valued-enums
宏集合和指定用于创建有值或类似 Python 枚举的 trait。
安装
[dependencies.valued-enums]
version = "1.1.9"
#git = "https://github.com/hack-wrench/valued-enums"
示例
在编写您的枚举集合时,我建议使用 pub
来在其他项目中扩展 trait。
use valued_enums::*;
py_enum! {
PyLikeEnum(u8):
ONE = 1
TWO = 2
THREE = 3
// Do not forget that this type of enumeration
// does not check the uniqueness of the value!
// ::from_value will return the element that is encountered first.
DUPLICATE = 3
}
rust_enum! {
enum RustLikeEnum((u8, u8)) {
ONE = (1, 1),
TWO = (2, 2),
THREE = (3, 3),
// Similar atention for any ValuedEnum
DUPLICATE = (3, 3),
}
}
mod some_inner_module {
use crate::valued_enum;
valued_enum! {
#[derive(Debug)]
pub enum VisibleCustomizeEnum(&'static str) {
pub(crate) CRATE_VISIBLE = "crate",
pub PUBLIC_VISIBLE = "public",
PRIVATE = "private",
}
}
}
// To use the match and equal pattern, you must implement #[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq)]
struct Point(i32, i32);
py_enum! {
PointEnum(Point):
A = Point(1, 2)
B = Point(3, 4)
}
fn main() {
println!("Equal enum with their value type: {}", PyLikeEnum::ONE.equal(&1));
println!("Get key: {}", RustLikeEnum::ONE.key());
println!("Get value: {}", PyLikeEnum::TWO.value());
// May help in the case of a whole enum owned
println!("Get ref value: &{}", PyLikeEnum::THREE.ref_value());
println!("Get all keys: {:?}", RustLikeEnum::keys());
println!("Get all values: {:?}", PyLikeEnum::values());
println!("Get all variants: {:?}", some_inner_module::VisibleCustomizeEnum::variants());
// Convert to private field can be dangerous!
println!("Convert from title to enum: {}", some_inner_module::VisibleCustomizeEnum::from_key("PRIVATE").unwrap().value());
// Don't forget to implement for your enum: #[derive(PartialEq, Eq)]
println!("Convert from value to enum: {}", PyLikeEnum::from_value(&3).unwrap().key());
}