Skip to main content

StateTrait

Trait StateTrait 

Source
pub trait StateTrait: StateTraitZst + 'static {
    // Required method
    fn type_name(&self) -> &'static str;
}
Expand description

Runtime identity for a state marker after the concrete marker type has been erased.

Most users never implement this trait directly. State marker types should be declared with States!, which wires this trait, StateMarker, concrete-state classification, and the static marker instance used by shared storage and tracing.

The erased marker is intentionally only an identity token. It is not the runtime data controlled by the state machine, and it is not used to make a transition valid. Valid transitions are still proven by the generic State<Storage, T, S> type and by the Transition contract. StateTrait is used at runtime only where the compiler cannot keep one concrete state in the type, for example:

  • a shared SRcRefCell or SArcMutex stores the currently committed state next to the data so a later borrow can check borrow::<Connected>();
  • TraceEntry records the source and destination state of each transition without making the trace vector generic over every state pair.

With the default feature set, erased states are stored as &'static dyn StateTrait. With the dynZST feature, they are stored through dynzst::DynZSTBox<dyn StateTrait>, and marker types must satisfy dynzst::IsZeroSized. The States! macro generates zero-sized marker structs, so normal users get the correct invariant without writing any unsafe code.

use magicstatemachines::{StateTrait, States};

States! {
    Disconnected;
    Connected;
}

let state: &'static dyn StateTrait = Disconnected::erased_state();
assert!(state.type_name().ends_with("::Disconnected"));

Required Methods§

Source

fn type_name(&self) -> &'static str

Fully qualified Rust type name of the concrete state marker.

This is meant for diagnostics and tests. Use typed APIs such as borrow::<Connected>() or generated union discrimination when the result should affect control flow.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T> StateTrait for T
where T: StateMarker + StateTraitZst + 'static,