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 can use in place of direct tokio calls. The default
implementation, TokioRuntime, preserves today’s behavior verbatim.
Follow-up commits will gradually migrate call sites; this commit lands
only the trait + the default impl + tests.
§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::legstrait’s job.TcpStream/UdpSocketare leg-impl details, not runtime-level.
§Implementations
| Impl | Status | Target |
|---|---|---|
TokioRuntime | ✅ | Linux / macOS / Windows / iOS / Android servers and clients |
WasmRuntime | ✅ (cfg(target_arch = "wasm32")) | wasm32-unknown-unknown browsers via wasm-bindgen-futures |
EmbeddedRuntime | scaffold | embassy / bare metal |
The non-tokio implementations are scaffolded under future feature flags; this commit only ships the trait and the tokio default so the call-site migration can begin against a stable abstraction.
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.