Skip to main content

Module guards

Module guards 

Source
Expand description

GuardStore: type-erased storage for per-(buffer, mode) Guards returned by Mode::on_activate.

The dispatcher stashes each successful activation’s Guard in this store, keyed by (BufferId, ModeId). On deactivation, the dispatcher removes the entry and drops the boxed Guard; the Guard’s Drop impl performs cleanup (unsubscribe, restore prior option value, drop supervisor handle, …).

Storage is Box<dyn Any + Send> because each mode’s Guard type is different. The dispatcher never downcasts – dropping through the dyn Any trait object correctly invokes the original type’s Drop via the vtable.

M-async.2: the store is accessed from two threads – the App thread (synchronous deactivate path; activation’s sync prefix) and the tokio worker that runs the spawned lifecycle future (inserts the Guard when on_activate resolves). The GuardStoreHandle wraps the store in Arc<Mutex<...>> so both threads can lock briefly without &mut lifetime gymnastics. The App owns one handle; the dispatcher clones it into each spawned task.

Structs§

GuardStore
Type-erased per-(buffer, mode) Guard storage.
GuardStoreHandle
Cheap-clone, thread-safe handle to a GuardStore. The App owns one; the dispatcher clones it into each spawned lifecycle task so the task can lock + insert the Guard on completion. Locks are held briefly (single map mutation per activation / deactivation); std::sync::Mutex is correct because no .await happens inside the lock.