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 |
#374 in Data structures
197 downloads per month
Used in 5 crates
(2 directly)
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.
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
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.