Tokio bridge (id_effect_tokio)
The core id_effect crate defines interpreters (run_blocking, run_async, run_fork, …) and the Runtime trait. Workspace crate id_effect_tokio supplies the Tokio-backed implementation you use in binaries and services that already run on #[tokio::main].
Read this section before Platform I/O, HTTP via reqwest, and Axum: every adapter below ultimately drives effects with the same Tokio integration rules.
What id_effect_tokio provides
TokioRuntime— implements [id_effect::Runtime]: cooperative sleep and yield, and schedules forked fibers on Tokio’s blocking thread pool viaspawn_blocking+ an internalrun_blockingdriver (the effect graph itself is not assumedSendfortokio::spawn).- Re-exports —
run_async,run_blocking,run_fork, andyield_nowfromid_effectfor convenient use at the async boundary next toTokioRuntime. spawn_blocking_run_async— when an async effect graph is still notSend(scopes, pool checkouts, …) but must be driven by the real async interpreter (run_async) rather than the blocking-only fiber driver, this pattern runs construction +run_asyncon Tokio’s blocking pool withHandle::block_oninside.id_effect_axumuses the same idea so Axum handlers returnSendfutures.
Mental model
| Concern | Where it lives |
|---|---|
| Describing work | Effect<A, E, R> in your domain |
| Blocking / tests | run_blocking(eff, env) |
| Async I/O on Tokio | id_effect_tokio::run_async(eff, env) with a live runtime |
| Fibers vs OS threads | run_fork + join / interrupt (see Concurrency) |
id_effect_tokio does not replace the effect system; it wires the interpreter to Tokio timers and thread pools so Effect::new_async steps compose with await from the host runtime.
Sharp edges
Send: futures produced byrun_asyncare often notSend. Do not stash them in atokio::spawntask without an adapter (Axum/Tower helpers handle this explicitly).- Router tests:
#[tokio::test]defaults to current-thread runtime;id_effect_axumdocuments thatmulti_threadflavor is required for its bridge. Prefer#[tokio::test(flavor = "multi_thread", worker_threads = 2)]when exercising Axum + effects together.
Further reading
- Crate docs:
cargo doc --open -p id_effect_tokio - Examples under
crates/id_effect_tokio/examples/(e.g. end-to-end Tokio wiring)