Skip to main content

qubit_state_machine/
lib.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! # Qubit State Machine
11//!
12//! A small, thread-safe finite state machine for Rust.
13//!
14//! This crate is intentionally compact. It stores immutable transition rules
15//! and applies events to an [`AtomicRef`] through a compare-and-swap executor.
16//!
17//! # Examples
18//!
19//! ```
20//! use qubit_state_machine::{AtomicRef, StateMachine};
21//!
22//! #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
23//! enum State {
24//!     New,
25//!     Running,
26//!     Done,
27//! }
28//!
29//! #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
30//! enum Event {
31//!     Start,
32//!     Finish,
33//! }
34//!
35//! let machine = StateMachine::builder()
36//!     .add_states(&[State::New, State::Running, State::Done])
37//!     .set_initial_state(State::New)
38//!     .set_final_state(State::Done)
39//!     .add_transition(State::New, Event::Start, State::Running)
40//!     .add_transition(State::Running, Event::Finish, State::Done)
41//!     .build()
42//!     .expect("state machine should be valid");
43//!
44//! let state = AtomicRef::from_value(State::New);
45//! assert_eq!(machine.trigger(&state, Event::Start).unwrap(), State::Running);
46//! assert_eq!(*state.load(), State::Running);
47//! ```
48//!
49
50#![deny(missing_docs)]
51
52mod state_machine;
53mod state_machine_build_error;
54mod state_machine_builder;
55mod state_machine_error;
56mod transition;
57
58pub use qubit_atomic::AtomicRef;
59pub use state_machine::StateMachine;
60pub use state_machine_build_error::StateMachineBuildError;
61pub use state_machine_builder::StateMachineBuilder;
62pub use state_machine_error::StateMachineError;
63pub use transition::Transition;
64
65/// Result returned by event-triggering state machine operations.
66///
67/// `S` is the state type and `E` is the event type.
68pub type StateMachineResult<S, E> = Result<S, StateMachineError<S, E>>;