Last active
March 22, 2018 15:42
-
-
Save mmlinford/c99655a37391ae7bc7fc5da39e73a75b to your computer and use it in GitHub Desktop.
Rust State Machine
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: State, | |
} | |
impl StateMachine { | |
fn new() -> StateMachine { | |
StateMachine { | |
state: State::A(ThingA), | |
} | |
} | |
fn nudge(&mut self) { | |
self.state = match self.state { | |
State::A(my_thing_a) => State::B(my_thing_a.do_action_a()), | |
State::B(my_thing_b) => State::B(my_thing_b), | |
} | |
} | |
} | |
fn main() { | |
let mut state_machine = StateMachine::new(); | |
state_machine.nudge(); | |
println!("done."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment