pub trait FutureExt: Future {
fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Self::Output>
where
Self: Unpin,
{ ... }
fn or<F>(self, other: F) -> Or<Self, F>ⓘNotable traits for Or<F1, F2>impl<T, F1, F2> Future for Or<F1, F2> where
F1: Future<Output = T>,
F2: Future<Output = T>, type Output = T;
where
F: Future<Output = Self::Output>,
{ ... }
fn race<F>(self, other: F) -> Race<Self, F>ⓘNotable traits for Race<F1, F2>impl<T, F1, F2> Future for Race<F1, F2> where
F1: Future<Output = T>,
F2: Future<Output = T>, type Output = T;
where
F: Future<Output = Self::Output>,
{ ... }
fn catch_unwind(self) -> CatchUnwind<Self>ⓘNotable traits for CatchUnwind<F>impl<F> Future for CatchUnwind<F> where
F: Future + UnwindSafe, type Output = Result<<F as Future>::Output, Box<dyn Any + Send + 'static, Global>>;
where
Self: UnwindSafe,
{ ... }
fn boxed<'a>(
self
) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a, Global>>ⓘNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
where
Self: 'a + Send,
{ ... }
fn boxed_local<'a>(
self
) -> Pin<Box<dyn Future<Output = Self::Output> + 'a, Global>>ⓘNotable traits for Pin<P>impl<P> Future for Pin<P> where
P: DerefMut,
<P as Deref>::Target: Future, type Output = <<P as Deref>::Target as Future>::Output;
where
Self: 'a,
{ ... }
}
Expand description
Extension trait for Future
.
Provided methods
A convenience for calling Future::poll()
on !
Unpin
types.
Returns the result of self
or other
future, preferring self
if both are ready.
If you need to treat the two futures fairly without a preference for either, use the
race()
function or the FutureExt::race()
method.
Examples
use futures_lite::future::{pending, ready, FutureExt};
assert_eq!(ready(1).or(pending()).await, 1);
assert_eq!(pending().or(ready(2)).await, 2);
// The first future wins.
assert_eq!(ready(1).or(ready(2)).await, 1);
Returns the result of self
or other
future, with no preference if both are ready.
Each time Race
is polled, the two inner futures are polled in random order. Therefore,
no future takes precedence over the other if both can complete at the same time.
If you have preference for one of the futures, use the or()
function or the
FutureExt::or()
method.
Examples
use futures_lite::future::{pending, ready, FutureExt};
assert_eq!(ready(1).race(pending()).await, 1);
assert_eq!(pending().race(ready(2)).await, 2);
// One of the two futures is randomly chosen as the winner.
let res = ready(1).race(ready(2)).await;
fn catch_unwind(self) -> CatchUnwind<Self>ⓘNotable traits for CatchUnwind<F>impl<F> Future for CatchUnwind<F> where
F: Future + UnwindSafe, type Output = Result<<F as Future>::Output, Box<dyn Any + Send + 'static, Global>>;
where
Self: UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>ⓘNotable traits for CatchUnwind<F>impl<F> Future for CatchUnwind<F> where
F: Future + UnwindSafe, type Output = Result<<F as Future>::Output, Box<dyn Any + Send + 'static, Global>>;
where
Self: UnwindSafe,
F: Future + UnwindSafe, type Output = Result<<F as Future>::Output, Box<dyn Any + Send + 'static, Global>>;
Catches panics while polling the future.
Examples
use futures_lite::future::FutureExt;
let fut1 = async {}.catch_unwind();
let fut2 = async { panic!() }.catch_unwind();
assert!(fut1.await.is_ok());
assert!(fut2.await.is_err());
Boxes the future and changes its type to dyn Future + Send + 'a
.
Examples
use futures_lite::future::{self, FutureExt};
let a = future::ready('a');
let b = future::pending();
// Futures of different types can be stored in
// the same collection when they are boxed:
let futures = vec![a.boxed(), b.boxed()];
Boxes the future and changes its type to dyn Future + 'a
.
Examples
use futures_lite::future::{self, FutureExt};
let a = future::ready('a');
let b = future::pending();
// Futures of different types can be stored in
// the same collection when they are boxed:
let futures = vec![a.boxed_local(), b.boxed_local()];