Skip to content

Instantly share code, notes, and snippets.

@justanotherdot
Created January 18, 2021 09:04
Show Gist options
  • Save justanotherdot/33771600e12aec713369941348165e39 to your computer and use it in GitHub Desktop.
Save justanotherdot/33771600e12aec713369941348165e39 to your computer and use it in GitHub Desktop.
#![feature(step_trait)]
#![feature(step_trait_ext)]
// This is unsightly.
use std::iter::Step;
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone)]
enum Thing {
One,
Two,
Three,
}
unsafe impl Step for Thing {
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
let delta = end.clone() as i64 - start.clone() as i64;
if delta < 0 {
return None;
}
return Some(delta as usize);
}
fn forward_checked(start: Self, count: usize) -> Option<Self> {
match start {
Thing::One => {
if count == 0 {
return Some(start);
} else if count > 2 {
return None;
} else {
if count == 1 {
return Some(Thing::Two);
} else {
return Some(Thing::Three);
}
}
},
Thing::Two => {
if count == 0 {
return Some(start);
} else if count > 1 {
return None;
} else {
return Some(Thing::Three);
}
},
Thing::Three => {
if count == 0 {
return Some(start);
}
return None;
}
}
}
fn backward_checked(start: Self, count: usize) -> Option<Self> {
match start{
Thing::Three => {
if count == 0 {
return Some(start);
} else if count > 2 {
return None;
} else {
if count == 1 {
return Some(Thing::Two);
} else {
return Some(Thing::One);
}
}
},
Thing::Two => {
if count == 0 {
return Some(start);
} else if count > 1 {
return None;
} else {
return Some(Thing::One);
}
},
Thing::One => {
if count == 0 {
return Some(start);
}
return None;
}
}
}
}
fn main() {
for thing in Thing::One..=Thing::Three {
dbg!(thing);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment