Macro enum_unitary::enum_unitary
[−]
[src]
macro_rules! enum_unitary { ( enum $enum:ident ($iter:ident) { } ) => { ... }; ( pub enum $enum:ident ($iter:ident) { } ) => { ... }; ( enum $enum:ident ($iter:ident) { $singleton:ident } ) => { ... }; ( pub enum $enum:ident ($iter:ident) { $singleton:ident } ) => { ... }; ( enum $enum:ident ($iter:ident) { $first:ident$(, $variant:ident)+ } ) => { ... }; ( enum $enum:ident ($iter:ident) {$($variant:ident),+} {$more:ident$(, $tail:ident)+} ) => { ... }; ( enum $enum:ident ($iter:ident) {$min:ident$(, $variant:ident)*} {$max:ident} ) => { ... }; ( pub enum $enum:ident ($iter:ident) { $first:ident$(, $variant:ident)+ } ) => { ... }; ( pub enum $enum:ident ($iter:ident) {$($variant:ident),+} {$more:ident$(, $tail:ident)+} ) => { ... }; ( pub enum $enum:ident ($iter:ident) {$min:ident$(, $variant:ident)*} {$max:ident} ) => { ... }; }
Wraps "unitary" enums (i.e. enums where variants do not have payloads) with
enum_derive
derives (IterVariants
, NextVariant
, PrevVariant
) and
implements the EnumUnitary
trait.
Also defines a count
const non-trait method that returns the number of
variants in the enum.
Currently the deriving attribute is fixed and can not be overridden, and
explicit discriminators are not allowed: enum variants will be numbered
starting from 0
.
Examples
#![feature(const_fn)] #[macro_use] extern crate enum_unitary; fn main () { enum_unitary! { pub enum E (EVariants) { A, B, C } } use enum_unitary::{EnumUnitary, Bounded}; assert_eq!(E::count(), 3); assert_eq!(Into::<usize>::into (E::A), 0); assert_eq!(Into::<usize>::into (E::B), 1); assert_eq!(Into::<usize>::into (E::C), 2); assert_eq!(E::min_value(), E::A); assert_eq!(E::max_value(), E::C); let mut i = E::iter_variants(); assert_eq!(i.next(), Some (E::A)); assert_eq!(i.next(), Some (E::B)); assert_eq!(i.next(), Some (E::C)); assert_eq!(i.next(), None); assert_eq!(E::A.next_variant(), Some (E::B)); assert_eq!(E::A.prev_variant(), None); assert_eq!(E::B.next_variant(), Some (E::C)); assert_eq!(E::B.prev_variant(), Some (E::A)); assert_eq!(E::C.next_variant(), None); assert_eq!(E::C.prev_variant(), Some (E::B)); }