#枚举 #宏 derive #derive #

enum_variants

使用宏推导 C 类枚举特性 1.1

1 个稳定版本

使用旧的 Rust 2015

1.0.0 2017年2月3日

#457#macro-derive

MIT 许可证

1KB

derivation

使用宏推导 C 类枚举的特性 1.1。

该包提供了 2 个可推导的特性,用于 C 类枚举,即 VariantsFromStr。第一个可以用于将枚举转换为变体列表,第二个可以将从 &str 解析到枚举变得轻而易举。

支持 Rust 1.15 及以上版本。

如何使用

  1. 将这两个包作为依赖项添加到您的 Cargo.toml

    [dependencies]
    
    derivation = "^0.1"
    enum_variants = "^1.0"
    
  2. 现在在您的包源代码顶部导入此包

    #[macro_use]
    extern crate derivation;
    
  3. 特性 Variants 只有一个方法:fn variants() -> Vec<Self>; 应返回所有变体的列表。在需要推导时导入它

    extern crate enum_variants;
    
    use enum_variants::Variants;
    
  4. 像使用其他特性一样推导 FromStrVariants

    #[derive(FromStr, Variants)]
    enum Enum {
        /* ... */
    }
    

注意,只支持 C 类(单一)枚举!

无运行时依赖