rust-event-loop-state

Diagnose and restructure Rust event loops sharing mutable state across handlers.

2|1|Updated Aug 19, 2026
One-click install
npx skills add https://github.com/po4yka/rust-skills --skill rust-event-loop-state-po4yka
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: rust-event-loop-state
Source: https://github.com/po4yka/rust-skills/tree/main/skills/rust-event-loop-state
Command: npx skills add https://github.com/po4yka/rust-skills --skill rust-event-loop-state-po4yka

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Rust event loops, tick loops, and handler registries often fail to compile when every handler needs &mut access to one shared state, producing errors like E0499, E0502, E0207, and E0119, or runtime panics from Rc<RefCell<_>> and ECS access conflicts. This Skill provides triage tables, structural patterns, and verified fixes for designing or reviewing such loops. ## Core Features & Use Cases - Structure selection: Pick the right architecture from a decision table, from a simple Vec<Box<dyn Handler>> plus separate State, to generic Handler<S> traits with capability bounds, to an ECS-shaped dynamic world. - Diagnostic triage: Map compiler errors (E0499, E0502, E0207, E0119) and runtime panics (RefCell already borrowed, ECS B0001 conflicts) directly to their causes and repairs. - Suspendable routines: Implement coroutine-style routines over shared state with a resume(&mut self, &mut State) trait instead of async fn(&mut State), which cannot work. - Use Case: A game loop stores its handler collection inside the game state and fails with E0499 on dispatch. Use this Skill to move the collection out, drain the event queue by value, and replace handler-held borrows with generational keys. ## Quick Start Ask the agent to review your Rust event loop that fails with E0499 when handlers mutate shared state and apply the recommended ownership structure.

Frequently Asked Questions about rust-event-loop-state

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix E0499 cannot borrow as mutable more than once in a Rust event loop?▼

E0499 in a dispatch loop usually means the handler collection is a field of the state being passed as `&mut`. Move the collection out of the state so the loop owns both as separate locals, or dispatch a disjoint sub-struct field instead.

How to share mutable state between handlers in Rust without RefCell?▼

Keep the handler registry and the state as two separate values owned by the loop, and pass `&mut State` into each handler call. Handlers that need persistent access to a state field store a key or index, never a borrow, and resolve it at the top of each call.

Should I use a trait parameter or associated type for handler state in Rust?▼

Use a trait parameter `Handler<S>`, not an associated `type State`. An associated type leaves the state type unconstrained and fails with E0207, while a trait parameter keeps the trait dyn compatible and lets each handler bound S by capability traits.

Can I use async fn with &mut State as a suspendable routine in Rust?▼

No. An `async fn(&mut State)` holds the borrow for the future's entire life, causing E0499 before any executor runs. Define a `resume(&mut self, st: &mut State) -> Step` trait instead, which takes a fresh reborrow on every call.

Why does my bevy_ecs system panic with B0001 conflicting system parameters?▼

B0001 fires when one system requests two overlapping mutable views of the same component, detected at schedule initialization, not compile time. Repair it with a ParamSet to access the queries one at a time, or disjoint With/Without filters, and enable the debug feature to name the conflict.

When should I use an ECS instead of a plain struct with a handler registry?▼

Use an ECS only when the component set is decided at run time by plugins, scripts, save files, or an editor. Otherwise a plain struct gives compile-time aliasing checks, while an ECS moves those errors to runtime panics when the schedule initializes.