Skip to content

Commit

Permalink
feat: expose references to Inner
Browse files Browse the repository at this point in the history
Problem: Mutable access to `Inner` is required for ergonomic unit
testing.

Solution: Add `inner` and `inner_mut` to:
- `awaitable::state_machine{UninitializedStateMachine,
  InitializedStateMachine}`
- `blocking::state_machine{UninitializedStateMachine,
  InitializedStateMachine}`
Mark the methods as `unsafe` for the case of an
`InitializedStateMachine` to force users to think critically about
mutating in thise case.

Testing: `cargo test`

Issue: mdeloof#34
  • Loading branch information
Christian Heussy committed Jan 11, 2025
1 parent bf6457a commit 9a59d54
Show file tree
Hide file tree
Showing 5 changed files with 360 additions and 5 deletions.
177 changes: 177 additions & 0 deletions statig/src/awaitable/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,67 @@ where
pub fn state(&self) -> &M::State {
&self.inner.state
}

/// Get a reference to the [StateMachine]'s [Inner] storage.
///
/// ```
/// # use statig::prelude::*;
/// # #[derive(Default)]
/// # pub struct Blinky {
/// # led: bool,
/// # }
/// #
/// # pub struct Event;
/// #
/// # #[state_machine(
/// # initial = "State::on()",
/// # state(derive(Debug, PartialEq, Eq))
/// # )]
/// # impl Blinky {
/// # #[state]
/// # fn on(event: &Event) -> Response<State> { Handled }
/// # }
/// #
/// let state_machine = Blinky::default().state_machine();
/// assert_eq!(state_machine.inner().shared_storage.led, false);
/// assert_eq!(state_machine.inner().state, State::on());
/// ```
pub fn inner(&self) -> &Inner<M> {
&self.inner
}

/// Get a mutable reference to the [StateMachine]'s [Inner] storage.
///
/// # Safety
///
/// - The user is responsible for validating that mutating a
/// [StateMachine] does not break any invariants.
///
/// ```
/// # use statig::prelude::*;
/// # #[derive(Default)]
/// # pub struct Blinky {
/// # led: bool,
/// # }
/// #
/// # pub struct Event;
/// #
/// # #[state_machine(initial = "State::on()")]
/// # impl Blinky {
/// # #[state]
/// # fn on(event: &Event) -> Response<State> { Handled }
/// # }
/// #
/// let mut state_machine = Blinky::default().state_machine();
///
/// unsafe {
/// state_machine.inner_mut().shared_storage.led = true;
/// state_machine.inner_mut().state = State::on();
/// }
/// ```
pub unsafe fn inner_mut(&mut self) -> &mut Inner<M> {
&mut self.inner
}
}

impl<M> Clone for StateMachine<M>
Expand Down Expand Up @@ -286,6 +347,67 @@ where
pub fn state(&self) -> &M::State {
&self.inner.state
}

/// Get a reference to the [InitializedStateMachine]'s [Inner] storage.
///
/// ```
/// # use statig::prelude::*;
/// # #[derive(Default)]
/// # pub struct Blinky {
/// # led: bool,
/// # }
/// #
/// # pub struct Event;
/// #
/// # #[state_machine(
/// # initial = "State::on()",
/// # state(derive(Debug, PartialEq, Eq))
/// # )]
/// # impl Blinky {
/// # #[state]
/// # fn on(event: &Event) -> Response<State> { Handled }
/// # }
/// #
/// # let uninitialized_state_machine = Blinky::default().uninitialized_state_machine();
/// let initialized_state_machine = uninitialized_state_machine.init();
/// assert_eq!(initialized_state_machine.inner().shared_storage.led, false);
/// assert_eq!(initialized_state_machine.inner().state, State::on());
/// ```
pub fn inner(&self) -> &Inner<M> {
&self.inner
}

/// Get a mutable reference to the [InitializedStateMachine]'s [Inner] storage.
///
/// # Safety
///
/// - The user is responsible for validating that mutating a
/// [InitializedStateMachine] does not break any invariants.
///
/// ```
/// # use statig::prelude::*;
/// # #[derive(Default)]
/// # pub struct Blinky {
/// # led: bool,
/// # }
/// #
/// # pub struct Event;
/// #
/// # #[state_machine(initial = "State::on()")]
/// # impl Blinky {
/// # #[state]
/// # fn on(event: &Event) -> Response<State> { Handled }
/// # }
/// #
/// # let uninitialized_state_machine = Blinky::default().uninitialized_state_machine();
/// let mut initialized_state_machine = uninitialized_state_machine.init();
/// unsafe {
/// initialized_state_machine.inner_mut().shared_storage.led = true;
/// }
/// ```
pub unsafe fn inner_mut(&mut self) -> &mut Inner<M> {
&mut self.inner
}
}

impl<M> Clone for InitializedStateMachine<M>
Expand Down Expand Up @@ -458,6 +580,61 @@ where
state_machine.inner.async_init_with_context(context).await;
state_machine
}

/// Get a reference to the [UninitializedStateMachine]'s [Inner] storage.
///
/// ```
/// # use statig::prelude::*;
/// # #[derive(Default)]
/// # pub struct Blinky {
/// # led: bool,
/// # }
/// #
/// # pub struct Event;
/// #
/// # #[state_machine(
/// # initial = "State::on()",
/// # state(derive(Debug, PartialEq, Eq))
/// # )]
/// # impl Blinky {
/// # #[state]
/// # fn on(event: &Event) -> Response<State> { Handled }
/// # }
/// #
/// let uninitialized_state_machine = Blinky::default().uninitialized_state_machine();
///
/// assert_eq!(uninitialized_state_machine.inner().shared_storage.led, false);
/// assert_eq!(uninitialized_state_machine.inner().state, State::on());
/// ```
pub fn inner(&self) -> &Inner<M> {
&self.inner
}

/// Get a mutable reference to the [UninitializedStateMachine]'s [Inner] storage.
///
/// ```
/// # use statig::prelude::*;
/// # #[derive(Default)]
/// # pub struct Blinky {
/// # led: bool,
/// # }
/// #
/// # pub struct Event;
/// #
/// # #[state_machine(initial = "State::on()")]
/// # impl Blinky {
/// # #[state]
/// # fn on(event: &Event) -> Response<State> { Handled }
/// # }
/// #
/// let mut uninitialized_state_machine = Blinky::default().uninitialized_state_machine();
///
/// uninitialized_state_machine.inner_mut().shared_storage.led = true;
/// uninitialized_state_machine.inner_mut().state = State::on();
/// ```
pub fn inner_mut(&mut self) -> &mut Inner<M> {
&mut self.inner
}
}

impl<M> Clone for UninitializedStateMachine<M>
Expand Down
1 change: 1 addition & 0 deletions statig/src/blocking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod superstate;
pub use crate::Response::{self, *};
pub use crate::*;

pub use inner::*;
pub use state::*;
pub use state_machine::*;
pub use superstate::*;
Loading

0 comments on commit 9a59d54

Please sign in to comment.