Skip to main content

CancelToken

Struct CancelToken 

Source
pub struct CancelToken(/* private fields */);
Expand description

A request to stop a conversion, shared by the thread that runs it and those that may stop it (clones share the same request).

use kakemono::{CancelToken, Cancelled, Paper};
let token = CancelToken::new();
let stopper = token.clone();
// Elsewhere, e.g. when the user closes the window: stopper.cancel();
let analysis = token.run(|| -> anyhow::Result<_> {
    let paper = Paper::from_path("tests/data/1602.03837v1.pdf")?;
    Ok(paper.analyze())
});
assert_eq!(analysis.err(), Some(Cancelled::Requested));

Implementations§

Source§

impl CancelToken

Source

pub fn new() -> Self

A token with no time limit, not cancelled.

Source

pub fn with_timeout(limit: Duration) -> Self

A token that times out limit from now.

Source

pub fn child(&self, limit: Option<Duration>) -> Self

A token stopped by this one (by its cancellation or its deadline), and by its own: cancelling it does not cancel this one. limit is its own time limit, from now.

Source

pub fn cancel(&self)

Asks the work running with this token (and its children) to stop. It stops at its next stopping point, normally within a page.

Source

pub fn state(&self) -> Option<Cancelled>

Why the work must stop, or None while it may go on.

Source

pub fn run<R>(&self, work: impl FnOnce() -> R) -> Result<R, Cancelled>

Runs work on this thread with this token current, and returns its result, or why it stopped early. Work that ends normally keeps its result, even when the token was cancelled meanwhile; work is not started with a token already cancelled.

The work stops by unwinding its stack, as a panic does but without the panic hook, so:

  • the program must be built with panic = "unwind" (the default): with panic = "abort", a cancellation aborts the process;
  • a catch_unwind inside work (one of yours, or a library’s) sees the cancellation as a panic: let a payload of type Cancelled go on with std::panic::resume_unwind;
  • a run inside work makes its own token current until it returns: give it a child of this one, so that cancelling this one stops it too;
  • threads that work starts itself do not see the token: only the library’s own worker threads do.
§Errors

Cancelled when the token was cancelled, or timed out, before work ended.

§Panics

When work panics: the panic goes on.

Trait Implementations§

Source§

impl Clone for CancelToken

Source§

fn clone(&self) -> Self

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 CancelToken

Source§

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

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

impl Default for CancelToken

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Forget for CancelToken

§

impl Freeze for CancelToken
where *const ArcInner<Inner> is !null: Freeze, PhantomData<ArcInner<Inner>>: Freeze,

§

impl Move for CancelToken

§

impl RefUnwindSafe for CancelToken

§

impl Send for CancelToken
where Arc<Inner>: Send,

§

impl Sync for CancelToken
where Arc<Inner>: Sync,

§

impl Unpin for CancelToken
where Arc<Inner>: Unpin,

§

impl UnsafeUnpin for CancelToken
where *const ArcInner<Inner> is !null: UnsafeUnpin, PhantomData<ArcInner<Inner>>: UnsafeUnpin,

§

impl UnwindSafe for CancelToken
where Arc<Inner>: UnwindSafe,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Finish for T

Source§

fn finish(self)

Does nothing but move self, equivalent to drop.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

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<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

Source§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
Source§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

Source§

fn to_owned_table(&self) -> U

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V