Expand description
Ergonomic typestate wrappers for compiler-enforced state machines.
This crate requires nightly Rust for arbitrary_self_types.
MagicStateMachines lets a state-machine contract live separately from the
runtime type that implements it. A definition crate owns the stand-in type,
state marker ZSTs, initial states, legal transitions, and state unions. An
implementation crate connects a runtime type to that contract with
StateMachineImpl! and exposes ordinary
inherent methods whose receiver type carries the current state.
The core receiver type is State<Storage, T, S>, where T is the runtime
implementation, S is the current state marker, and Storage selects how
the runtime value is held. Methods usually constrain storage by capability:
SRef for read-only access, SMut for mutable transitions, SPinMut
for pinned transitions, and SMove when storage must move by value. This
allows the same state-machine methods to work for owned values, boxes,
pinned boxes, shared guard views, and custom storage backends.
In the default configuration this crate denies unsafe code. The dynZST
feature uses the external dynzst crate for thin erased ZST state markers.
Without tracing, directly owned state wrappers are layout-transparent over
the runtime data, and concrete-state transitions are statically dispatched.
When state crosses a boundary the compiler cannot prove, such as shared
storage behind Rc, Arc, RefCell, Mutex, or RwLock, the committed
erased state is checked at the boundary before returning a typed view.
State unions support methods over a set of states. A union such as
Online generates a sealed membership trait such as InOnline, a
discriminated state representation, and an enum such as OnlineEnum.
Static union transitions use transition! with
the const form when all members share the same transition body. Dynamic
union transitions use the dyn form when the concrete member must be
discriminated first.
Macros§
- State
Machine Definition - Defines the state-machine contract for a stand-in type.
- State
Machine Impl - Connects a runtime type to a state-machine definition and generates its effects.
- State
Union - Defines a named union of concrete state markers.
- States
- Defines concrete state marker ZSTs.
- transition
- Performs a transition inside a module that invoked
StateMachineImpl!.
Structs§
- Concrete
State Kind - Marker kind for concrete state ZSTs.
- Concrete
Stated - Raw runtime data paired with a concrete state marker.
- DynState
- Direct erased-state storage without a runtime borrow checker.
- DynStorage
- Storage marker used by
DynStateviews. - Mutex
Storage SharedStorageimplementation backed byMutex.- RefCell
Storage SharedStorageimplementation backed byRefCell.- RwLock
Storage SharedStorageimplementation backed byRwLock.- Shared
State - Shared state using an explicit, replaceable storage backend.
- Shared
Value - The state marker and runtime data held by a shared-storage backend.
- State
- A state token parameterized by storage, runtime implementation, and state.
- State
Mut - Mutable typed view into shared state.
- State
Owned - A directly owned runtime implementation
Twhose compile-time state isS. - State
Ref - Immutable shared-state read guard used as
Statestorage. - Storage
State Mut - Generic
Statebackend for a mutable shared-state guard. - Storage
State Owned - Backend for directly owned values.
- Storage
State Owned Box - Backend for
Box<T>owned values. - Storage
State Owned PinBox - Backend for
Pin<Box<T>>owned values. - Storage
State Ref - Generic
Statebackend for an immutable shared-state guard. - Union
State Kind - Marker kind for generated union state markers.
- WeakS
Arc - Weak counterpart to
SArc. - WeakSRc
- Weak counterpart to
SRc. - Wrong
State Error - Failure caused by asking a shared container for the wrong state marker.
Enums§
- Shared
State Error - Failure to acquire a typed view of shared state.
Traits§
- EnumExt
- Convenience extension for converting a state into a union’s generated enum.
- In
- Marks a state as being a concrete marker or a member of a generated state union.
- Initial
- Declares that a definition crate permits
TStateas an initial state. - MayTransition
- Storage backend whose state token may be consumed and retagged by a transition.
- SMap
Runtime - Storage backend that can transform the runtime value while preserving state.
- SMove
- Storage backend whose state token can be consumed by value.
- SMut
- Storage backend that can expose a mutable runtime reference.
- SPinMut
- Storage backend that can expose a pinned mutable runtime reference.
- SPinRef
- Storage backend that can expose a pinned runtime reference.
- SRef
- Storage backend that can expose a runtime reference.
- Shared
Storage - Replaceable storage backend for
super::SharedState. - Shared
Storage View - Guard types used by runtime-erased state views.
- State
Clone - State policy permitting
crate::Stateto clone its runtime value. - State
Copy - State policy permitting
crate::Stateto copy its runtime value. - State
Kind - Classifies state marker types.
- State
Machine Impl - Connects an implementation type to a state-machine definition.
- State
Marker - Common trait implemented by concrete states and generated union markers.
- State
Storage - Storage backend used by
State. - State
Storage New - Storage backend that can create initial owned state.
- State
Trait - Runtime identity for a state marker after the concrete marker type has been erased.
- State
Union Discriminant - Selects the value-carrying enum type for a union marker.
- Transition
- Declares that a definition crate permits
TFrom -> TTo.
Functions§
- pin_mut
- Mutably borrows a state’s runtime value through its storage’s pin guarantee.
- pin_ref
- Borrows a state’s runtime value through its storage’s pin guarantee.
- transition
- Creates a callable transition requiring the definition’s arguments.
- transition_
mut - Creates a callable transition for a mutable shared-state guard.
- transition_
state - Creates a callable transition for generic state storage.
Type Aliases§
- Discriminated
State - Union-typed state that still remembers its concrete variant.
- Runtime
State Marker - Runtime marker used when borrowing
Markerfrom shared storage. - SArc
- Shared state backed by
Arc<Storage::Storage<T>>. - SArc
Mutex - Shared state backed by
Arc<Mutex<...>>. - SArc
RwLock - Shared state backed by
Arc<RwLock<...>>. - SBox
- Owned state whose runtime value is stored in
Box<T>. - SMut
View - State view held by a mutable guard from a shared storage backend.
- SMutex
- Mutable-guard storage backend for
MutexStorage. - SOwned
- Short alias for
StorageStateOwned. - SPin
- Owned state whose runtime value is pinned by an arbitrary pinning pointer.
- SPinBox
- Owned state whose runtime value is stored in
Pin<Box<T>>. - SRc
- Shared state backed by
Rc<Storage::Storage<T>>. - SRcRef
Cell - Shared state backed by
Rc<RefCell<...>>. - SRef
Cell - Mutable-guard storage backend for
RefCellStorage. - SRef
View - State view held by an immutable guard from a shared storage backend.
- SResult
- A result whose success and error values are states of the same machine.
- SRwLock
- Mutable-guard storage backend for
RwLockStorage. - WeakS
ArcMutex - Weak handle for
SArcMutex. - WeakS
ArcRw Lock - Weak handle for
SArcRwLock. - WeakS
RcRef Cell - Weak handle for
SRcRefCell.