Trait tokio_util::context::RuntimeExt[][src]

pub trait RuntimeExt {
    fn wrap<F: Future>(&self, fut: F) -> TokioContext<F>
Notable traits for TokioContext<F>
impl<F: Future> Future for TokioContext<F> type Output = F::Output;
; }
This is supported on crate feature rt only.
Expand description

Extension trait that simplifies bundling a Handle with a Future.

Required methods

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);

Implementations on Foreign Types

Implementors