pub struct CancellationToken { /* private fields */ }Expand description
A shared, one-shot signal that a request was aborted.
Cloning shares the signal. cancel is one-shot by
construction: the reason is stored in a OnceLock, so the first caller
wins and later ones observe the same reason. That matters because a
deadline and a peer’s cancellation notification can land at the same
instant, and whichever the request actually acted on is the one a
post-mortem needs to see.
Implementations§
Source§impl CancellationToken
impl CancellationToken
Sourcepub fn cancel(&self, reason: AbortReason) -> bool
pub fn cancel(&self, reason: AbortReason) -> bool
Aborts, unless something already has.
Wakes every waiter currently registered. Returns whether this call is the one that published the reason, which is what tells a caller racing a deadline against a peer’s notification which of the two the request actually acted on.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Whether the request was aborted.
Sourcepub fn reason(&self) -> Option<&AbortReason>
pub fn reason(&self) -> Option<&AbortReason>
Why it was aborted, if it was.
Sourcepub const fn cancelled(&self) -> Cancelled<'_> ⓘ
pub const fn cancelled(&self) -> Cancelled<'_> ⓘ
Resolves once the request is aborted.
Never resolves before that, and never resolves twice.
§Examples
use protocol_droid::domain::session::{AbortReason, CancellationToken};
let token = CancellationToken::new();
let observer = token.clone();
let signal = token.cancelled();
token.cancel(AbortReason::Expired);
signal.await;
assert!(observer.is_cancelled());