Expand description
§ready-active-safe
A small lifecycle engine for externally driven systems.
Most real systems do not have an “anything can go anywhere” state graph. They have a lifecycle. They start up, run, and shut down. When things go wrong, they go safe and recover.
This crate gives you a tiny kernel for that shape of problem.
You write one pure function, Machine::on_event, and you get back a Decision.
The decision is plain data, not effects. Your runtime stays yours.
This crate is not a general purpose state machine framework. It is a lifecycle engine.
§What You Write
Mode: lifecycle phases like Ready, Active, SafeEvent: input from the outside worldCommand: work for your runtime to execute
§Feature Flags
| Feature | Default | Requires | Description |
|---|---|---|---|
full | Yes | none | Enables all features below |
std | No* | none | Standard library support |
runtime | No* | std | Event loop and command dispatch |
time | No* | none | Clock, instant, and deadline types |
journal | No* | std | Transition recording and replay |
*Enabled by default through the full feature.
§Module Overview
- Root types (
Machine,Decision,ModeChange,Policy): Always available,no_std-compatible. These are the core contracts. LifecycleError: transition failure errors.time(featuretime): Clock and deadline abstractions.runtime(featureruntime): Event acceptance and command dispatch.journal(featurejournal): Transition recording and replay.
§Examples
Run any example with cargo run --example <name>. Suggested reading order:
basic— core API:Machine,Decision,stay(),transition(),emit()openxr_session— real-world domain mapping (XR session lifecycle)recovery—Policyenforcement andapply_checkedrunner—Runnerevent loop with policy denial handlingrecovery_cycle— repeated fault/recovery with external retry logicchannel_runtime— multi-threaded event feeding over channelsmetrics— observability wrapper aroundRunnerreplay— journal recording and deterministic replay
§Quick Start
use ready_active_safe::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq)]
enum Mode {
Ready,
Active,
Safe,
}
#[derive(Debug)]
enum Event {
Start,
Stop,
Fault,
}
#[derive(Debug)]
enum Command {
Initialize,
Shutdown,
}
struct System;
impl Machine for System {
type Mode = Mode;
type Event = Event;
type Command = Command;
fn initial_mode(&self) -> Mode { Mode::Ready }
fn on_event(&self, mode: &Mode, event: &Event) -> Decision<Mode, Command> {
use Command::*;
use Event::*;
use Mode::*;
match (mode, event) {
(Ready, Start) => transition(Active).emit(Initialize),
(Active, Stop | Fault) => transition(Safe).emit(Shutdown),
_ => ignore(),
}
}
}
let system = System;
let mut mode = system.initial_mode();
let decision = system.decide(&mode, &Event::Start);
assert!(decision.is_transition());
assert_eq!(decision.target_mode(), Some(&Mode::Active));Modules§
- journal
journal - Transition recording and deterministic replay.
- prelude
- Convenience re-exports for common imports.
- runtime
runtime - A small reference runtime.
- time
time - Clock, instant, and deadline abstractions for lifecycle timing.
Macros§
- assert_
emits - Asserts that a decision emits the expected commands.
- assert_
stays - Asserts that a decision stays in the current mode.
- assert_
transitions_ to - Asserts that a decision transitions to the expected mode.
Structs§
- Allow
All - A policy that permits every transition.
- Decision
- The outcome of processing an event through
Machine::on_event. - DenyAll
- A policy that denies every transition.
- Mode
Change - A requested change from one mode to another.
Enums§
- Lifecycle
Error - Errors that can occur during lifecycle operations.
Traits§
- Machine
- A pure function from (mode, event) to
Decision. - Policy
- Determines whether a mode transition is allowed.
Functions§
- apply_
decision - Applies a decision to an owned current mode.
- apply_
decision_ checked - Applies a decision to an owned current mode after checking a policy.
- ignore
- Creates a decision that deliberately ignores an unmatched event.
- stay
- Creates a decision that stays in the current mode.
- transition
- Creates a decision that transitions to the given mode.