enum-unitary
Trait and macro for unitary enums
Documentation
The EnumUnitary
trait carries a number of constraints for primitive
conversions iterating over variants of a unitary enum (i.e. enum variants do not
have payloads).
The enum_unitary!
macro defines a new enum implementing EnumUnitary
and
required traits.
Usage
For the macro to derive Sequence
, the enum-iterator
crate must also be added
to Cargo.toml
:
toml
enum-iterator = "1.0"
enum-unitary = "0.5"
Define a unitary enum:
```rust
use enumunitary::{enumunitary, EnumUnitary, FromPrimitive, ToPrimitive};
enum_unitary! {
#[derive(Debug, PartialEq)]
pub enum E {
A, B, C
}
}
asserteq!(enumiterator::cardinality::(), 3);
asserteq!(Into::::into (E::A), 0);
asserteq!(Into::::into (E::B), 1);
asserteq!(Into::::into (E::C), 2);
asserteq!(enumiterator::first::().unwrap(), E::A);
asserteq!(enumiterator::last::().unwrap(), E::C);
let mut i = enumiterator::all::();
asserteq!(i.next(), Some (E::A));
asserteq!(i.next(), Some (E::B));
asserteq!(i.next(), Some (E::C));
asserteq!(i.next(), None);
asserteq!(enumiterator::next (&E::A), Some (E::B));
asserteq!(enumiterator::previous (&E::A), None);
asserteq!(enumiterator::next (&E::B), Some (E::C));
asserteq!(enumiterator::previous (&E::B), Some (E::A));
asserteq!(enumiterator::next (&E::C), None);
asserteq!(enumiterator::previous (&E::C), Some (E::B));
```