pub trait FutureExt: Future {
// Provided methods
fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Self::Output>
where Self: Unpin { ... }
fn or<F>(self, other: F) -> Or<Self, F> ⓘ
where Self: Sized,
F: Future<Output = Self::Output> { ... }
fn catch_unwind(self) -> CatchUnwind<Self> ⓘ
where Self: Sized + UnwindSafe { ... }
fn boxed<'a>(
self,
) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
where Self: Sized + Send + 'a { ... }
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>
where Self: Sized + 'a { ... }
}
Expand description
Extension trait for Future
.
Provided Methods§
Sourcefn poll(&mut self, cx: &mut Context<'_>) -> Poll<Self::Output>where
Self: Unpin,
fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Self::Output>where
Self: Unpin,
A convenience for calling Future::poll()
on !
Unpin
types.
Sourcefn or<F>(self, other: F) -> Or<Self, F> ⓘ
fn or<F>(self, other: F) -> Or<Self, F> ⓘ
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);
Sourcefn catch_unwind(self) -> CatchUnwind<Self> ⓘwhere
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self> ⓘwhere
Self: Sized + UnwindSafe,
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());
Sourcefn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
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()];
Sourcefn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
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()];