Qubit State Machine (rs-state-machine)
Documentation: API Reference
qubit-state-machine is a small Rust finite state machine crate for lifecycle,
workflow, and task-state tracking code.
It provides immutable transition rules, validation at build time, and a
CAS-backed AtomicRef for applying events to shared state.
Why Use It
Use qubit-state-machine when you need:
- explicit finite state machine rules built from enum-like state and event types
- immutable transition tables that can be shared across threads
- build-time validation for unknown states and conflicting transitions
- event-driven state updates through
triggerandtry_trigger - success callbacks that observe the old and new state after an update
- simple state tracking for services, jobs, devices, or UI logic
Installation
[]
= "0.2"
Quick Start: Job Processing
use ;
Build-Time Validation
Invalid rules are rejected before a StateMachine is created.
use ;
let error = builder
.add_state
.add_transition
.build
.expect_err;
assert_eq!;
Applying Events Without Error Handling
Use try_trigger or try_trigger_with when an invalid transition should be a
simple false result.
use ;
let machine = builder
.add_states
.add_transition
.build
.expect;
let state = from_value;
assert!;
assert!;
assert_eq!;
Common Next Steps
| Task | API |
|---|---|
| Define states and transitions | StateMachine::builder, StateMachineBuilder |
| Add one or more states | add_state, add_states |
| Mark initial and final states | set_initial_state, set_initial_states, set_final_state, set_final_states |
| Add transition rules | add_transition, add_transition_value, Transition |
| Query transition targets without changing state | transition_target |
| Apply events and get detailed errors | trigger, trigger_with, StateMachineError |
| Apply events without handling errors | try_trigger, try_trigger_with |
| Store shared mutable state | AtomicRef |
Core API At A Glance
| Type | Purpose |
|---|---|
Transition |
Immutable value describing source --event--> target. |
StateMachineBuilder |
Mutable builder for states, initial states, final states, and transitions. |
StateMachine |
Immutable, validated transition table used to query and trigger events. |
AtomicRef |
Re-exported atomic reference used for CAS-backed current state. |
StateMachineBuildError |
Validation error returned while building invalid rule sets. |
StateMachineError |
Runtime error returned when an event cannot be applied. |
Project Scope
qubit-state-machineis intended for simple finite state machines, not a full workflow engine.- State and event types should be small enum-like values implementing
Copy + Eq + Hash + Debug. - Rule definitions become immutable after
StateMachineBuilder::build. triggeracceptsAtomicRef<S>directly.- Event-driven transitions are installed through
qubit-cas. - Callbacks run after the CAS update has succeeded.
Contributing
Issues and pull requests are welcome.
Please keep contributions focused and easy to review:
- open an issue for bug reports, design questions, or larger feature proposals
- keep pull requests scoped to one behavior change, fix, or documentation update
- run
./ci-check.shbefore submitting changes - include tests when changing runtime behavior
- update the README when public API behavior changes
By contributing to this project, you agree that your contribution will be licensed under the same license as the project.
License
Licensed under the Apache License, Version 2.0.