Last active
November 4, 2018 18:20
-
-
Save Geal/698790eac1ecbde59c94114dc29a77b3 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
#[derive(Clone, Debug, PartialEq)] | |
pub enum State { | |
Start(Start), | |
End(End), | |
Error(Error), | |
} | |
#[derive(Clone, Debug, PartialEq)] | |
pub struct Start { | |
pub x: u8, | |
} | |
impl Start { | |
pub fn x(&self) -> &u8 { | |
&self.x | |
} | |
pub fn x_mut(&mut self) -> &mut u8 { | |
&mut self.x | |
} | |
} | |
#[derive(Clone, Debug, PartialEq)] | |
pub struct End { | |
pub x: u8, | |
y: bool, | |
} | |
impl End { | |
pub fn x(&self) -> &u8 { | |
&self.x | |
} | |
pub fn x_mut(&mut self) -> &mut u8 { | |
&mut self.x | |
} | |
pub fn y(&self) -> &bool { | |
&self.y | |
} | |
pub fn y_mut(&mut self) -> &mut bool { | |
&mut self.y | |
} | |
} | |
#[derive(Clone, Debug, PartialEq)] | |
pub struct Error {} | |
impl Error {} | |
impl State { | |
pub fn start(x: u8) -> State { | |
State::Start(Start { x }) | |
} | |
pub fn end(x: u8, y: bool) -> State { | |
State::End(End { x, y }) | |
} | |
pub fn error() -> State { | |
State::Error(Error {}) | |
} | |
pub fn x(&self) -> Option<&u8> { | |
match self { | |
State::Start(ref v) => Some(v.x()), | |
State::End(ref v) => Some(v.x()), | |
_ => None, | |
} | |
} | |
pub fn x_mut(&mut self) -> Option<&mut u8> { | |
match self { | |
State::Start(ref mut v) => Some(v.x_mut()), | |
State::End(ref mut v) => Some(v.x_mut()), | |
_ => None, | |
} | |
} | |
pub fn y(&self) -> Option<&bool> { | |
match self { | |
State::End(ref v) => Some(v.y()), | |
_ => None, | |
} | |
} | |
pub fn y_mut(&mut self) -> Option<&mut bool> { | |
match self { | |
State::End(ref mut v) => Some(v.y_mut()), | |
_ => None, | |
} | |
} | |
} |
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
impl State { | |
pub fn on_Msg1(self, input: Msg1) -> Option<State> { | |
match self { | |
State::Start(state) => Some(State::End(state.on_Msg1(input))), | |
State::End(state) => Some(State::End(state.on_Msg1(input))), | |
_ => None, | |
} | |
} | |
pub fn on_Msg2(self, input: Msg2) -> Option<State> { | |
match self { | |
State::Start(state) => Some(State::Error(state.on_Msg2(input))), | |
_ => None, | |
} | |
} | |
} |
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
#[macro_use] | |
extern crate hello_world_derive; | |
pub trait Transitions { | |
fn next(&mut self); | |
} | |
machine!( | |
enum State { | |
Start { pub x:u8 }, | |
End { pub x: u8, y: bool }, | |
Error, | |
} | |
); | |
pub struct Msg1; | |
pub struct Msg2; | |
transitions!(State, | |
[ | |
(Start, Msg1) => End, | |
(End, Msg1) => End, | |
(Start, Msg2) => Error | |
] | |
); | |
fn main() { | |
let start = State::start(0); | |
let end = State::end(1, true); | |
let err = State::error(); | |
assert_eq!(start, State::Start(Start { x: 0 })); | |
} |
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
State :: Error ( state . on_Msg2 ( input ) ) ) , _ => None , } } } [14/1922] | |
error[E0599]: no method named `on_Msg1` found for type `Start` in the current scope | |
--> src/main.rs:19:1 | |
| | |
8 | / machine!( | |
9 | | enum State { | |
10 | | Start { pub x:u8 }, | |
11 | | End { pub x: u8, y: bool }, | |
12 | | Error, | |
13 | | } | |
14 | | ); | |
| |__- method `on_Msg1` not found for this | |
... | |
19 | / transitions!(State, | |
20 | | [ | |
21 | | (Start, Msg1) => End, | |
22 | | (End, Msg1) => End, | |
23 | | (Start, Msg2) => Error | |
24 | | ] | |
25 | | ); | |
| |__^ | |
error[E0599]: no method named `on_Msg1` found for type `End` in the current scope | |
--> src/main.rs:19:1 | |
| | |
8 | / machine!( | |
9 | | enum State { | |
10 | | Start { pub x:u8 }, | |
11 | | End { pub x: u8, y: bool }, | |
12 | | Error, | |
13 | | } | |
14 | | ); | |
| |__- method `on_Msg1` not found for this | |
... | |
19 | / transitions!(State, | |
20 | | [ | |
21 | | (Start, Msg1) => End, | |
22 | | (End, Msg1) => End, | |
23 | | (Start, Msg2) => Error | |
24 | | ] | |
25 | | ); | |
| |__^ | |
error[E0599]: no method named `on_Msg2` found for type `Start` in the current scope | |
--> src/main.rs:19:1 | |
| | |
8 | / machine!( | |
9 | | enum State { | |
10 | | Start { pub x:u8 }, | |
11 | | End { pub x: u8, y: bool }, | |
12 | | Error, | |
13 | | } | |
14 | | ); | |
| |__- method `on_Msg2` not found for this | |
... | |
19 | / transitions!(State, | |
20 | | [ | |
21 | | (Start, Msg1) => End, | |
22 | | (End, Msg1) => End, | |
23 | | (Start, Msg2) => Error | |
24 | | ] | |
"box" 19:19 04-Nov-18 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment