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 provides a generic state machine (StateMachine) and a compact fast state machine (FastStateMachine) built on compact usize codes and FastCas.

§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])
    .initial_state(State::New)
    .final_state(State::Done)
    .transition(State::New, Event::Start, State::Running)
    .transition(State::Running, Event::Finish, State::Done)
    .build()
    .expect("job 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);

Structs§

AtomicRef
Atomic reference type.
FastStateMachine
A compact, high-performance state machine backed by FastCas.
FastStateMachineBuilder
Builder for dense, integer-coded state machine rules.
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§

FastCasPolicy
Retry policy used by crate::FastCas when compare-and-swap loses a race.
FastStateMachineBuildError
Error returned when fast state machine configuration is invalid.
FastStateMachineError
Error returned when applying an event through a crate::FastStateMachine.
StateMachineBuildError
Error returned when state machine rules are internally inconsistent.
StateMachineError
Error returned when an event cannot be applied to the current state.

Constants§

FAST_STATE_MACHINE_DEFAULT_CAS_POLICY
Default retry policy used by FastStateMachineBuilder.

Type Aliases§

FastStateMachineResult
Result returned by FastStateMachine runtime transition APIs.
StateMachineResult
Result returned by event-triggering state machine operations.