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.
Runtimeis a detachedspawn, asleep, ayield_nowthat hands the thread to the host once with no timer, a monotonicnowand a defaultedsleep_until. An implementor writes only those.spawnturns any future into aTask, which resolves toSomeoutput, or toNonewhen it was aborted or panicked, and keeps running when it is dropped.timeoutbounds a future by a duration on a runtime’s clock, polling the future first, so a ready future wins atDuration::ZERO.intervalticks at once and then every period, and anIntervaltakes aMissedTickto say what to do about ticks it was late for.TokioRuntimeis the native runtime, on tokio’s own clock so a paused clock drives it, anddefault_runtimereturns it off wasm32. On wasm32BrowserRuntimeis the default:spawn_local,setTimeout, aMessageChannelpost andperformance.now.Instant,SystemTimeandUNIX_EPOCHareweb-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. - System
Time - 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.
- Tokio
Runtime - The runtime over tokio’s spawn, sleep, yield and clock.
Enums§
- Missed
Tick - What an
Intervaldoes about ticks it was too late to take on time.
Constants§
- UNIX_
EPOCH - An anchor in time which can be used to create new
SystemTimeinstances or learn about where in time aSystemTimelies.
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’sTask,timeout,Interval) is built on these, so an implementor writes only these.
Functions§
- default_
runtime - The runtime a client uses when none is injected:
TokioRuntimeon a host target andBrowserRuntimeonwasm32. - interval
- Ticks every
period, the first at once, onruntime’s clock. - spawn
- Spawn
futureonruntime, which needs only its detached spawn. - timeout
- Await
futurefor at mostdurationonruntime’s clock: its output, orNonewhendurationpassed first.