Trait tokio_util::context::RuntimeExt

source ·
pub trait RuntimeExt {
    // Required method
    fn wrap<F: Future>(&self, fut: F) -> TokioContext<F> ;
}
Available on crate feature rt only.
Expand description

Extension trait that simplifies bundling a Handle with a Future.

Required Methods§

source

fn wrap<F: Future>(&self, fut: F) -> TokioContext<F>

Create a TokioContext that wraps the provided future and runs it in this runtime’s context.

§Examples

This example creates two runtimes, but only enables time on one of them. It then uses the context of the runtime with the timer enabled to execute a sleep future on the runtime with timing disabled.

use tokio::time::{sleep, Duration};
use tokio_util::context::RuntimeExt;

// This runtime has timers enabled.
let rt = tokio::runtime::Builder::new_multi_thread()
    .enable_all()
    .build()
    .unwrap();

// This runtime has timers disabled.
let rt2 = tokio::runtime::Builder::new_multi_thread()
    .build()
    .unwrap();

// Wrap the sleep future in the context of rt.
let fut = rt.wrap(async { sleep(Duration::from_millis(2)).await });

// Execute the future on rt2.
rt2.block_on(fut);

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl RuntimeExt for Runtime

source§

fn wrap<F: Future>(&self, fut: F) -> TokioContext<F>

Implementors§