From b32358cf8fdd5bdf661969db560cc48bcd57602c Mon Sep 17 00:00:00 2001 From: Rudi Benkovic Date: Sat, 9 Oct 2021 14:35:06 +0200 Subject: [PATCH] use the specific state type instead of the entire FSM --- finny/src/fsm/inspect/mod.rs | 20 ++++++-------------- finny/src/fsm/mod.rs | 2 +- finny/src/fsm/states.rs | 4 ++-- finny/src/fsm/transitions.rs | 8 ++++---- finny/src/inspect/chain.rs | 5 +++-- finny/src/inspect/events.rs | 5 +++-- finny/src/inspect/null.rs | 5 +++-- finny/src/inspect/slog.rs | 4 +++- 8 files changed, 25 insertions(+), 28 deletions(-) diff --git a/finny/src/fsm/inspect/mod.rs b/finny/src/fsm/inspect/mod.rs index 4fa65a3..308b2b7 100644 --- a/finny/src/fsm/inspect/mod.rs +++ b/finny/src/fsm/inspect/mod.rs @@ -1,20 +1,12 @@ +use core::fmt::Debug; use core::any::Any; use crate::{FsmBackend, FsmBackendImpl, FsmEvent, FsmStates}; -#[derive(Debug)] -pub enum InspectFsmEvent where F: FsmBackend { - StateEnter(<::States as FsmStates>::StateKind), - StateExit(<::States as FsmStates>::StateKind) -} - -impl Clone for InspectFsmEvent 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 where S: Debug + Clone { + StateEnter(S), + StateExit(S) } pub trait Inspect: InspectEvent { @@ -36,5 +28,5 @@ pub trait Inspect: InspectEvent { } pub trait InspectEvent { - fn on_event(&self, event: InspectFsmEvent); + fn on_event(&self, event: &InspectFsmEvent); } \ No newline at end of file diff --git a/finny/src/fsm/mod.rs b/finny/src/fsm/mod.rs index 6878b64..25468df 100644 --- a/finny/src/fsm/mod.rs +++ b/finny/src/fsm/mod.rs @@ -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. diff --git a/finny/src/fsm/states.rs b/finny/src/fsm/states.rs index e39ea4e..79121cf 100644 --- a/finny/src/fsm/states.rs +++ b/finny/src/fsm/states.rs @@ -5,9 +5,9 @@ use crate::FsmResult; /// The implementation should hold all of the FSM's states as fields. pub trait FsmStates: FsmStateFactory 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]> + AsMut<[FsmCurrentState]>; + type CurrentState: Clone + Copy + Debug + Default + AsRef<[FsmCurrentState]> + AsMut<[FsmCurrentState]> + 'static; } /// The current state of the FSM. diff --git a/finny/src/fsm/transitions.rs b/finny/src/fsm/transitions.rs index 309152f..4ab6a43 100644 --- a/finny/src/fsm/transitions.rs +++ b/finny/src/fsm/transitions.rs @@ -26,8 +26,8 @@ pub trait FsmState where Self: Sized { context.inspect.on_state_enter::(); let kind = ::fsm_state(); - let ev = InspectFsmEvent::::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(); @@ -51,8 +51,8 @@ pub trait FsmState where Self: Sized { context.inspect.on_state_exit::(); let kind = ::fsm_state(); - let ev = InspectFsmEvent::::StateExit(kind); - context.inspect.on_event(ev); + let ev = InspectFsmEvent::StateExit(kind); + context.inspect.on_event(&ev); } } diff --git a/finny/src/inspect/chain.rs b/finny/src/inspect/chain.rs index c868e63..42bb446 100644 --- a/finny/src/inspect/chain.rs +++ b/finny/src/inspect/chain.rs @@ -1,5 +1,6 @@ use crate::{FsmBackend, FsmBackendImpl, FsmEvent, Inspect, InspectEvent, InspectFsmEvent}; use core::any::Any; +use core::fmt::Debug; use super::{null::InspectNull}; @@ -110,8 +111,8 @@ impl Inspect for InspectChain impl InspectEvent for InspectChain where A: Inspect, B: Inspect { - fn on_event(&self, event: InspectFsmEvent) { - self.a.on_event(event.clone()); + fn on_event(&self, event: &InspectFsmEvent) { + self.a.on_event(event); self.b.on_event(event); } } \ No newline at end of file diff --git a/finny/src/inspect/events.rs b/finny/src/inspect/events.rs index 2573ae3..0b8c783 100644 --- a/finny/src/inspect/events.rs +++ b/finny/src/inspect/events.rs @@ -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 @@ -69,7 +70,7 @@ impl Inspect for EventInspector impl InspectEvent for EventInspector where TI: InspectEvent + Clone { - fn on_event(&self, event: crate::InspectFsmEvent) { + fn on_event(&self, event: &InspectFsmEvent) { self.event_handler.on_event(event) } } \ No newline at end of file diff --git a/finny/src/inspect/null.rs b/finny/src/inspect/null.rs index f0a9af4..04c3b3b 100644 --- a/finny/src/inspect/null.rs +++ b/finny/src/inspect/null.rs @@ -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; @@ -57,7 +58,7 @@ impl Inspect for InspectNull { } impl InspectEvent for InspectNull { - fn on_event(&self, event: InspectFsmEvent) { + fn on_event(&self, event: &InspectFsmEvent) { } } \ No newline at end of file diff --git a/finny/src/inspect/slog.rs b/finny/src/inspect/slog.rs index a6f3ed8..cc04cba 100644 --- a/finny/src/inspect/slog.rs +++ b/finny/src/inspect/slog.rs @@ -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 @@ -94,7 +96,7 @@ impl Inspect for InspectSlog impl InspectEvent for InspectSlog { - fn on_event(&self, event: InspectFsmEvent) { + fn on_event(&self, event: &InspectFsmEvent) { info!(self.logger, "Inspection event {:?}", event); } } \ No newline at end of file