How mdeloof/statig works
Main components
a. User/Application Layer
• Your Rust code that invokes #[state_machine], defines states, events, actions.
• Examples (examples/macro/*, examples/no_macro/*) and tests (statig/tests/*) live here.
b. Compile-time Proc-Macro Pipeline (macro/ crate)
• Entry point: macro/src/lib.rs (the #[state_machine] procedural macro).
• Submodules:
• parse.rs – Builds a syn::DeriveInput AST
• .rs – Validates and collects state machine metadata
• lower.rs – Converts AST to an intermediate representation (IR)
• codegen.rs – Emits Rust code: trait impls for State, Superstate, StateMachine
• visitors/ – Helpers (generic lifetimes, parameters, etc.)
c. Runtime Library (statig/ crate)
• lib.rs – Core traits and exports
• Modules:
• awaitable/ – Async state‐machine implementation
• blocking/ – Sync (blocking) state‐machine implementation
• response.rs – Response enum (Handled, Transition, Super)
• state_or_superstate.rs – Helper enum for introspection
• response.rs & response machinery
d. External Dependencies
• proc_macro, syn, quote, proc_macro2 (compile time)
• std/no_std (runtime feature)
• async (runtime feature)
Relationships and data flow
1. Your Rust source with #[state_machine]
2. Procedural macro entry (macro/src/lib.rs) invoked by Rust compiler
3. parse → analyze → lower → codegen → generate expanded Rust code
4. Expanded code plus statig runtime crate get compiled into the final binary
1. Main program calls YourType::state_machine() → returns a StateMachine instance
2. state_machine.handle(event) or handle_with_context
3. Dispatch to current leaf‐state handler
4. If Super, dispatch to its superstate, repeat until handled or top reached
5. On Transition, compute exit/entry sequence via shortest path in state hierarchy
6. Execute exit actions, then entry actions
c. Testing/Examples
• Examples drive both macro and runtime behavior
• Tests verify generated code vs hand‐written code
Architectural patterns
• Separation of concerns:
• Proc‐macro pipeline (compile time) vs runtime library
• Hierarchical state machine pattern (tree of states + superstates)
• Dual runtime implementations: async (awaitable/) vs sync (blocking/)
• No-std compatibility (zero-allocation, ROM-based machines)
Relevant technologies/frameworks
• Rust Proc-Macros: proc_macro, syn, quote
• Core library: statig crate
• Features: async (generates an async state‐machine), no_std