| ▲ | throwaway894345 a day ago | |||||||||||||||||||||||||
I’m probably doing it wrong, but when there’s a state with multiple transitions out, I can either model it as distinct methods per transition in which case the caller needs to know how to transition between states or I can have the caller pass an enum in which moves the branch into the state machine at the expense of an enum and a match statement. It’s also like 10x the code. Again, I’m very open to the possibility that I’m doing something wrong. Curious how you would model a state machine for (1) reserving the right to do the inventory (2) querying the next page of results (based on a cursor) and (3) recording the page information and the next cursor. | ||||||||||||||||||||||||||
| ▲ | vatsachak a day ago | parent [-] | |||||||||||||||||||||||||
In the type state pattern you would have something like this pub trait ValidState {} struct StateMachine<'a, T> where
{untyped: &'a mut UntypedStateMachine, _marker: PhantomData<T> } fn reserve_right<'a>(state: StateMachine<'a, Begin>) -> StateMachine<'a, Reserved> fn query<'a>(state: StateMachine<'a, Reserved>) -> StateMachine<'a, Queried> fn record<'a>(state: StateMachine<'a, Queried>) -> StateMachine<'a, Recorded> | ||||||||||||||||||||||||||
| ||||||||||||||||||||||||||