Closed k3d3 closed 1 year ago
Adding a T: 'static
bound should do trick I think.
use statig::prelude::*;
use std::marker::PhantomData;
pub struct Event;
pub enum State<T> {
Init,
Other(PhantomData<T>),
}
impl<T> statig::State<Machine<T>> for State<T>
where
T: 'static, // Added bound here ...
{
fn call_handler(&mut self, machine: &mut Machine<T>, event: &Event) -> Response<Self> {
todo!()
}
}
#[derive(Default)]
pub struct Machine<T> {
_marker: PhantomData<T>,
}
impl<T> statig::StateMachine for Machine<T>
where
T: 'static, // ... and here.
{
type Event<'a> = Event;
type State = State<T>;
type Superstate<'a> = ();
const INITIAL: Self::State = State::Init;
}
fn main() {
let machine = Machine::<u32>::default().state_machine().init();
}
As far as I understand this tells the compiler that T
that can be safely held indefinitely long, including (but not necessarily) up until the end of the program. (link)
Hi, just wanted to check back with you if you managed to solve your problem?
(I probably shouldn't have closed this issue before given you a chance to respond, sorry for that).
Oh, no worries! That did solve my issue, thank you :)
This is tangential to #7, but not completely related. I don't know if I'd consider it a bug, but it's a bit of a pain.
I've been trying to get a state machine working where I have a generic type in the state.
My initial attempt looks something like this:
However I get the error:
Okay, fair enough. So I add to the
Machine
itself to constrain it:But now I get an even stranger error message:
If I change the line to this, as it suggests:
I now get a third different error:
I've tried several things to try getting around the two latter errors, but I've given up sadly. :(
I'm not sure how to get generics to work in state, apart from forking statig and removing every mention of Superstate, which is a very... hacky brute-force solution.
Do you have any ideas on things I could try? I've unfortunately run out of ideas.