Crates.io | enum-rotate |
lib.rs | enum-rotate |
version | 0.1.1 |
source | src |
created_at | 2024-03-25 08:36:58.252999 |
updated_at | 2024-03-25 09:38:40.947277 |
description | Rotate and iterate your enums |
homepage | |
repository | https://github.com/gorilskij/enum-rotate |
max_upload_size | |
id | 1185079 |
size | 26,997 |
Treat your enums as iterators.
This crate provides the EnumRotate
trait along with the accompanying derive macro allowing you to
use enum_rotate::EnumRotate;
use Enum::*;
#[derive(EnumRotate, PartialEq)]
enum Enum { A, B, C }
fn main() {
assert_eq!(A.next(), B);
assert_eq!(A.prev(), C);
assert_eq!(
Enum::iter().collect::<Vec<_>>(),
vec![A, B, C],
);
assert_eq!(
C.iter_from().collect::<Vec<_>>(),
vec![C, A, B],
);
}
It is also possible to specify a custom iteration order for the enum variants.
use enum_rotate::EnumRotate;
use Enum::*;
#[derive(EnumRotate, PartialEq)]
#[iteration_order(B, A, C)]
enum Enum { A, B, C }
fn main() {
assert_eq!(
Enum::iter().collect::<Vec<_>>(),
vec![B, A, C],
);
}