pub struct FastStateMachine { /* private fields */ }Expand description
A compact, high-performance state machine backed by FastCas.
States and events are represented as contiguous integer code spaces:
- states are in
[0, state_count) - events are in
[0, event_count)
Transition resolution is a single table index lookup:
index = source * event_count + event.
Implementations§
Source§impl FastStateMachine
impl FastStateMachine
Sourcepub fn builder() -> FastStateMachineBuilder
pub fn builder() -> FastStateMachineBuilder
Creates a builder used to configure counts, initial/final flags, transitions,
and CAS policy before calling super::FastStateMachineBuilder::build.
§Returns
A new, empty super::FastStateMachineBuilder.
Sourcepub const fn state_count(&self) -> usize
pub const fn state_count(&self) -> usize
Returns the number of distinct state codes configured for this machine.
Valid state codes are integers in 0..state_count().
§Returns
The configured state-space size (length of the transition table rows).
Sourcepub const fn event_count(&self) -> usize
pub const fn event_count(&self) -> usize
Returns the number of distinct event codes accepted by this machine.
Valid event codes are integers in 0..event_count().
§Returns
The configured event-space size (length of each row in the transition table).
Sourcepub fn transitions(&self) -> &[usize]
pub fn transitions(&self) -> &[usize]
Returns the dense transition table.
The table is laid out row-major: source index first, then event index.
Sourcepub fn cas_policy(&self) -> FastCasPolicy
pub fn cas_policy(&self) -> FastCasPolicy
Returns the CAS retry policy used for all transitions.
This is the policy configured in the builder via
crate::FastStateMachineBuilder::cas_policy, or
crate::FAST_STATE_MACHINE_DEFAULT_CAS_POLICY when no override is
supplied.
Sourcepub fn initial_states(&self) -> &[bool]
pub fn initial_states(&self) -> &[bool]
Returns a read-only slice marking which state codes are initial.
The slice has length Self::state_count; index s corresponds to state code s,
and is true if that state was registered as initial in the builder.
Sourcepub fn final_states(&self) -> &[bool]
pub fn final_states(&self) -> &[bool]
Returns a read-only slice marking which state codes are final (accepting).
The slice has length Self::state_count; index s corresponds to state code s,
and is true if that state was registered as final in the builder.
Sourcepub const fn contains_state(&self, state: usize) -> bool
pub const fn contains_state(&self, state: usize) -> bool
Sourcepub fn is_initial_state(&self, state: usize) -> bool
pub fn is_initial_state(&self, state: usize) -> bool
Sourcepub fn is_final_state(&self, state: usize) -> bool
pub fn is_final_state(&self, state: usize) -> bool
Sourcepub fn trigger(
&self,
state: &FastCasState,
event: usize,
) -> FastStateMachineResult
pub fn trigger( &self, state: &FastCasState, event: usize, ) -> FastStateMachineResult
Applies one event atomically on state using the configured FastCas policy.
Reads the current code from state, resolves the transition for event, and stores
the new code back if the transition is valid.
§Arguments
state— Shared compact state updated by compare-and-swap.event— Event code to apply.
§Returns
Ok(new_state) after a successful transition and CAS store.
§Errors
FastStateMachineError::UnknownState— current code not in this machine.FastStateMachineError::UnknownTransition— no transition for(current, event).FastStateMachineError::CasConflict— CAS retries exhausted.
Sourcepub fn trigger_with<F>(
&self,
state: &FastCasState,
event: usize,
on_success: F,
) -> FastStateMachineResult
pub fn trigger_with<F>( &self, state: &FastCasState, event: usize, on_success: F, ) -> FastStateMachineResult
Like Self::trigger, but invokes on_success after the CAS update succeeds.
The callback receives (old_state, new_state) as observed for the successful
transition. It is not called when Self::trigger would return an error.
§Arguments
state— Shared compact state updated by compare-and-swap.event— Event code to apply.on_success— Called with previous and new state codes only on success.
§Returns
Same as Self::trigger: Ok(new_state) on success.
§Errors
Same as Self::trigger.
Sourcepub fn try_trigger(&self, state: &FastCasState, event: usize) -> bool
pub fn try_trigger(&self, state: &FastCasState, event: usize) -> bool
Attempts the same transition as Self::trigger, discarding error details.
§Returns
true if the transition and CAS update succeeded; false if validation failed or
CAS retries were exhausted.
Sourcepub fn try_trigger_with<F>(
&self,
state: &FastCasState,
event: usize,
on_success: F,
) -> bool
pub fn try_trigger_with<F>( &self, state: &FastCasState, event: usize, on_success: F, ) -> bool
Like Self::trigger_with, but returns only whether the transition succeeded.
on_success runs only when the CAS update succeeds, matching Self::trigger_with.
§Returns
true on success; false on any error that Self::trigger_with would surface.
Trait Implementations§
Source§impl Clone for FastStateMachine
impl Clone for FastStateMachine
Source§fn clone(&self) -> FastStateMachine
fn clone(&self) -> FastStateMachine
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more