Crates.io | cancellation-token |
lib.rs | cancellation-token |
version | 0.1.1 |
source | src |
created_at | 2023-08-20 13:19:59.790694 |
updated_at | 2024-07-23 14:49:06.156958 |
description | A Rust implementation of C#'s CancellationToken API. |
homepage | |
repository | https://github.com/frozenlib/cancellation-token-rs |
max_upload_size | |
id | 949380 |
size | 46,048 |
A Rust implementation of C#'s CancellationToken API.
use cancellation_token::{CancellationToken, CancellationTokenSource, MayBeCanceled};
fn cancelable_function(ct: &CancellationToken) -> MayBeCanceled<u32> {
for _ in 0..100 {
ct.canceled()?; // Return from this function if canceled
heavy_work();
}
Ok(100)
}
fn heavy_work() { }
fn main() {
let cts = CancellationTokenSource::new();
std::thread::scope(|s| {
s.spawn(|| {
std::thread::sleep(std::time::Duration::from_secs(2));
cts.cancel();
});
if cancelable_function(&cts.token()).is_err() {
println!("canceled");
}
});
}
This project is dual licensed under Apache-2.0/MIT. See the two LICENSE-* files for details.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.