Skip to content

Instantly share code, notes, and snippets.

@prepor
Created December 6, 2016 08:58
Show Gist options
  • Save prepor/4e2d33d5f96cd62b1eb91a3813d1c20a to your computer and use it in GitHub Desktop.
Save prepor/4e2d33d5f96cd62b1eb91a3813d1c20a to your computer and use it in GitHub Desktop.
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