Skip to content

Instantly share code, notes, and snippets.

@mmlinford
Created March 23, 2018 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmlinford/ead5d94994bc980652aeff0dfa03626c to your computer and use it in GitHub Desktop.
Save mmlinford/ead5d94994bc980652aeff0dfa03626c to your computer and use it in GitHub Desktop.
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