← ClaudeAtlas

rust-event-loop-statelisted

Use when you design or review an event loop, tick loop, or handler registry whose handlers all need &mut to one shared mutable state - a game loop, a TUI loop, or any god object every handler writes to. Covers the decision table that picks the structure from the shape of the handler set, why the loop must own the handler set and the state separately (E0499, E0502), state as a trait generic parameter instead of an associated type (E0207), capability bounds plus one Vec<Box<dyn Handler<App>>>, the blanket-impl one-way door (E0119), why DerefMut on a context wrapper destroys disjoint-field borrows, when an ECS-shaped dynamic world earns its run-time conflict panic, and why async fn(&mut State) and nightly coroutine resume arguments cannot express a suspendable routine over shared state. Triggers on "event loop", "tick loop", "handler registry", "shared mutable state", "god object", "ECS", "system and world", "Rc<RefCell> between handlers", "E0499 in my dispatch loop", or "coroutine resume".
po4yka/rust-skills · ★ 2 · AI & Automation · score 76
Install: claude install-skill po4yka/rust-skills
# Rust event loop state ## Purpose Decide which structure an event loop gets when every handler needs `&mut` to one shared state. The reader arrives with `E0499` in a dispatch loop. The answer is almost never a different line; it is a different owner. This skill picks the owner. The one sentence not to get wrong: **the loop owns the handler set and the state as two separate values, and passes one into the other.** Every other rule here follows from it. This skill covers synchronous loops. Runtime behaviour under tokio — `select!`, `JoinSet`, `FuturesUnordered`, waker plumbing, cancellation — belongs to `rust-async-internals`. Every diagnostic below comes from rustc 1.97.0, edition 2024, on aarch64-apple-darwin; the nightly ones from rustc 1.97.0-nightly (d7f14d3d8 2026-05-15). ## Pick the structure from the shape of the handler set Read the left column first. Stop at the first row that describes your handler set. Each row down costs something concrete, and you pay it whether or not you needed it. | Shape of the handler set | Structure | What it costs | | --- | --- | --- | | Fixed at compile time. The state's fields are known to the crate that writes the loop | The loop holds `Vec<Box<dyn Handler>>` and `State` as two locals. Dispatch `h.handle(&mut state, &ev)` | Nothing. Start here | | Handlers must be reused across applications that have different states | State is a trait parameter: `trait Handler<S>`. Each handler declares the capability traits it needs on `S`. The