Created
December 6, 2016 08:58
-
-
Save prepor/4e2d33d5f96cd62b1eb91a3813d1c20a 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
| enum State { | |
| Init, Started, Stopped | |
| } | |
| enum InitEvent { | |
| Start, Stop | |
| } | |
| enum StartedEvent { | |
| Stop | |
| } | |
| enum StoppedEvent { | |
| Start | |
| } | |
| fn init_event() -> InitEvent { | |
| return InitEvent::Start; | |
| } | |
| fn started_event() -> StartedEvent { | |
| return StartedEvent::Stop; | |
| } | |
| fn stopped_event() -> StoppedEvent { | |
| return StoppedEvent::Start; | |
| } | |
| fn apply(s: State) -> State { | |
| match s { | |
| State::Init => match init_event() { | |
| InitEvent::Start => State::Started, | |
| InitEvent::Stop => State::Stopped, | |
| }, | |
| State::Started => match started_event() { | |
| StartedEvent::Stop => State::Stopped, | |
| }, | |
| State::Stopped => match stopped_event() { | |
| StoppedEvent::Start => State::Started, | |
| } | |
| } | |
| } | |
| fn state_loop(init: State) { | |
| let mut s = init; | |
| loop { | |
| s = apply(s); | |
| } | |
| } | |
| fn main() { | |
| state_loop(State::Init) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment