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§
Sourcefn wrap<F: Future>(&self, fut: F) -> TokioContext<F> ⓘ
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);
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.