Skip to content

Commit

Permalink
Make initial constructor a function instead of a function pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeloof committed Jan 8, 2025
1 parent 6adedf4 commit 8fa359c
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/macro/history/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion examples/no_macro/basic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ON_TRANSITION: fn(&mut Self, &Self::State, &Self::State) = |_, source, target| {
Expand Down
4 changes: 3 additions & 1 deletion examples/no_macro/bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CdPlayer> for State {
Expand Down
4 changes: 3 additions & 1 deletion examples/no_macro/blinky/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion examples/no_macro/calculator/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Calculator> for State {
Expand Down
6 changes: 4 additions & 2 deletions examples/no_macro/history/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion macro/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,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
}

#on_transition

Expand Down
2 changes: 1 addition & 1 deletion statig/src/awaitable/into_state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions statig/src/awaitable/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ where
{
let inner = Inner {
shared_storage: self,
state: Self::INITIAL,
state: Self::initial(),
};
StateMachine {
inner,
Expand All @@ -28,7 +28,7 @@ where
fn uninitialized_state_machine(self) -> UninitializedStateMachine<Self> {
let inner = Inner {
shared_storage: self,
state: Self::INITIAL,
state: Self::initial(),
};
UninitializedStateMachine { inner }
}
Expand Down Expand Up @@ -154,7 +154,7 @@ where
fn default() -> Self {
let inner = Inner {
shared_storage: M::default(),
state: M::INITIAL,
state: M::initial(),
};
Self {
inner,
Expand Down
2 changes: 1 addition & 1 deletion statig/src/blocking/into_state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions statig/src/blocking/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ where
{
let inner = Inner {
shared_storage: self,
state: Self::INITIAL,
state: Self::initial(),
};
StateMachine {
inner,
Expand All @@ -27,7 +27,7 @@ where
fn uninitialized_state_machine(self) -> UninitializedStateMachine<Self> {
let inner = Inner {
shared_storage: self,
state: Self::INITIAL,
state: Self::initial(),
};
UninitializedStateMachine { inner }
}
Expand Down Expand Up @@ -148,7 +148,7 @@ where
fn default() -> Self {
let inner = Inner {
shared_storage: M::default(),
state: M::INITIAL,
state: M::initial(),
};
Self {
inner,
Expand Down
7 changes: 4 additions & 3 deletions statig/src/state_or_superstate.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion statig/tests/async_transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ mod tests {

type Context<'ctx> = ();

const INITIAL: State = State::S11;
fn initial() -> Self::State {
State::S11
}
}

impl awaitable::State<Foo> for State {
Expand Down
12 changes: 7 additions & 5 deletions statig/tests/state_local_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down
4 changes: 3 additions & 1 deletion statig/tests/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ mod tests {

type Context<'ctx> = ();

const INITIAL: State = State::S11;
fn initial() -> Self::State {
State::S11
}
}

impl blocking::State<Foo> for State {
Expand Down

0 comments on commit 8fa359c

Please sign in to comment.