Module state
Expand description
See state for the system contract and examples.
Deferred lifecycle transitions (loading, running, paused, etc.) with an explicit
allow-list. Hosts handle enter/exit work after commit; callbacks only run when
explicitly supplied to StateMachine::commit_with or
StateMachine::try_commit_with. No state-specific game rules are imposed.
use ferravex_engine::state::{StateMachine, TransitionError};
let mut states = StateMachine::new("loading");
assert!(states.allow("loading", "running"));
states.check_request(&"running")?;
states.request("running")?;
assert_eq!(states.pending_transition().unwrap().from, &"loading");
let mut lifecycle = Vec::new();
states.commit_with(|transition| {
lifecycle.push(("exit", transition.from));
lifecycle.push(("enter", transition.to));
});
assert_eq!(lifecycle, [("exit", "loading"), ("enter", "running")]);
assert_eq!(states.current(), &"running");
assert!(states.commit_with(|_| unreachable!()).is_none());Optional tracing records successful commits, including those made through
guarded/hooked APIs. Enable it with StateMachine::set_trace_capacity; use
StateMachine::request_with_cause to attach a static diagnostic label.
Tracing reads no clocks and does not drive simulation or lifecycle effects.
Structs§
- State
Machine - A bounded directed transition graph with one deferred request at a time.
- Transition
- A transition’s endpoints, owned after commit or borrowed during inspection.
- Transition
Record - An owned observation of a committed transition, retained in commit order.
Enums§
- Transition
Error - Why a request was rejected. Rejection changes neither current nor pending state.