| Crates.io | async-cancellation-token |
| lib.rs | async-cancellation-token |
| version | 0.1.4 |
| created_at | 2025-08-27 03:43:00.807793+00 |
| updated_at | 2025-12-23 03:38:56.068856+00 |
| description | A lightweight single-threaded Rust library for cancellation tokens, enabling cooperative cancellation of asynchronous tasks and callbacks. |
| homepage | |
| repository | https://github.com/fxdmhtt/async-cancellation-token |
| max_upload_size | |
| id | 1812168 |
| size | 44,941 |
async-cancellation-token is a lightweight single-threaded Rust library that provides cancellation tokens for cooperative cancellation of asynchronous tasks and callbacks.
It is designed for !Send / single-threaded async environments (e.g., futures::executor::LocalPool).
CancellationTokenSource to control cancellation.CancellationTokens to be passed to tasks.token.cancelled().await completes when the token is cancelled.Cloneable and works with Rc/Cell/RefCell.⚠️ This crate is not thread-safe. Use only in single-threaded async contexts.
Add this to your Cargo.toml:
[dependencies]
async-cancellation-token = "0.1"
use async_cancellation_token::{CancellationTokenSource, Cancelled};
use futures::{executor::LocalPool, pin_mut, select, FutureExt};
use futures_timer::Delay;
use std::time::Duration;
let cts = CancellationTokenSource::new();
let token = cts.token();
let mut pool = LocalPool::new();
let spawner = pool.spawner();
spawner.spawn_local(async move {
for i in 1..=10 {
let delay = Delay::new(Duration::from_millis(200)).fuse();
let cancelled = token.cancelled().fuse();
pin_mut!(delay, cancelled);
select! {
_ = delay => println!("Step {i}"),
_ = cancelled => {
println!("Cancelled!");
break;
}
}
}
}.map(|_| ())).unwrap();
// Cancel after 1 second
spawner.spawn_local(async move {
Delay::new(Duration::from_secs(1)).await;
cts.cancel();
}.map(|_| ())).unwrap();
pool.run();
Apache-2.0