Skip to main content

Module runtime

Module runtime 

Source
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 spawningspawn(boxed_future) -> SpawnHandle. The handle exposes a non-blocking abort() so callers can cancel the task.
  • Sleepsleep(duration) -> BoxFuture<()> for delay loops.
  • Monotonic clocknow_monotonic() -> Instant for RTT and expiry math.
  • Wall-clocknow_wall_clock() -> SystemTime for cookie buckets and timestamp-bound material.

§What the trait deliberately does NOT cover (yet)

  • Channelstokio::sync::mpsc is portable enough across tokio and tokio derivatives that we keep using it directly.
  • Mutexes — see above.
  • Network I/O — this is the crate::transport::legs trait’s job. TcpStream / UdpSocket are leg-impl details, not runtime-level.

§Implementations

ImplStatusTarget
TokioRuntimeLinux / macOS / Windows / iOS / Android servers and clients
WasmRuntime✅ (cfg(target_arch = "wasm32"))wasm32-unknown-unknown browsers via wasm-bindgen-futures
EmbeddedRuntimescaffoldembassy / 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§

SpawnHandle
Opaque handle to a spawned task. Created by Runtime::spawn.
TokioRuntime
Tokio-backed Runtime. Zero-sized — construct with TokioRuntime and wrap in Arc to share across tasks.

Traits§

Runtime
Async runtime abstraction.
SpawnHandleInner
Implementation detail of SpawnHandle. Runtime adapters implement this on their concrete handle type.

Type Aliases§

BoxFuture
Boxed, owned, Send future of unit output — the shape Runtime::spawn and Runtime::sleep work in.