Skip to main content

RuntimeTrait

Trait RuntimeTrait 

Source
pub trait RuntimeTrait:
    Into<Runtime>
    + Clone
    + Send
    + Sync
    + 'static {
    // Required methods
    fn spawn<Fut>(
        &self,
        fut: Fut,
    ) -> DroppableFuture<impl Future<Output = Option<Fut::Output>> + Send + 'static> 
       where Fut: Future + Send + 'static,
             Fut::Output: Send + 'static;
    fn delay(&self, duration: Duration) -> impl Future<Output = ()> + Send;
    fn interval(
        &self,
        period: Duration,
    ) -> impl Stream<Item = ()> + Send + 'static;
    fn block_on<Fut>(&self, fut: Fut) -> Fut::Output
       where Fut: Future;

    // Provided methods
    fn spawn_detached<Fut>(&self, fut: Fut)
       where Fut: Future<Output = ()> + Send + 'static { ... }
    fn timeout<'runtime, 'fut, Fut>(
        &'runtime self,
        duration: Duration,
        fut: Fut,
    ) -> impl Future<Output = Option<Fut::Output>> + Send + 'fut
       where Fut: Future + Send + 'fut,
             Fut::Output: Send + 'static,
             'runtime: 'fut { ... }
    fn hook_signals(
        &self,
        signals: impl IntoIterator<Item = i32>,
    ) -> impl Stream<Item = i32> + Send + 'static { ... }
}
Expand description

A trait that covers async runtime behavior.

You likely do not need to name this type. For a type-erased runtime, see Runtime

Required Methods§

Source

fn spawn<Fut>( &self, fut: Fut, ) -> DroppableFuture<impl Future<Output = Option<Fut::Output>> + Send + 'static>
where Fut: Future + Send + 'static, Fut::Output: Send + 'static,

Spawn a future on the runtime, returning a future that has detach-on-drop semantics

As the various runtimes each has different behavior for spawn, implementations of this trait are expected to conform to the following:

  • detach on drop: If the returned DroppableFuture is dropped immediately, the task will continue to execute until completion.

  • unwinding: If the spawned future panics, this must not propagate to the join handle. Instead, the awaiting the join handle returns None in case of panic.

Source

fn delay(&self, duration: Duration) -> impl Future<Output = ()> + Send

Wake in this amount of wall time

Source

fn interval(&self, period: Duration) -> impl Stream<Item = ()> + Send + 'static

Returns a Stream that yields a () on the provided period

Source

fn block_on<Fut>(&self, fut: Fut) -> Fut::Output
where Fut: Future,

Runtime implementation hook for blocking on a top level future.

Provided Methods§

Source

fn spawn_detached<Fut>(&self, fut: Fut)
where Fut: Future<Output = ()> + Send + 'static,

Spawn a future on the runtime without a join handle.

The output type is () because no completion signal or output is available to the caller. If the spawned future panics, the panic does not propagate to the spawning task; whether it is logged or swallowed is runtime-specific.

The default implementation spawns and immediately drops the join handle, relying on the detach-on-drop contract of spawn.

Source

fn timeout<'runtime, 'fut, Fut>( &'runtime self, duration: Duration, fut: Fut, ) -> impl Future<Output = Option<Fut::Output>> + Send + 'fut
where Fut: Future + Send + 'fut, Fut::Output: Send + 'static, 'runtime: 'fut,

Race a future against the provided duration, returning None in case of timeout.

Source

fn hook_signals( &self, signals: impl IntoIterator<Item = i32>, ) -> impl Stream<Item = i32> + Send + 'static

trap and return a Stream of signals that match the provided signals

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§