Crate enum_meta[][src]

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:

Syntax

This example is not tested
meta! {
  EnumType, MetaDataType;
  VariantOne, "MetadataValue";
  VariantTwo, "MetadataValue";
  VariantThree, "MetadataValue";
}

In this case, the type of the metadata must be defined before hand and will be either a reference type or a copy type, as the values will be returned statically. For example:

#[macro_use] extern crate enum_meta;
use enum_meta::*;
enum Colour
{
   Red, Orange, Green
}

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

fn main() {
   assert_eq!(Colour::Orange.meta(), "Orange");
}

A second macro allows the values to be calculated at run time on first access. The values are calculated only once.

#[macro_use] extern crate enum_meta;
use enum_meta::*;
pub enum Colour{
    Red,
    Orange,
    Green
}

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

fn main() {
   assert_eq!(Colour::Red.meta(), "1:Red");
}

In this case, values are stored in a global variable whose name is provided (META_Colour2 in this instance). Values returned are references to the given return type.

Re-exports

pub use lazy_static::*;

Macros

lazy_meta
lazy_static
meta

Structs

Discriminant

Opaque type representing the discriminant of an enum.

HashMap

A hash map implemented with linear probing and Robin Hood bucket stealing.

Traits

Meta

Trait for accessing metadata

Functions

discriminant

Returns a value uniquely identifying the enum variant in v.