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 *    You may obtain a copy of the License at
9 *
10 *        http://www.apache.org/licenses/LICENSE-2.0
11 *
12 *    Unless required by applicable law or agreed to in writing, software
13 *    distributed under the License is distributed on an "AS IS" BASIS,
14 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 *    See the License for the specific language governing permissions and
16 *    limitations under the License.
17 *
18 ******************************************************************************/
19//! # Qubit State Machine
20//!
21//! A small, thread-safe finite state machine for Rust.
22//!
23//! This crate provides a generic state machine (`StateMachine`) and a compact
24//! fast state machine (`FastStateMachine`) built on compact `usize` codes and
25//! `FastCas`.
26//!
27//! # Examples
28//!
29//! ```
30//! use qubit_state_machine::{AtomicRef, StateMachine};
31//!
32//! #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
33//! enum State {
34//!     New,
35//!     Running,
36//!     Done,
37//! }
38//!
39//! #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
40//! enum Event {
41//!     Start,
42//!     Finish,
43//! }
44//!
45//! let machine = StateMachine::builder()
46//!     .add_states(&[State::New, State::Running, State::Done])
47//!     .initial_state(State::New)
48//!     .final_state(State::Done)
49//!     .transition(State::New, Event::Start, State::Running)
50//!     .transition(State::Running, Event::Finish, State::Done)
51//!     .build()
52//!     .expect("job state machine should be valid");
53//!
54//! let state = AtomicRef::from_value(State::New);
55//! assert_eq!(machine.trigger(&state, Event::Start).unwrap(), State::Running);
56//! assert_eq!(*state.load(), State::Running);
57//! ```
58
59#![deny(missing_docs)]
60
61mod fast;
62mod standard;
63
64pub use qubit_atomic::AtomicRef;
65pub use qubit_cas::FastCasPolicy;
66
67pub use fast::{
68    FAST_STATE_MACHINE_DEFAULT_CAS_POLICY,
69    FastStateMachine,
70    FastStateMachineBuildError,
71    FastStateMachineBuilder,
72    FastStateMachineError,
73    FastStateMachineResult,
74};
75
76pub use standard::{
77    StateMachine,
78    StateMachineBuildError,
79    StateMachineBuilder,
80    StateMachineError,
81    StateMachineResult,
82    Transition,
83};