Skip to main content

Module runtime

Module runtime 

Source
Available on crate feature 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 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::session_transport SessionTransport impls’ job (the byte-pipe abstraction). The concrete TcpStream / UdpSocket are transport-impl details, not runtime-level.

§Implementations

ImplStatusTarget
TokioRuntimeLinux / 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
EmbeddedRuntimescaffold (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_runtimeembedded
EmbeddedRuntime — minimal Runtime implementation for embeddings that do not (or cannot) carry tokio (Phase 3.1 scaffold).

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.