From e94e1831f965e4ed7cafb03b95ae59550e412499 Mon Sep 17 00:00:00 2001 From: Maxim Deloof <20443795+mdeloof@users.noreply.github.com> Date: Wed, 8 Jan 2025 19:34:57 +0100 Subject: [PATCH] Make initial constructor a function instead of a function pointer --- examples/macro/history/src/main.rs | 2 +- examples/no_macro/basic/src/main.rs | 4 +++- examples/no_macro/bench/src/main.rs | 4 +++- examples/no_macro/blinky/src/main.rs | 4 +++- examples/no_macro/calculator/src/state.rs | 4 +++- examples/no_macro/history/src/main.rs | 6 ++++-- macro/src/codegen.rs | 5 ++++- statig/src/awaitable/into_state_machine.rs | 2 +- statig/src/awaitable/state_machine.rs | 6 +++--- statig/src/blocking/into_state_machine.rs | 2 +- statig/src/blocking/state_machine.rs | 6 +++--- statig/src/state_or_superstate.rs | 7 ++++--- statig/tests/async_transition.rs | 4 +++- statig/tests/state_local_storage.rs | 12 +++++++----- statig/tests/transition.rs | 4 +++- 15 files changed, 46 insertions(+), 26 deletions(-) diff --git a/examples/macro/history/src/main.rs b/examples/macro/history/src/main.rs index 4b9f863..7849016 100644 --- a/examples/macro/history/src/main.rs +++ b/examples/macro/history/src/main.rs @@ -79,7 +79,7 @@ impl Dishwasher { fn main() { let mut state_machine = Dishwasher { - previous_state: Dishwasher::INITIAL, + previous_state: Dishwasher::initial(), } .uninitialized_state_machine() .init(); diff --git a/examples/no_macro/basic/src/main.rs b/examples/no_macro/basic/src/main.rs index 6e4981c..04f28a5 100644 --- a/examples/no_macro/basic/src/main.rs +++ b/examples/no_macro/basic/src/main.rs @@ -30,7 +30,9 @@ impl IntoStateMachine for Blinky { type Context<'ctx> = (); /// The initial state of the state machine. - const INITIAL: State = State::Off; + fn initial() -> Self::State { + State::Off + } /// This method is called on every transition of the state machine. const AFTER_TRANSITION: fn(&mut Self, &Self::State, &Self::State) = |_, source, target| { diff --git a/examples/no_macro/bench/src/main.rs b/examples/no_macro/bench/src/main.rs index d2f26fd..0bd5034 100644 --- a/examples/no_macro/bench/src/main.rs +++ b/examples/no_macro/bench/src/main.rs @@ -49,7 +49,9 @@ impl IntoStateMachine for CdPlayer { type Context<'ctx> = (); /// The initial state of the state machine. - const INITIAL: State = State::Empty; + fn initial() -> Self::State { + State::Empty + } } impl blocking::State for State { diff --git a/examples/no_macro/blinky/src/main.rs b/examples/no_macro/blinky/src/main.rs index c416201..6b0b690 100644 --- a/examples/no_macro/blinky/src/main.rs +++ b/examples/no_macro/blinky/src/main.rs @@ -41,7 +41,9 @@ impl IntoStateMachine for Blinky { type Context<'ctx> = (); /// The initial state of the state machine. - const INITIAL: State = State::LedOn; + fn initial() -> State { + State::LedOn + } } // Implement the `statig::State` trait for the state enum. diff --git a/examples/no_macro/calculator/src/state.rs b/examples/no_macro/calculator/src/state.rs index 69210ea..6a8e177 100644 --- a/examples/no_macro/calculator/src/state.rs +++ b/examples/no_macro/calculator/src/state.rs @@ -66,7 +66,9 @@ impl blocking::IntoStateMachine for Calculator { type Context<'ctx> = (); - const INITIAL: State = State::Begin; + fn initial() -> Self::State { + State::Begin + } } impl blocking::State for State { diff --git a/examples/no_macro/history/src/main.rs b/examples/no_macro/history/src/main.rs index acb7701..78ce2f7 100644 --- a/examples/no_macro/history/src/main.rs +++ b/examples/no_macro/history/src/main.rs @@ -36,7 +36,9 @@ impl IntoStateMachine for Dishwasher { type Context<'ctx> = (); - const INITIAL: State = State::Idle; + fn initial() -> Self::State { + State::Idle + } // On every transition we update the previous state, so we can // transition back to it. @@ -135,7 +137,7 @@ impl Dishwasher { fn main() { let mut state_machine = Dishwasher { - previous_state: Dishwasher::INITIAL, + previous_state: Dishwasher::initial(), } .uninitialized_state_machine() .init(); diff --git a/macro/src/codegen.rs b/macro/src/codegen.rs index f4bdb6b..b9e4df1 100644 --- a/macro/src/codegen.rs +++ b/macro/src/codegen.rs @@ -144,7 +144,10 @@ fn codegen_state_machine_impl(ir: &Ir) -> ItemImpl { type Context<#context_lifetime> = #context_type; type State = #state_ident #state_generics; type Superstate<#superstate_lifetime> = #superstate_ident #superstate_generics ; - const INITIAL: #state_ident #state_generics = #initial_state; + + fn initial() -> #state_ident #state_generics { + #initial_state + } #before_transition #after_transition diff --git a/statig/src/awaitable/into_state_machine.rs b/statig/src/awaitable/into_state_machine.rs index 4fb0757..4482e87 100644 --- a/statig/src/awaitable/into_state_machine.rs +++ b/statig/src/awaitable/into_state_machine.rs @@ -22,7 +22,7 @@ where Self::State: 'sub; /// Initial state of the state machine. - const INITIAL: Self::State; + fn initial() -> Self::State; /// Method that is called *before* an event is dispatched to a state or /// superstate handler. diff --git a/statig/src/awaitable/state_machine.rs b/statig/src/awaitable/state_machine.rs index 2c54e4e..ae75e48 100644 --- a/statig/src/awaitable/state_machine.rs +++ b/statig/src/awaitable/state_machine.rs @@ -15,7 +15,7 @@ where { let inner = Inner { shared_storage: self, - state: Self::INITIAL, + state: Self::initial(), }; StateMachine { inner, @@ -28,7 +28,7 @@ where fn uninitialized_state_machine(self) -> UninitializedStateMachine { let inner = Inner { shared_storage: self, - state: Self::INITIAL, + state: Self::initial(), }; UninitializedStateMachine { inner } } @@ -154,7 +154,7 @@ where fn default() -> Self { let inner = Inner { shared_storage: M::default(), - state: M::INITIAL, + state: M::initial(), }; Self { inner, diff --git a/statig/src/blocking/into_state_machine.rs b/statig/src/blocking/into_state_machine.rs index 0fdb92b..7a4c1ec 100644 --- a/statig/src/blocking/into_state_machine.rs +++ b/statig/src/blocking/into_state_machine.rs @@ -20,7 +20,7 @@ where Self::State: 'sub; /// Initial state of the state machine. - const INITIAL: Self::State; + fn initial() -> Self::State; /// Method that is called *before* an event is dispatched to a state or /// superstate handler. diff --git a/statig/src/blocking/state_machine.rs b/statig/src/blocking/state_machine.rs index 86afb60..8cbd004 100644 --- a/statig/src/blocking/state_machine.rs +++ b/statig/src/blocking/state_machine.rs @@ -14,7 +14,7 @@ where { let inner = Inner { shared_storage: self, - state: Self::INITIAL, + state: Self::initial(), }; StateMachine { inner, @@ -27,7 +27,7 @@ where fn uninitialized_state_machine(self) -> UninitializedStateMachine { let inner = Inner { shared_storage: self, - state: Self::INITIAL, + state: Self::initial(), }; UninitializedStateMachine { inner } } @@ -148,7 +148,7 @@ where fn default() -> Self { let inner = Inner { shared_storage: M::default(), - state: M::INITIAL, + state: M::initial(), }; Self { inner, diff --git a/statig/src/state_or_superstate.rs b/statig/src/state_or_superstate.rs index 5ecd2ff..9baf172 100644 --- a/statig/src/state_or_superstate.rs +++ b/statig/src/state_or_superstate.rs @@ -1,4 +1,5 @@ -use core::fmt::Debug; +use core::fmt; +use core::fmt::{Debug, Formatter}; /// Holds a reference to either a state or superstate. pub enum StateOrSuperstate<'a, State, Superstate> { @@ -8,12 +9,12 @@ pub enum StateOrSuperstate<'a, State, Superstate> { Superstate(&'a Superstate), } -impl<'a, State, Superstate> core::fmt::Debug for StateOrSuperstate<'a, State, Superstate> +impl<'a, State, Superstate> Debug for StateOrSuperstate<'a, State, Superstate> where State: Debug, Superstate: Debug, { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { Self::State(state) => f.debug_tuple("State").field(state as &dyn Debug).finish(), Self::Superstate(superstate) => f diff --git a/statig/tests/async_transition.rs b/statig/tests/async_transition.rs index 4960161..eb7ed1f 100644 --- a/statig/tests/async_transition.rs +++ b/statig/tests/async_transition.rs @@ -58,7 +58,9 @@ mod tests { type Context<'ctx> = (); - const INITIAL: State = State::S11; + fn initial() -> Self::State { + State::S11 + } } impl awaitable::State for State { diff --git a/statig/tests/state_local_storage.rs b/statig/tests/state_local_storage.rs index 40b0265..375e579 100644 --- a/statig/tests/state_local_storage.rs +++ b/statig/tests/state_local_storage.rs @@ -24,15 +24,17 @@ mod tests { type Context<'ctx> = (); /// The initial state of the state machine. - const INITIAL: StateEnum = StateEnum::On { - led: false, - counter: 23, - }; + fn initial() -> Self::State { + StateEnum::On { + led: false, + counter: 23, + } + } } impl Default for StateEnum { fn default() -> Self { - Blinky::INITIAL + Blinky::initial() } } diff --git a/statig/tests/transition.rs b/statig/tests/transition.rs index 493f13b..1e4c455 100644 --- a/statig/tests/transition.rs +++ b/statig/tests/transition.rs @@ -55,7 +55,9 @@ mod tests { type Context<'ctx> = (); - const INITIAL: State = State::S11; + fn initial() -> Self::State { + State::S11 + } } impl blocking::State for State {