Skip to main content

Crate polyoxide_rt

Crate polyoxide_rt 

Source
Expand description

§polyoxide-rt

The runtime seam of the polyoxide crates. A crate that sleeps, reads the clock or spawns takes a Runtime instead of calling a runtime’s own functions, so a runtime can be injected rather than assumed.

  • Runtime is a detached spawn, a sleep, a yield_now that hands the thread to the host once with no timer, a monotonic now and a defaulted sleep_until. An implementor writes only those.
  • spawn turns any future into a Task, which resolves to Some output, or to None when it was aborted or panicked, and keeps running when it is dropped.
  • timeout bounds a future by a duration on a runtime’s clock, polling the future first, so a ready future wins at Duration::ZERO.
  • interval ticks at once and then every period, and an Interval takes a MissedTick to say what to do about ticks it was late for.
  • TokioRuntime is the native runtime, on tokio’s own clock so a paused clock drives it, and default_runtime returns it off wasm32. On wasm32 BrowserRuntime is the default: spawn_local, setTimeout, a MessageChannel post and performance.now.
  • Instant, SystemTime and UNIX_EPOCH are web-time’s: std’s own types on native.
use std::time::Duration;

use polyoxide_rt::{default_runtime, spawn, timeout};

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let runtime = default_runtime();

    let task = spawn(&*runtime, async { 6 * 7 });
    assert_eq!(task.await, Some(42));

    let slow = runtime.sleep(Duration::from_secs(60));
    let waited = timeout(&*runtime, Duration::from_millis(10), slow).await;
    assert_eq!(waited, None);
}

The crate depends on nothing in the workspace and names no venue, so it publishes first and every other crate can use it.

Structs§

Instant
A measurement of a monotonically nondecreasing clock. Opaque and useful only with Duration.
Interval
A stream of ticks a period apart; built by interval.
SystemTime
A measurement of the system clock, useful for talking to external entities like the file system or other processes.
Task
A spawned future, as a future of its output.
TokioRuntime
The runtime over tokio’s spawn, sleep, yield and clock.

Enums§

MissedTick
What an Interval does about ticks it was too late to take on time.

Constants§

UNIX_EPOCH
An anchor in time which can be used to create new SystemTime instances or learn about where in time a SystemTime lies.

Traits§

Runtime
What a polyoxide crate needs of an async runtime: a detached spawn, a sleep, a yield to the host, and a monotonic clock. Everything else in this crate (spawn’s Task, timeout, Interval) is built on these, so an implementor writes only these.

Functions§

default_runtime
The runtime a client uses when none is injected: TokioRuntime on a host target and BrowserRuntime on wasm32.
interval
Ticks every period, the first at once, on runtime’s clock.
spawn
Spawn future on runtime, which needs only its detached spawn.
timeout
Await future for at most duration on runtime’s clock: its output, or None when duration passed first.

Type Aliases§

BoxFuture
An owned, sendable future: what a Runtime spawns and sleeps on.