Skip to main content

scoped

Function scoped 

Source
pub fn scoped<F: Future>(fut: F) -> impl Future<Output = F::Output>
Expand description

Run fut pinned to the CURRENT account, without spawning.

The counterpart to spawn_bound for work that is NOT a task we started: a Tauri command, an SDK call, a JNI entry point. Nothing installs a session for those, so every db:: call inside them re-resolves the live account and a swap mid-operation silently moves the rest of their writes. Wrapping the body fixes the account for its whole duration — a swap then leaves the operation completing against the account that asked for it, which is both the correct outcome and the one the user intended.

This is what makes the hand-written “did the account change?” checks unnecessary rather than merely redundant. NOT an async fn, and that is load-bearing. An async fn stores its parameters in the state machine it returns, so scoped would be sized to hold the body it binds — and nesting would multiply, exactly as passing the future to scope unboxed does. A plain fn returning impl Future hands back a wrapper around a Pin<Box<_>>: a pointer, whatever the body.

This is not hypothetical. Boot crashed with a stack overflow on BOTH Android and macOS, several layers into the community sync, on stacks every test here runs on happily. binding_a_future_does_not_embed_it is the guard.