pub trait EnumCycle {
// Required methods
fn up(&self) -> Self;
fn down(&self) -> Self;
}Expand description
This trait is the central piece to move up and down an Enum.
By using #[derive(EnumCycle] you can save yourself the hassle of
having to write the implementation.
§Example
//Bring it into scope
use enum_cycling::EnumCycle;
#[derive(PartialEq, Debug, EnumCycle)]
enum MainMenu {
NewGame,
#[skip]
Secret,
Continue,
Quit,
}
fn main() {
assert_eq!(MainMenu::NewGame.down(), MainMenu::Continue);
}Additionally, you may use the ‘cycle’ attribute in order to keep the order of your cycle independant from the enum Variant order. This allows you to keep your enum variants alphabetically sorted, while also using EnumCycle.
use enum_cycling::EnumCycle;
#[derive(PartialEq, Debug, EnumCycle)]
#[cycle(NewGame, Continue, Quit)]
enum MainMenu {
Continue,
NewGame,
Quit,
Secret,
}The generated code should look something along the lines of:
impl EnumCycle for MainMenu {
fn up(&self) -> Self {
match *self {
Self::NewGame => Self::Quit,
Self::Continue => Self::NewGame,
Self::Quit => Self::Continue,
_ => panic!("Unable to call 'up' on a skipped variant"),
}
}
fn down(&self) -> Self {
match *self {
Self::NewGame => Self::Continue,
Self::Continue => Self::Quit,
Self::Quit => Self::NewGame,
_ => panic!("Unable to call 'down' on a skipped variant"),
}
}
}Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.