Hi,
For a state machine I thought this makes sense:
#![allow(unused)]
#![allow(dead_code)]
use std::fmt::Debug;
type TransistionResult = std::result::Result<Box<dyn State>, Box<dyn std::error::Error>>;
type StateBox = Box<dyn State>;
trait State: Debug + Send + Sync {
fn feed(&self, chars: &[u8]) -> TransistionResult;
}
#[derive(Debug)]
struct Hello {
}
impl State for Hello {
fn feed(&self, chars: &[u8]) -> TransistionResult {
println!("Transitioning ...");
Ok(Box::new(Bye {}))
}
}
#[derive(Debug)]
struct Bye {}
impl State for Bye {
fn feed(&self, chars: &[u8]) -> TransistionResult {
println!("Staying in the same state.");
Ok(Box::new(Bye {}))
}
}
fn main() {
let mut state: StateBox = Box::new(Hello {});
let foo = [1u8,2,3];
for _ in 0..2 {
state = state.feed(&foo).unwrap();
}
dbg!(state);
}
It seem to work. However, when in the Bye
state I want to simply return the same Box
again, I struggle:
I made the methods use self
in contrast to &self
which results in a move and returned a Box::new(self)
.
impl State for Bye {
fn feed(self, chars: &[u8]) -> TransistionResult {
println!("Staying in the same state.");
Ok(Box::new(self))
}
}
This does not work:
error[E0161]: cannot move a value of type `dyn State`
--> src/main.rs:39:17
|
39 | state = state.feed(&foo).unwrap();
| ^^^^^ the size of `dyn State` cannot be statically determined
What would be a better solution here?
Thanks,
Philipp