#graceful-shutdown #cancel #shutdown-signal

cancellation-token

A Rust implementation of C#'s CancellationToken API

2 releases

0.1.1 Jul 23, 2024
0.1.0 Aug 20, 2023

#1329 in Asynchronous

Download history 1254/week @ 2025-11-10 1010/week @ 2025-11-17 769/week @ 2025-11-24 753/week @ 2025-12-01 1032/week @ 2025-12-08 698/week @ 2025-12-15 222/week @ 2025-12-22 321/week @ 2025-12-29 581/week @ 2026-01-05 911/week @ 2026-01-12 1084/week @ 2026-01-19 846/week @ 2026-01-26 983/week @ 2026-02-02 1395/week @ 2026-02-09 19/week @ 2026-02-16 9/week @ 2026-02-23

2,416 downloads per month

MIT/Apache

18KB
341 lines

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.

Dependencies

~210–610KB
~14K SLoC