-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for sub-machines (#13)
- Loading branch information
Showing
29 changed files
with
1,140 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::{EventContext, FsmBackend, lib::*}; | ||
|
||
use super::{FsmEventBuilderState, FsmQueueMock, FsmStateBuilder}; | ||
|
||
|
||
pub struct FsmSubMachineBuilder<TFsm, TContext, TSubMachine> { | ||
pub (crate) _fsm: PhantomData<TFsm>, | ||
pub (crate) _ctx: PhantomData<TContext>, | ||
pub (crate) _sub: PhantomData<TSubMachine>, | ||
pub (crate) _state_builder: FsmStateBuilder<TFsm, TContext, TSubMachine> | ||
} | ||
|
||
impl<TFsm, TContext, TSubMachine> FsmSubMachineBuilder<TFsm, TContext, TSubMachine> | ||
where TFsm: FsmBackend<Context = TContext>, TSubMachine: FsmBackend | ||
{ | ||
/// Adds a context adapter. A referenced context of the parent machine is provided, and a new | ||
/// instance of the submachine's context has to be instantiated. | ||
pub fn with_context<TCtxFactory: Fn(&TContext) -> <TSubMachine as FsmBackend>::Context>(&mut self, _sub_context_factory: TCtxFactory) -> &Self { | ||
self | ||
} | ||
|
||
/// Execute this action when entering the sub-machine state. | ||
pub fn on_entry<'a, TAction: Fn(&mut TSubMachine, &mut EventContext<'a, TFsm, FsmQueueMock<TFsm>>)>(&self, _action: TAction) -> &Self { | ||
self | ||
} | ||
|
||
/// Execute this action when exiting the sub-machine state. | ||
pub fn on_exit<'a, TAction: Fn(&mut TSubMachine, &mut EventContext<'a, TFsm, FsmQueueMock<TFsm>>)>(&self, _action: TAction) -> &Self { | ||
self | ||
} | ||
|
||
/// What happens if we receive this event and we are in this submachine's state right now? | ||
pub fn on_event<TEvent>(&self) -> FsmEventBuilderState<TFsm, TContext, TEvent, TSubMachine> { | ||
FsmEventBuilderState { | ||
_state_builder: &self._state_builder, | ||
_event: PhantomData::default() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use crate::lib::*; | ||
use crate::{EventContext, FsmBackend, FsmBackendImpl, FsmEvent, FsmEventQueue, FsmEventQueueSub, FsmRegionId, FsmResult, Inspect}; | ||
|
||
pub struct DispatchContext<'a, 'b, 'c, F, Q, I> | ||
where F: FsmBackend, | ||
Q: FsmEventQueue<F>, | ||
I: Inspect | ||
{ | ||
pub queue: &'a mut Q, | ||
pub inspect: &'b mut I, | ||
pub backend: &'c mut FsmBackendImpl<F> | ||
} | ||
|
||
impl<'a, 'b, 'c, F, Q, I> DispatchContext<'a, 'b, 'c, F, Q, I> | ||
where F: FsmBackend, | ||
Q: FsmEventQueue<F>, | ||
I: Inspect | ||
{ | ||
|
||
pub fn to_event_context(&'a mut self, region: FsmRegionId) -> EventContext<'a, F, Q> | ||
{ | ||
EventContext { | ||
context: &mut self.backend.context, | ||
queue: self.queue, | ||
region | ||
} | ||
} | ||
} | ||
|
||
/// Used to funnel the event down to the sub-machine. | ||
pub fn dispatch_to_submachine<'a, 'b, 'c, TFsm, TSubMachine, TEvent, Q, I>(ctx: &mut DispatchContext<'a, 'b, 'c, TFsm, Q, I>, ev: &TEvent, inspect_event_ctx: &mut I) | ||
-> FsmResult<()> | ||
where | ||
TFsm: FsmBackend, | ||
<TFsm as FsmBackend>::States: AsMut<TSubMachine>, | ||
TSubMachine: FsmBackend + DerefMut<Target = FsmBackendImpl<TSubMachine>> + FsmBackend<Events = TEvent>, | ||
Q: FsmEventQueue<TFsm>, | ||
I: Inspect, | ||
<TFsm as FsmBackend>::Events: From<<TSubMachine as FsmBackend>::Events>, | ||
TEvent: Clone | ||
{ | ||
let sub_fsm: &mut TSubMachine = ctx.backend.states.as_mut(); | ||
|
||
let mut queue_adapter = FsmEventQueueSub { | ||
parent: ctx.queue, | ||
_parent_fsm: core::marker::PhantomData::<TFsm>::default(), | ||
_sub_fsm: core::marker::PhantomData::<TSubMachine>::default() | ||
}; | ||
|
||
let mut inspect = inspect_event_ctx.for_sub_machine::<TSubMachine>(); | ||
|
||
let sub_dispatch_ctx = DispatchContext { | ||
backend: sub_fsm, | ||
inspect: &mut inspect, | ||
queue: &mut queue_adapter | ||
}; | ||
|
||
<TSubMachine>::dispatch_event(sub_dispatch_ctx, FsmEvent::Event(ev.clone())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.