Skip to content

Commit

Permalink
use the specific state type instead of the entire FSM
Browse files Browse the repository at this point in the history
  • Loading branch information
rudib committed Oct 9, 2021
1 parent 1195576 commit b32358c
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 28 deletions.
20 changes: 6 additions & 14 deletions finny/src/fsm/inspect/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
use core::fmt::Debug;
use core::any::Any;

use crate::{FsmBackend, FsmBackendImpl, FsmEvent, FsmStates};

#[derive(Debug)]
pub enum InspectFsmEvent<F> where F: FsmBackend {
StateEnter(<<F as FsmBackend>::States as FsmStates<F>>::StateKind),
StateExit(<<F as FsmBackend>::States as FsmStates<F>>::StateKind)
}

impl<F> Clone for InspectFsmEvent<F> where F: FsmBackend {
fn clone(&self) -> Self {
match self {
Self::StateEnter(arg0) => Self::StateEnter(arg0.clone()),
Self::StateExit(arg0) => Self::StateExit(arg0.clone()),
}
}
#[derive(Debug, Clone)]
pub enum InspectFsmEvent<S> where S: Debug + Clone {
StateEnter(S),
StateExit(S)
}

pub trait Inspect: InspectEvent {
Expand All @@ -36,5 +28,5 @@ pub trait Inspect: InspectEvent {
}

pub trait InspectEvent {
fn on_event<F: FsmBackend + Any>(&self, event: InspectFsmEvent<F>);
fn on_event<S: Any + Debug + Clone>(&self, event: &InspectFsmEvent<S>);
}
2 changes: 1 addition & 1 deletion finny/src/fsm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub type FsmDispatchResult = FsmResult<()>;

/// Finite State Machine backend. Handles the dispatching, the types are
/// defined by the code generator.
pub trait FsmBackend where Self: Sized + Debug + 'static {
pub trait FsmBackend where Self: Sized + Debug {
/// The machine's context that is shared between its constructors and actions.
type Context;
/// The type that holds the states of the machine.
Expand Down
4 changes: 2 additions & 2 deletions finny/src/fsm/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::FsmResult;
/// The implementation should hold all of the FSM's states as fields.
pub trait FsmStates<TFsm>: FsmStateFactory<TFsm> where TFsm: FsmBackend {
/// The enum type for all states that's used as the "current state" field in the FSM's backend.
type StateKind: Clone + Copy + Debug + PartialEq;
type StateKind: Clone + Copy + Debug + PartialEq + 'static;
/// An array of current states for the machine, one for each region.
type CurrentState: Clone + Copy + Debug + Default + AsRef<[FsmCurrentState<Self::StateKind>]> + AsMut<[FsmCurrentState<Self::StateKind>]>;
type CurrentState: Clone + Copy + Debug + Default + AsRef<[FsmCurrentState<Self::StateKind>]> + AsMut<[FsmCurrentState<Self::StateKind>]> + 'static;
}

/// The current state of the FSM.
Expand Down
8 changes: 4 additions & 4 deletions finny/src/fsm/transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub trait FsmState<F: FsmBackend> where Self: Sized {
context.inspect.on_state_enter::<Self>();

let kind = <Self>::fsm_state();
let ev = InspectFsmEvent::<F>::StateEnter(kind);
context.inspect.on_event(ev);
let ev = InspectFsmEvent::StateEnter(kind);
context.inspect.on_event(&ev);
}

let state: &mut Self = context.backend.states.as_mut();
Expand All @@ -51,8 +51,8 @@ pub trait FsmState<F: FsmBackend> where Self: Sized {
context.inspect.on_state_exit::<Self>();

let kind = <Self>::fsm_state();
let ev = InspectFsmEvent::<F>::StateExit(kind);
context.inspect.on_event(ev);
let ev = InspectFsmEvent::StateExit(kind);
context.inspect.on_event(&ev);
}
}

Expand Down
5 changes: 3 additions & 2 deletions finny/src/inspect/chain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{FsmBackend, FsmBackendImpl, FsmEvent, Inspect, InspectEvent, InspectFsmEvent};
use core::any::Any;
use core::fmt::Debug;
use super::{null::InspectNull};


Expand Down Expand Up @@ -110,8 +111,8 @@ impl<A, B> Inspect for InspectChain<A, B>
impl<A, B> InspectEvent for InspectChain<A, B>
where A: Inspect, B: Inspect
{
fn on_event<F: FsmBackend + Any>(&self, event: InspectFsmEvent<F>) {
self.a.on_event(event.clone());
fn on_event<S: Any + Debug + Clone>(&self, event: &InspectFsmEvent<S>) {
self.a.on_event(event);
self.b.on_event(event);
}
}
5 changes: 3 additions & 2 deletions finny/src/inspect/events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{FsmBackend, FsmBackendImpl, FsmEvent, Inspect, InspectEvent};
use crate::{FsmBackend, FsmBackendImpl, FsmEvent, Inspect, InspectEvent, InspectFsmEvent};
use core::any::Any;
use core::fmt::Debug;

#[derive(Clone)]
pub struct EventInspector<T>
Expand Down Expand Up @@ -69,7 +70,7 @@ impl<TI> Inspect for EventInspector<TI>
impl<TI> InspectEvent for EventInspector<TI>
where TI: InspectEvent + Clone
{
fn on_event<F: FsmBackend + Any>(&self, event: crate::InspectFsmEvent<F>) {
fn on_event<S: Any + Debug + Clone>(&self, event: &InspectFsmEvent<S>) {
self.event_handler.on_event(event)
}
}
5 changes: 3 additions & 2 deletions finny/src/inspect/null.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{FsmBackend, FsmBackendImpl, FsmEvent, Inspect, InspectEvent, InspectFsmEvent};

use core::fmt::Debug;
use core::any::Any;

#[derive(Default)]
pub struct InspectNull;
Expand Down Expand Up @@ -57,7 +58,7 @@ impl Inspect for InspectNull {
}

impl InspectEvent for InspectNull {
fn on_event<F: FsmBackend>(&self, event: InspectFsmEvent<F>) {
fn on_event<S: Any + Debug + Clone>(&self, event: &InspectFsmEvent<S>) {

}
}
4 changes: 3 additions & 1 deletion finny/src/inspect/slog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use slog::{info, o, error};
use crate::{FsmBackend, FsmBackendImpl, FsmEvent, Inspect, InspectEvent, InspectFsmEvent};
use crate::lib::*;
use AsRef;
use core::fmt::Debug;
use core::any::Any;

pub struct InspectSlog {
pub logger: slog::Logger
Expand Down Expand Up @@ -94,7 +96,7 @@ impl Inspect for InspectSlog

impl InspectEvent for InspectSlog
{
fn on_event<F: FsmBackend>(&self, event: InspectFsmEvent<F>) {
fn on_event<S: Any + Debug + Clone>(&self, event: &InspectFsmEvent<S>) {
info!(self.logger, "Inspection event {:?}", event);
}
}

0 comments on commit b32358c

Please sign in to comment.