#[derive(Clone)]
pub struct CancellationToken {
inner: tokio_util::sync::CancellationToken,
}
impl CancellationToken {
pub fn new() -> Self {
Self {
inner: tokio_util::sync::CancellationToken::new(),
}
}
pub fn cancel(&self) {
self.inner.cancel();
}
pub fn is_cancelled(&self) -> bool {
self.inner.is_cancelled()
}
pub async fn cancelled(&self) {
self.inner.cancelled().await;
}
}
impl Default for CancellationToken {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn cancellation_propagation() {
let token = CancellationToken::new();
let clone = token.clone();
assert!(!token.is_cancelled());
assert!(!clone.is_cancelled());
token.cancel();
assert!(token.is_cancelled());
assert!(clone.is_cancelled());
}
}