pub struct Token(/* private fields */);Expand description
A cooperative cancellation signal.
A token is cheap to clone and every clone observes the same cancellation.
Token::child_token derives a token that cancels when its parent does but
whose own cancellation stays local, which is how a Run scopes a tool batch’s
cancellation beneath its own.
Cancellation is one-way and idempotent: once cancelled, a token stays
cancelled and further Token::cancel calls change nothing.
use reef_core::cancel;
let run = cancel::Token::new();
let batch = run.child_token();
assert!(!batch.is_cancelled());
// Cancelling the child leaves the parent running.
batch.cancel();
assert!(batch.is_cancelled());
assert!(!run.is_cancelled());
// Cancelling the parent reaches every descendant.
let sibling = run.child_token();
run.cancel();
assert!(sibling.is_cancelled());Implementations§
Source§impl Token
impl Token
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Reports whether cancellation has been requested.
Sourcepub fn child_token(&self) -> Self
pub fn child_token(&self) -> Self
Derives a token cancelled by this one but not cancelling it.
Sourcepub fn cancelled(&self) -> impl Future<Output = ()> + Send + '_
pub fn cancelled(&self) -> impl Future<Output = ()> + Send + '_
Completes once cancellation has been requested.
The returned future is anonymous on purpose: naming a concrete future
type here would put the underlying implementation back into the public
surface that Token exists to keep it out of. Awaiting it is
cancel-safe — dropping the future deregisters its waker and observes
nothing.