roopes_core/patterns/state/
mod.rs

1//!
2#![cfg_attr(feature = "doc-images",
3  cfg_attr(
4    all(),
5    doc = ::embed_doc_image::embed_image!(
6        "state-diagram",
7        "src/patterns/state/state.svg"
8)))]
9//! This module implements the State pattern.
10//!
11//! ![state diagram][state-diagram]
12
13pub mod simple;
14
15/// This trait holds the active state, and acts as
16/// a persistent handle for the state machine,
17/// which could otherwise be the states
18/// themselves.
19pub trait Context<S>
20where
21    S: State,
22{
23    /// Delegates the state transition to the
24    /// currently active state held by the
25    /// context, probably triggering a state
26    /// change by the context.
27    fn handle(&mut self);
28}
29
30/// Provides transitions between states.
31pub trait State
32{
33    /// Allow the state to engage in whatever
34    /// processing it would normally do.
35    /// The returned [`State`] is used to
36    /// determine the new state the
37    /// [`Context`] should assume.
38    #[must_use]
39    fn execute(&self) -> Self;
40}
41
42/// Exposes the [`Context`] and [`State`] types at
43/// the library level.
44pub mod prelude
45{
46    pub use super::{
47        Context,
48        State,
49    };
50}
51
52#[cfg(test)]
53mod tests;