Created
March 23, 2018 00:52
-
-
Save mmlinford/ead5d94994bc980652aeff0dfa03626c to your computer and use it in GitHub Desktop.
This file contains 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
struct ThingA; | |
impl ThingA { | |
fn do_action_a(self) -> ThingB { | |
ThingB | |
} | |
} | |
struct ThingB; | |
enum State { | |
A(ThingA), | |
B(ThingB), | |
} | |
struct StateMachine { | |
state: Option<State>, | |
} | |
impl StateMachine { | |
fn new() -> StateMachine { | |
StateMachine { | |
state: Some(State::A(ThingA)), | |
} | |
} | |
fn nudge(&mut self) { | |
let old_state = self.state.take().unwrap(); | |
self.state = match old_state { | |
State::A(my_thing_a) => { | |
println!("transitioning from A -> B"); | |
Some(State::B(my_thing_a.do_action_a())) | |
}, | |
State::B(my_thing_b) => { | |
println!("transitioning from B -> B"); | |
Some(State::B(my_thing_b)) | |
}, | |
} | |
} | |
} | |
fn main() { | |
let mut state_machine = StateMachine::new(); | |
state_machine.nudge(); | |
state_machine.nudge(); | |
println!("done."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment