Last active
August 29, 2015 14:23
-
-
Save Geal/b9c97995371bffa44636 to your computer and use it in GitHub Desktop.
state machine syntax
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
pub enum State { | |
A, | |
B(u8), | |
C(u8), | |
D | |
Error | |
} | |
enum Output { | |
X(u8), | |
Y, | |
Z, | |
Default | |
}; | |
pub fn parse(arg:u8) -> (State, Output); | |
machine!(MyMachine(State) { | |
{ | |
initial: State::A, | |
error: State::Error // Sink state, if the transition is not valid | |
} | |
event [tr1] { | |
State::A => State::B(0), | |
State::B(i) => State::C(i+1) | |
} | |
event [tr2(arg1:u8) -> Option<u8> : None] { // the value after ':' is the default return value | |
State::A => { (State::B(0), Some(42)) } , | |
State::B(i) => { (State::C(i+1), Some(i+1)) } | |
} | |
event [tr3(arg1:u8) -> Output : Output::Default] { | |
State::A => { parse(arg1) }, | |
State::B(i) => { parse(arg1 + i) } | |
} | |
}); | |
let mut m = Machine { state: State::A }; | |
m.tr1(); // returns a Option<()>. None if the current state does not accept this transition | |
m.tr2(42); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment