std only.Expand description
Async runtime abstraction (Phase 3.1).
phantom_protocol historically hard-coded tokio everywhere: every
tokio::spawn, every tokio::time::sleep, every tokio::sync::Mutex,
every tokio::net::TcpStream. That couples the library to a
multi-threaded executor that does not exist on wasm32-unknown-unknown
(single-threaded, JS event loop) or on bare-metal embedded targets
(no executor at all without embassy/RTIC).
This module introduces a small trait surface — Runtime — that the
rest of the crate uses in place of direct tokio calls. The default
implementation, TokioRuntime, preserves the historical behavior
verbatim, so the UniFFI-stable surface (connect_with_transport,
bind, …) keeps running on tokio; non-tokio embedders inject an
alternative via the _with_runtime constructors.
§What the trait covers
- Task spawning —
spawn(boxed_future) -> SpawnHandle. The handle exposes a non-blockingabort()so callers can cancel the task. - Sleep —
sleep(duration) -> BoxFuture<()>for delay loops. - Monotonic clock —
now_monotonic() -> Instantfor RTT and expiry math. - Wall-clock —
now_wall_clock() -> SystemTimefor cookie buckets and timestamp-bound material.
§What the trait deliberately does NOT cover (yet)
- Channels —
tokio::sync::mpscis portable enough acrosstokioandtokioderivatives that we keep using it directly. - Mutexes — see above.
- Network I/O — this is the
crate::transport::session_transportSessionTransportimpls’ job (the byte-pipe abstraction). The concreteTcpStream/UdpSocketare transport-impl details, not runtime-level.
§Implementations
| Impl | Status | Target |
|---|---|---|
TokioRuntime | ✅ | Linux / macOS / Windows / iOS / Android servers and clients |
WasmRuntime | ✅ (cfg(target_arch = "wasm32", target_os = "unknown")) | browser wasm32-unknown-unknown via wasm-bindgen-futures |
WasiRuntime | ✅ (feature wasi-leg, cfg(target_os = "wasi")) | WASI Preview 2 single-task executor |
EmbeddedRuntime | scaffold (features embedded + std) | host-side stand-in; bare metal ships its own embassy / RTIC impl |
Only TokioRuntime is unconditional; the other three are each gated to
their target / feature (see the cfgs on the module declarations below).
EmbeddedRuntime is a one-thread-per-future stand-in for host tests — see
embedded_runtime.rs for why it is not a production bare-metal executor.
Re-exports§
pub use embedded_runtime::EmbeddedRuntime;embedded
Modules§
- embedded_
runtime embedded EmbeddedRuntime— minimalRuntimeimplementation for embeddings that do not (or cannot) carry tokio (Phase 3.1 scaffold).
Structs§
- Spawn
Handle - Opaque handle to a spawned task. Created by
Runtime::spawn. - Tokio
Runtime - Tokio-backed
Runtime. Zero-sized — construct withTokioRuntimeand wrap inArcto share across tasks.
Traits§
- Runtime
- Async runtime abstraction.
- Spawn
Handle Inner - Implementation detail of
SpawnHandle. Runtime adapters implement this on their concrete handle type.
Type Aliases§
- BoxFuture
- Boxed, owned,
Sendfuture of unit output — the shapeRuntime::spawnandRuntime::sleepwork in.