Skip to main content

CancellationToken

Struct CancellationToken 

Source
pub struct CancellationToken { /* private fields */ }
Expand description

A cheap, clonable handle used to request cooperative cancellation of a run.

Cloning a token is O(1) (an Arc bump) and every clone observes the same underlying state: cancelling any clone cancels them all. Cancellation is latching — once requested it can never be undone — so a token only ever transitions from “live” to “cancelled”.

Construct a fresh, never-cancelled token with CancellationToken::new (or Default). Attach one to a run via crate::harness::context::RunContext::with_cancellation; the default RunContext carries a fresh token that is never cancelled, so cancellation is strictly opt-in.

§Example

use tinyagents::harness::cancel::CancellationToken;

let token = CancellationToken::new();
assert!(!token.is_cancelled());

let clone = token.clone();
clone.cancel();
assert!(token.is_cancelled()); // visible through every clone

Implementations§

Source§

impl CancellationToken

Source

pub fn new() -> Self

Creates a fresh token that has not been cancelled.

Source

pub fn cancel(&self)

Requests cancellation.

This latches the token into the cancelled state (it can never be undone) and wakes every task currently awaiting CancellationToken::cancelled. Calling cancel more than once is harmless and idempotent. Because every clone shares one state, this cancels the token observed through all clones.

Source

pub fn is_cancelled(&self) -> bool

Returns true once CancellationToken::cancel has been called on this token or any of its clones.

Source

pub async fn cancelled(&self)

Resolves as soon as the token is (or becomes) cancelled.

If the token is already cancelled the returned future completes immediately. Otherwise it parks until CancellationToken::cancel is called. The implementation registers its interest with the underlying Notify before re-checking the flag, closing the race where a cancel lands between the initial check and parking.

This future is cancel-safe and may be used in a select! arm; dropping it simply deregisters the waiter.

Trait Implementations§

Source§

impl Clone for CancellationToken

Source§

fn clone(&self) -> CancellationToken

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CancellationToken

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CancellationToken

A Default CancellationToken is a fresh, never-cancelled token.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.