mirror of
https://github.com/PacktPublishing/Rust-High-Performance.git
synced 2026-01-25 02:34:19 +09:00
41 lines
618 B
Rust
41 lines
618 B
Rust
pub trait StateMachine {
|
|
type Next: MainLogic;
|
|
fn execute(self) -> Self::Next;
|
|
}
|
|
|
|
pub trait MainLogic {
|
|
fn main_logic(self);
|
|
}
|
|
|
|
impl<S> MainLogic for S
|
|
where
|
|
S: StateMachine,
|
|
{
|
|
fn main_logic(self) {
|
|
self.execute().main_logic();
|
|
}
|
|
}
|
|
|
|
struct FirstState;
|
|
struct LastState;
|
|
|
|
impl StateMachine for FirstState {
|
|
type Next = LastState;
|
|
|
|
fn execute(self) -> Self::Next {
|
|
unimplemented!()
|
|
}
|
|
}
|
|
|
|
impl MainLogic for LastState {
|
|
fn main_logic(self) {
|
|
unimplemented!()
|
|
}
|
|
}
|
|
|
|
// Will panic
|
|
fn main() {
|
|
let start = FirstState;
|
|
start.execute().main_logic();
|
|
}
|