rust-event-loop-statelisted
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