[][src]Function task_scope::cancellation::cancellation

Important traits for Cancellation
pub fn cancellation() -> Cancellation

Creates a future that checks the current cancellation status.

The returned future resolves to:

  • Some(e) when the cancellation is in progress. The error object indicates whether the cancellation is graceful or forced.
  • None when the cancellation is not supported in the current context.

Notes

Normally, a task should not await the future, because the task will be blocked until someone issues a cancellation. Instead, try polling the cancellation future regularly:

use futures::{pin_mut, poll};
use task_scope::cancellation;
use std::task::Poll;

let cancellation = cancellation();
pin_mut!(cancellation);

loop {
    if let Poll::Ready(Some(e)) = poll!(cancellation.as_mut()) {
        // run cancellation logic

        return Err("canceled".into());
    }

    // normal operations
}

You can wrap Future/AsyncRead/AsyncWrite using cancelable function, which automatically stops the underlying operation on a cancellation.

The cancellation future should be stored in the tasks. Otherwise, the cancellation might not wake them up.