Skip to main content

Crate qubit_state_machine

Crate qubit_state_machine 

Source
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§

AtomicRef
Atomic reference type.
StateMachine
Immutable finite state machine rules.
StateMachineBuilder
Builder used to define and validate finite state machine rules.
Transition
A directed transition in a finite state machine.

Enums§

StateMachineBuildError
Error returned when state machine rules are internally inconsistent.
StateMachineError
Error returned when an event cannot be applied to the current state.

Type Aliases§

StateMachineResult
Result returned by event-triggering state machine operations.