enum-unitary
Trait and macro for unitary enums
The EnumUnitary
trait carries a number of constraints and exposes some
methods for working with variants.
The enum_unitary!
macro defines new enum implementing EnumUnitary
and an
additional const function 'count
' returning the number of variants in the
enum.
Add dependency to Cargo.toml
:
toml
[dependencies.enum-unitary]
version = "0.2.*"
git = "git://github.com/spearman/enum-unitary.git"
The count
method generated by the invocation of enum_unitary!
is a const
fn
, so the const_fn
feature must be enabled in addition to importing the
crate:
```rust
```
Define a unitary enum:
rust
enum_unitary! {
pub enum E (EVariants) {
A, B, C
}
}
use enum_unitary::EnumUnitary;
use enum_unitary::num::Bounded;
assert_eq!(E::count(), 3);
assert_eq!(Into::<usize>::into (E::A), 0);
assert_eq!(Into::<usize>::into (E::A), 1);
assert_eq!(Into::<usize>::into (E::A), 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));