[go: up one dir, main page]

Expand description

Asynchronous values.

Cancellation

Futures can be cancelled by dropping them before they finish executing. This is useful when we’re no longer interested in the result of an operation, as it allows us to stop doing needless work. This also means that a future may cancel at any .await point, and so just like with ? we have to be careful to roll back local state if our future halts there.

In order to perform a cancellation remotely, you can use the cancel function to create a cancel sender/receiver pair. When the sender side of this pair is dropped, all receivers resolve. This can be passed to Future::timeout or Stream::timeout to perform a cancellation.

use futures_time::prelude::*;
use futures_time::future::cancel;
use futures_time::time::Duration;

fn main() {
    async_io::block_on(async {
        let (send, recv) = cancel(); // create a new send/receive pair
        let mut counter = 0;
        let value = async { "meow" }
            .delay(Duration::from_millis(100))
            .timeout(recv) // time-out if the sender is dropped.
            .await;

        assert_eq!(value.unwrap(), "meow");
    })
}

Structs

The receiving side of a cancel operation.

The sending side of a cancel operation.

A future that delays execution by the specified time.

A future that times out after a duration of time.

Traits

A future which holds a deadline relative to now.

Extend Future with time-based operations.

Conversion into a Future.

Functions

Cancel futures remotely.