#enums #macro #defines #sequential #indexed #zero #zero-based

macro itsuki

macro that defines a simple zero-based sequential enum

4 releases

0.2.0 May 11, 2024
0.1.2 Apr 15, 2024
0.1.1 Apr 7, 2024
0.1.0 Mar 31, 2024

#28 in #indexed

Download history 14/week @ 2024-07-15 5/week @ 2024-07-22 7/week @ 2024-07-29 15/week @ 2024-08-26 32/week @ 2024-09-02 28/week @ 2024-09-09 23/week @ 2024-09-16 21/week @ 2024-09-23 35/week @ 2024-09-30 23/week @ 2024-10-07 15/week @ 2024-10-14 23/week @ 2024-10-28

64 downloads per month
Used in 2 crates

MIT license

14KB
245 lines

itsuki

Crate Status

macro that defines a simple zero-based sequential enum ⭐️

Usage

Function-like

use itsuki::define_zero_indexed_enum;

define_zero_indexed_enum! {
    Quintuplets => [Ichika, Nino, Miku, Yotsuba, Itsuki]
}

let miku = Quintuplets::Miku;

assert_eq!(miku, Quintuplets::Miku);
assert_ne!(miku, Quintuplets::Nino);

assert_eq!(Quintuplets::len(), 5);

use Quintuplets::*;

assert_eq!(Quintuplets::vars_vec(), vec![Ichika, Nino, Miku, Yotsuba, Itsuki]);
assert_eq!(Quintuplets::vars_array(), [Ichika, Nino, Miku, Yotsuba, Itsuki]);

assert_eq!(Nino.next(), Miku);
assert_eq!(Itsuki.next(), Ichika);

assert_eq!(Yotsuba.prev(), Miku);
assert_eq!(Ichika.prev(), Itsuki);

assert_eq!(Ichika.next_in(|q| [Miku, Yotsuba].contains(&q)), Miku);
assert_eq!(Miku.next_in(|q| [Miku, Yotsuba].contains(&q)), Yotsuba);

assert_eq!(Nino.prev_in(|q| [Miku, Yotsuba].contains(&q)), Yotsuba);
assert_eq!(Yotsuba.prev_in(|q| [Miku, Yotsuba].contains(&q)), Miku);

assert_eq!(Miku.val(), 2);
assert_eq!(Yotsuba.val(), 3);

assert_eq!(Quintuplets::try_from(0), Ok(Ichika));
assert_eq!(Quintuplets::try_from(4), Ok(Itsuki));
assert_eq!(Quintuplets::try_from(5), Err(()));

assert_eq!(1.try_into(), Ok(Nino));
assert_eq!(3.try_into(), Ok(Yotsuba));

Derive

use itsuki::ZeroIndexedEnum;

#[derive(ZeroIndexedEnum, Debug, Clone, Copy, PartialEq, Eq)]
enum Quintuplets {
    Ichika,
    Nino,
    Miku,
    Yotsuba,
    Itsuki,
}

Attribute

use itsuki::zero_indexed_enum;

#[zero_indexed_enum]
enum Quintuplets {
    Ichika,
    Nino,
    Miku,
    Yotsuba,
    Itsuki,
}

License

MIT

Dependencies

~230–670KB
~16K SLoC