Skip to main content

FastStateMachine

Struct FastStateMachine 

Source
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

Source

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.

Source

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).

Source

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).

Source

pub fn transitions(&self) -> &[usize]

Returns the dense transition table.

The table is laid out row-major: source index first, then event index.

Source

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.

Source

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.

Source

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.

Source

pub const fn contains_state(&self, state: usize) -> bool

Returns whether state is a valid code for this machine.

§Arguments
  • state — Candidate state code.
§Returns

true if state < state_count(), otherwise false.

Source

pub fn is_initial_state(&self, state: usize) -> bool

Returns whether state was configured as an initial state.

§Arguments
  • state — State code to test.
§Returns

true if state is in range and marked initial; false if out of range or not initial.

Source

pub fn is_final_state(&self, state: usize) -> bool

Returns whether state was configured as a final state.

§Arguments
  • state — State code to test.
§Returns

true if state is in range and marked final; false if out of range or not final.

Source

pub fn transition_target(&self, source: usize, event: usize) -> Option<usize>

Looks up the next state for a specific (source, event) pair.

§Arguments
  • source — Current state code.
  • event — Event code.
§Returns

Some(target) when a transition is configured; None if source or event is out of range, or if no transition exists for that pair.

Source

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
Source

pub fn trigger_with<F>( &self, state: &FastCasState, event: usize, on_success: F, ) -> FastStateMachineResult
where F: Fn(usize, usize),

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.

Source

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.

Source

pub fn try_trigger_with<F>( &self, state: &FastCasState, event: usize, on_success: F, ) -> bool
where F: Fn(usize, usize),

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

Source§

fn clone(&self) -> FastStateMachine

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FastStateMachine

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoResult<T> for T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.