7 releases (breaking)

Uses old Rust 2015

0.7.0 Jun 6, 2024
0.6.0 Oct 21, 2018
0.5.0 Oct 20, 2018
0.4.0 Oct 20, 2018
0.1.0 Oct 16, 2018

#525 in Data structures

Download history 75/week @ 2024-08-10 14/week @ 2024-08-17 22/week @ 2024-08-24 40/week @ 2024-08-31 28/week @ 2024-09-07 20/week @ 2024-09-14 55/week @ 2024-09-21 92/week @ 2024-09-28 14/week @ 2024-10-05 44/week @ 2024-10-12 35/week @ 2024-10-19 39/week @ 2024-10-26 46/week @ 2024-11-02 57/week @ 2024-11-09 29/week @ 2024-11-16 33/week @ 2024-11-23

182 downloads per month
Used in 5 crates (2 directly)

MIT/Apache

13KB
258 lines

enum-meta

This crate enables attaching metadata to C-like Enums (or strictly any Enum). The metadata can be of an arbitrary type, but must be of the same type for the all variants although can be different values.

Travis-CI Status

This fills the use-case when the Enum variants are flags for something else -- for example, HTTP error codes, or parts of a syntax tree associated with some explicit string rendering when concretized.

The crate provides two macros which can be used to add this metadata to the enum. This can be done at a separate location from the declaration of the enum. The first macro is for values that are determined at compile time:

enum Colour {
   Red, Orange, Green
}

meta!{
   Colour, &'static str;
   Red, "Red";
   Orange, "Orange";
   Green, "Green";
}

assert_eq!(Red.meta(), "Red");

And the second for where values are calculated at runtime.

pub enum Colour{
    Red,
    Orange,
    Green
}

lazy_meta!{
    Colour, &String, META_Colour2
    Red, format!("{}:{}", 1, "Red");
    Orange, format!("{}:{}", 2, "Orange");
    Green, format!("{}:{}", 3, "Green");
}

The former returns values directly, while the second returns references. Values are only calculated once on first usage.

License

Licensed under either of

at your option.

No runtime deps