Skip to main content

Runtime

Trait Runtime 

Source
pub trait Runtime:
    Send
    + Sync
    + Debug
    + 'static {
    // Required methods
    fn spawn(&self, task: BoxFuture<()>);
    fn sleep(&self, duration: Duration) -> BoxFuture<()>;
    fn yield_now(&self) -> BoxFuture<()>;
    fn now(&self) -> Instant;

    // Provided method
    fn sleep_until(&self, deadline: Instant) -> BoxFuture<()> { ... }
}
Expand description

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.

A client takes its runtime from its builder, and falls back to default_runtime(); nothing is process-wide.

Required Methods§

Source

fn spawn(&self, task: BoxFuture<()>)

Run task to completion, detached.

Source

fn sleep(&self, duration: Duration) -> BoxFuture<()>

Complete after duration. Any duration, Duration::MAX included, is valid.

Source

fn yield_now(&self) -> BoxFuture<()>

Hand the thread back to the host once: work already queued (other tasks, I/O callbacks, a browser’s messages) runs before the caller continues, and no timer is involved.

It has no default because nothing generic really yields: re-waking the task at once only re-queues it, which under a browser’s microtask queue never reaches the event loop, and a zero sleep is no timer at all there. Tokio’s runtime defers to tokio::task::yield_now; the browser’s resumes on the next macrotask, through a MessageChannel post, which no timer clamp delays.

Source

fn now(&self) -> Instant

The runtime’s monotonic clock.

Provided Methods§

Source

fn sleep_until(&self, deadline: Instant) -> BoxFuture<()>

Complete at deadline, at once when it has passed.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§