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

#455 in Data structures

Download history 56/week @ 2024-07-22 200/week @ 2024-07-29 212/week @ 2024-08-05 28/week @ 2024-08-12 10/week @ 2024-08-19 44/week @ 2024-08-26 21/week @ 2024-09-02 29/week @ 2024-09-09 19/week @ 2024-09-16 63/week @ 2024-09-23 83/week @ 2024-09-30 12/week @ 2024-10-07 56/week @ 2024-10-14 26/week @ 2024-10-21 50/week @ 2024-10-28 36/week @ 2024-11-04

169 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