pub trait FutureExt: Future {
// Provided methods
fn timeout(self, timeout: Duration) -> Timeout<Self>
where Self: Sized { ... }
fn timeout_at(self, deadline: Instant) -> Timeout<Self>
where Self: Sized { ... }
fn with_cancellation_token(
self,
cancellation_token: &CancellationToken,
) -> WithCancellationTokenFuture<'_, Self>
where Self: Sized { ... }
fn with_cancellation_token_owned(
self,
cancellation_token: CancellationToken,
) -> WithCancellationTokenFutureOwned<Self>
where Self: Sized { ... }
}
time
only.Expand description
A trait which contains a variety of convenient adapters and utilities for Future
s.
Provided Methods§
Sourcefn timeout(self, timeout: Duration) -> Timeout<Self>where
Self: Sized,
fn timeout(self, timeout: Duration) -> Timeout<Self>where
Self: Sized,
A wrapper around tokio::time::timeout
, with the advantage that it is easier to write
fluent call chains.
§Examples
use tokio::{sync::oneshot, time::Duration};
use tokio_util::future::FutureExt;
let (_tx, rx) = oneshot::channel::<()>();
let res = rx.timeout(Duration::from_millis(10)).await;
assert!(res.is_err());
Sourcefn timeout_at(self, deadline: Instant) -> Timeout<Self>where
Self: Sized,
fn timeout_at(self, deadline: Instant) -> Timeout<Self>where
Self: Sized,
A wrapper around tokio::time::timeout_at
, with the advantage that it is easier to write
fluent call chains.
§Examples
use tokio::{sync::oneshot, time::{Duration, Instant}};
use tokio_util::future::FutureExt;
let (_tx, rx) = oneshot::channel::<()>();
let deadline = Instant::now() + Duration::from_millis(10);
let res = rx.timeout_at(deadline).await;
assert!(res.is_err());
Sourcefn with_cancellation_token(
self,
cancellation_token: &CancellationToken,
) -> WithCancellationTokenFuture<'_, Self>where
Self: Sized,
fn with_cancellation_token(
self,
cancellation_token: &CancellationToken,
) -> WithCancellationTokenFuture<'_, Self>where
Self: Sized,
Similar to CancellationToken::run_until_cancelled
,
but with the advantage that it is easier to write fluent call chains,
and biased towards waiting for CancellationToken
to complete.
§Fairness
Calling this on an already-cancelled token directly returns None
.
For all subsequent polls, in case of concurrent completion and
cancellation, this is biased towards the future completion.
§Examples
use tokio::sync::oneshot;
use tokio_util::future::FutureExt;
use tokio_util::sync::CancellationToken;
let (_tx, rx) = oneshot::channel::<()>();
let token = CancellationToken::new();
let token_clone = token.clone();
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
token.cancel();
});
assert!(rx.with_cancellation_token(&token_clone).await.is_none())
Sourcefn with_cancellation_token_owned(
self,
cancellation_token: CancellationToken,
) -> WithCancellationTokenFutureOwned<Self>where
Self: Sized,
fn with_cancellation_token_owned(
self,
cancellation_token: CancellationToken,
) -> WithCancellationTokenFutureOwned<Self>where
Self: Sized,
Similar to CancellationToken::run_until_cancelled_owned
,
but with the advantage that it is easier to write fluent call chains,
and biased towards waiting for CancellationToken
to complete.
§Fairness
Calling this on an already-cancelled token directly returns None
.
For all subsequent polls, in case of concurrent completion and
cancellation, this is biased towards the future completion.
§Examples
use tokio::sync::oneshot;
use tokio_util::future::FutureExt;
use tokio_util::sync::CancellationToken;
let (_tx, rx) = oneshot::channel::<()>();
let token = CancellationToken::new();
let token_clone = token.clone();
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
token.cancel();
});
assert!(rx.with_cancellation_token_owned(token_clone).await.is_none())