14 releases

0.4.4 Dec 8, 2021
0.4.3 Aug 11, 2021
0.4.2 May 25, 2020
0.4.1 Oct 9, 2019
0.3.3 May 11, 2018

#534 in Rust patterns

Download history 87/week @ 2022-03-11 96/week @ 2022-03-18 26/week @ 2022-03-25 57/week @ 2022-04-01 40/week @ 2022-04-08 64/week @ 2022-04-15 41/week @ 2022-04-22 111/week @ 2022-04-29 121/week @ 2022-05-06 304/week @ 2022-05-13 83/week @ 2022-05-20 153/week @ 2022-05-27 156/week @ 2022-06-03 44/week @ 2022-06-10 39/week @ 2022-06-17 39/week @ 2022-06-24

298 downloads per month
Used in 2 crates

Apache-2.0

26KB
695 lines

enum-unitary

Trait and macro for unitary enums

Documentation

The EnumUnitary trait carries a number of constraints for primitive conversions and limits and 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 IntoEnumIterator, the enum-iterator crate must also be added to Cargo.toml:

enum-iterator = "0.5"
enum-unitary = "0.4"

Define a unitary enum:

use enum_unitary::{enum_unitary, EnumUnitary, Bounded};
enum_unitary! {
  #[derive(Debug, PartialEq)]
  pub enum E {
    A, B, C
  }
}
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));

Dependencies

~0.4–0.8MB
~19K SLoC