cancellation-token-eagle

Crates.iocancellation-token-eagle
lib.rscancellation-token-eagle
version0.2.0
sourcesrc
created_at2024-07-23 03:36:33.55771
updated_at2024-07-23 03:39:17.683103
descriptionA Rust implementation of C#'s CancellationToken API.
homepage
repositoryhttps://github.com/bitcoin-eagle/cancellation-token-rs
max_upload_size
id1312350
size45,836
Bitcoin Eagle (bitcoin-eagle)

documentation

https://docs.rs/cancellation-token-eagle/

README

cancellation-token-rs

Crates.io Docs.rs Actions Status

A Rust implementation of C#'s CancellationToken API.

Example

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");
        }
    });
}

License

This project is dual licensed under Apache-2.0/MIT. See the two LICENSE-* files for details.

Contribution

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.

Commit count: 0

cargo fmt