#enums #macro-derive #derive #macro

macro derivation

Deriving traits for C-like enums using macro 1.1

1 unstable release

Uses old Rust 2015

0.1.0 Feb 3, 2017

#106 in #enum

21 downloads per month

MIT license

5KB
101 lines

derivation

Deriving traits for C-like enums using macro 1.1.

This crate makes 2 traits derivable for C-like enums, namely Variants and FromStr. The first one could be derived for enums to transform it into a list of variants, and the second make parsing from &str to an enum easy as a piece of cake.

Supports Rust 1.15 and above.

How to use:

  1. Add these 2 crate as dependencies in your Cargo.toml

    [dependencies]
    
    derivation = "^0.1"
    enum_variants = "^1.0"
    
  2. Now import this crate in your source code at the top of your crate

    #[macro_use]
    extern crate derivation;
    
  3. The trait Variants has only one method: fn variants() -> Vec<Self>; which should return the list of all variants. Import it when you need to derive it:

    extern crate enum_variants;
    
    use enum_variants::Variants;
    
  4. Derive FromStr or Variants like you do with other traits:

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

Notice that only C-like (unitary) enums are supported!

Dependencies

~1.5MB
~40K SLoC