[][src]Crate rotate_enum

rotate-enum crate

This crate provides a simple macro that implements prev() and next() methods to an enum.

Motivation

Sometimes you define an enum like this

enum Direction {
    Up,
    Left,
    Down,
    Right,
}

and you want to rotate them in some logic,

let up = Direction::Up;
let left = Direction::Left;
let down = Direction::Down;
let right = Direction::Right;

assert!(up.next() == left);
assert!(left.next() == down);
assert!(down.next() == right);
assert!(right.next() == up);

assert!(up.prev() == right);
assert!(left.prev() == up);
assert!(down.prev() == left);
assert!(right.prev() == down);

You can of course implement these methods manually, but it's repetitive and error prone. Don't you think it should be automated? This crate provides a RotateEnum derive macro to just do this.

Shifting

This crate also provides ShiftEnum, which will exhaust at the end of the enum list, rather than rotating.

let up = Direction::Up;
let left = Direction::Left;
let down = Direction::Down;
let right = Direction::Right;

assert!(up.next() == Some(left));
assert!(left.next() == Some(down));
assert!(down.next() == Some(right));
assert!(right.next() == None);

assert!(up.prev() == None);
assert!(left.prev() == Some(up));
assert!(down.prev() == Some(left));
assert!(right.prev() == Some(down));

Usage

use rotate_enum::RotateEnum;

#[derive(RotateEnum)]
enum Direction {
    Up,
    Left,
    Down,
    Right,
}

Derive Macros

RotateEnum

This derive macro will implement next() and prev() methods that rotates the variant to the annotated enum.

ShiftEnum

This derive macro will implement next() and prev() methods that shifts the variant to the annotated enum.