March 2018
Intermediate to advanced
272 pages
7h 4m
English
Let's first think about how to implement this in a C/C++ environment. You will probably have a global state and then a while loop that would change the state after each iteration. There are, of course, many ways of implementing a state machine but, in C, all of them require either metaprogramming or a runtime evaluation of the global state.
In Rust, we have a Turing-complete type system, so why not try and use it to create that state machine? Let's start by defining some traits that will have the power of creating the state machine. We first define a StateMachine trait, that will have the functionality of moving from one state to another state:
pub trait StateMachine { type Next: MainLogic; fn execute(self) -> Self::Next; ...Read now
Unlock full access