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