cancel 0.1.0

Simple co-operative cancellation
Documentation
  • Coverage
  • 100%
    10 out of 10 items documented1 out of 10 items with examples
  • Size
  • Source code size: 15.86 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.57 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • wez/cancel-rs
    5 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • wez

Co-operative Cancellation Tokens in Rust

Build Status

Documentation

This crate provides a simple token that can be used to co-operatively manage cancellation of an operation. Setting a deadline/timeout is also supported.

Usage

Explicit interactive cancellation

This one is a little awkward to show a complete working sketch, but the concept is simple: pass the token to a long running operation and have it check for cancellation every so often.

Then you can wire up a button click or CTRL-C so that it calls token.cancel().

Note that the implementation of Token::cancel() is a simple atomic operation and is async signal safe.

use cancel::{Canceled, Token};

fn do_something(token: Arc<Token>) -> Result<bool, Canceled> {
  while !done {
    token.check_cancel()?;

    // process more stuff here
  }

  Ok(true)
}

fn cancel_button_clicked(token: Arc<Token>) {
  token.cancel();
}

Simple timeout management

In this scenario the token has been configured with a deadline. The deadline is co-operatively checked by the do_something function when it calls check_cancel.

use cancel::{Canceled, Token};
use std::time::Duration;

fn do_something(token: &Token) -> Result<bool, Canceled> {
  while !done {
    token.check_cancel()?;

    // process more stuff here
  }

  Ok(true)
}

fn start_something() -> Result<bool, Canceled> {
  let token = Token::with_duration(Duration::new(10, 0));
  do_something(&token)
}