Skip to main content

aimux_core/
abort_signal.rs

1//! Cooperative caller cancellation for Core operations.
2
3use tokio_util::sync::CancellationToken;
4
5/// A cancellation signal analogous to the Web `AbortSignal`.
6///
7/// Timeouts are deliberately not encoded as cancellation reasons. Core owns
8/// timeout deadlines and returns [`crate::AiMuxError::Timeout`] directly;
9/// this type represents only caller-requested cancellation.
10#[derive(Debug, Clone, Default)]
11pub struct AbortSignal {
12    token: CancellationToken,
13}
14
15impl AbortSignal {
16    /// Create a fresh signal.
17    #[must_use]
18    pub fn new() -> Self {
19        Self::default()
20    }
21
22    /// Cancel with the default reason.
23    pub fn abort(&self) {
24        self.token.cancel();
25    }
26
27    /// Whether cancellation has been requested.
28    #[must_use]
29    pub fn is_aborted(&self) -> bool {
30        self.token.is_cancelled()
31    }
32
33    /// Resolve when cancellation is requested.
34    pub fn cancelled(&self) -> impl std::future::Future<Output = ()> + Send + 'static {
35        self.token.clone().cancelled_owned()
36    }
37}