Skip to content

Commit ff33c3a

Browse files
committed
Add a Debug derive implementation to all structs.
1 parent f65b6f4 commit ff33c3a

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "async-cancellation-token"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
edition = "2024"
55
authors = ["Junkang Yuan <yuanjunkang@gmail.com>"]
66
description = "A lightweight single-threaded Rust library for cancellation tokens, enabling cooperative cancellation of asynchronous tasks and callbacks."

src/lib.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
use std::{
5555
cell::{Cell, RefCell},
5656
error::Error,
57-
fmt::Display,
57+
fmt::{Debug, Display},
5858
future::Future,
5959
pin::Pin,
6060
rc::{Rc, Weak},
@@ -82,6 +82,16 @@ struct Inner {
8282
callbacks: RefCell<Slab<Box<dyn FnOnce()>>>,
8383
}
8484

85+
impl Debug for Inner {
86+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87+
f.debug_struct("Inner")
88+
.field("cancelled", &self.cancelled)
89+
.field("wakers.len", &self.wakers.borrow().len())
90+
.field("callbacks.len", &self.callbacks.borrow().len())
91+
.finish()
92+
}
93+
}
94+
8595
/// A source that can cancel associated `CancellationToken`s.
8696
///
8797
/// Cancellation is **cooperative** and single-threaded. When cancelled:
@@ -100,7 +110,7 @@ struct Inner {
100110
/// cts.cancel();
101111
/// assert!(cts.is_cancelled());
102112
/// ```
103-
#[derive(Default, Clone)]
113+
#[derive(Debug, Default, Clone)]
104114
pub struct CancellationTokenSource {
105115
inner: Rc<Inner>,
106116
}
@@ -125,13 +135,13 @@ pub struct CancellationTokenSource {
125135
/// cts.cancel();
126136
/// pool.run();
127137
/// ```
128-
#[derive(Clone)]
138+
#[derive(Debug, Clone)]
129139
pub struct CancellationToken {
130140
inner: Rc<Inner>,
131141
}
132142

133143
/// Error returned when a cancelled token is checked synchronously.
134-
#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Hash)]
144+
#[derive(Debug, Copy, Clone, Default, Eq, Ord, PartialEq, PartialOrd, Hash)]
135145
pub struct Cancelled;
136146

137147
impl Display for Cancelled {
@@ -277,6 +287,7 @@ impl CancellationToken {
277287
/// This ensures that callbacks are **only called once** and resources are cleaned up.
278288
///
279289
/// **Single-threaded only.** Not safe to use concurrently.
290+
#[derive(Debug)]
280291
pub struct CancellationTokenRegistration {
281292
inner: Weak<Inner>,
282293
key: usize,
@@ -301,6 +312,7 @@ impl Drop for CancellationTokenRegistration {
301312
///
302313
/// **Single-threaded only.** Not Send or Sync.
303314
/// The future will be woken exactly once when the token is cancelled.
315+
#[derive(Debug)]
304316
pub struct CancelledFuture {
305317
token: CancellationToken,
306318
}

0 commit comments

Comments
 (0)