Expand description
§Qubit State Machine
A small, thread-safe finite state machine for Rust.
This crate is intentionally compact. It stores immutable transition rules
and applies events to an AtomicRef through a compare-and-swap executor.
§Examples
use qubit_state_machine::{AtomicRef, StateMachine};
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
enum State {
New,
Running,
Done,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
enum Event {
Start,
Finish,
}
let machine = StateMachine::builder()
.add_states(&[State::New, State::Running, State::Done])
.set_initial_state(State::New)
.set_final_state(State::Done)
.add_transition(State::New, Event::Start, State::Running)
.add_transition(State::Running, Event::Finish, State::Done)
.build()
.expect("state machine should be valid");
let state = AtomicRef::from_value(State::New);
assert_eq!(machine.trigger(&state, Event::Start).unwrap(), State::Running);
assert_eq!(*state.load(), State::Running);§Author
Haixing Hu
Structs§
- Atomic
Ref - Atomic reference type.
- State
Machine - Immutable finite state machine rules.
- State
Machine Builder - Builder used to define and validate finite state machine rules.
- Transition
- A directed transition in a finite state machine.
Enums§
- State
Machine Build Error - Error returned when state machine rules are internally inconsistent.
- State
Machine Error - Error returned when an event cannot be applied to the current state.
Type Aliases§
- State
Machine Result - Result returned by event-triggering state machine operations.