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.
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.
```rust use cancel::{Canceled, Token};
fn dosomething(token: Arc
// process more stuff here
}
Ok(true) }
fn cancelbuttonclicked(token: Arc
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
.
```rust use cancel::{Canceled, Token}; use std::time::Duration;
fn dosomething(token: &Token) -> Result
// process more stuff here
}
Ok(true) }
fn startsomething() -> Result