Skip to content

Commit c821118

Browse files
committed
feat: Implement Display and Error traits for Cancelled errors to enhance error handling capabilities
1 parent c3e183e commit c821118

3 files changed

Lines changed: 13 additions & 3 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.1"
3+
version = "0.1.2"
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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
4747
use std::{
4848
cell::{Cell, RefCell},
49+
error::Error,
50+
fmt::Display,
4951
future::Future,
5052
pin::Pin,
5153
rc::Rc,
@@ -108,9 +110,17 @@ pub struct CancellationToken {
108110
}
109111

110112
/// Error returned when a cancelled token is checked synchronously.
111-
#[derive(Debug)]
113+
#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Hash)]
112114
pub struct Cancelled;
113115

116+
impl Display for Cancelled {
117+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118+
f.write_str("cancelled by CancellationTokenSource")
119+
}
120+
}
121+
122+
impl Error for Cancelled {}
123+
114124
impl Default for CancellationTokenSource {
115125
fn default() -> Self {
116126
Self::new()

0 commit comments

Comments
 (0)