Created
January 18, 2021 09:04
-
-
Save justanotherdot/33771600e12aec713369941348165e39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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