[][src]Trait async_std::prelude::FutureExt

pub trait FutureExt: Future {
    fn delay(self, dur: Duration) -> DelayFuture<Self>
    where
        Self: Future + Sized
, { ... }
fn flatten(
        self
    ) -> FlattenFuture<Self, <Self::Output as IntoFuture>::Future>
    where
        Self: Future + Sized,
        Self::Output: IntoFuture
, { ... }
fn race<F>(self, other: F) -> Race<Self, F>
    where
        Self: Future + Sized,
        F: Future<Output = Self::Output>
, { ... }
fn try_race<F: Future, T, E>(self, other: F) -> TryRace<Self, F>
    where
        Self: Future<Output = Result<T, E>> + Sized,
        F: Future<Output = Self::Output>
, { ... } }

Extension methods for Future.

Provided methods

fn delay(self, dur: Duration) -> DelayFuture<Self> where
    Self: Future + Sized

This is supported on unstable only.

Returns a Future that delays execution for a specified time.

Examples

use async_std::prelude::*;
use async_std::future;
use std::time::Duration;

let a = future::ready(1).delay(Duration::from_millis(2000));
dbg!(a.await);

fn flatten(self) -> FlattenFuture<Self, <Self::Output as IntoFuture>::Future> where
    Self: Future + Sized,
    Self::Output: IntoFuture

This is supported on unstable only.

Flatten out the execution of this future when the result itself can be converted into another future.

Examples

use async_std::prelude::*;

let nested_future = async { async { 1 } };
let future = nested_future.flatten();
assert_eq!(future.await, 1);

fn race<F>(self, other: F) -> Race<Self, F> where
    Self: Future + Sized,
    F: Future<Output = Self::Output>, 

This is supported on unstable only.

Waits for one of two similarly-typed futures to complete.

Awaits multiple futures simultaneously, returning the output of the first future that completes.

This function will return a new future which awaits for either one of both futures to complete. If multiple futures are completed at the same time, resolution will occur in the order that they have been passed.

Note that this function consumes all futures passed, and once a future is completed, all other futures are dropped.

Examples

use async_std::prelude::*;
use async_std::future;

let a = future::pending();
let b = future::ready(1u8);
let c = future::ready(2u8);

let f = a.race(b).race(c);
assert_eq!(f.await, 1u8);

fn try_race<F: Future, T, E>(self, other: F) -> TryRace<Self, F> where
    Self: Future<Output = Result<T, E>> + Sized,
    F: Future<Output = Self::Output>, 

This is supported on unstable only.

Waits for one of two similarly-typed fallible futures to complete.

Awaits multiple futures simultaneously, returning all results once complete.

try_race is similar to race, but keeps going if a future resolved to an error until all futures have been resolved. In which case an error is returned.

The ordering of which value is yielded when two futures resolve simultaneously is intentionally left unspecified.

Examples

use async_std::prelude::*;
use async_std::future;
use std::io::{Error, ErrorKind};

let a = future::pending::<Result<_, Error>>();
let b = future::ready(Err(Error::from(ErrorKind::Other)));
let c = future::ready(Ok(1u8));

let f = a.try_race(b).try_race(c);
assert_eq!(f.await?, 1u8);
Loading content...

Implementors

impl<T: Future + ?Sized> FutureExt for T[src]

Loading content...