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
impl CancelToken
Sourcepub fn with_timeout(limit: Duration) -> Self
pub fn with_timeout(limit: Duration) -> Self
A token that times out limit from now.
Sourcepub fn child(&self, limit: Option<Duration>) -> Self
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.
Sourcepub fn cancel(&self)
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.
Sourcepub fn run<R>(&self, work: impl FnOnce() -> R) -> Result<R, Cancelled>
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): withpanic = "abort", a cancellation aborts the process; - a
catch_unwindinsidework(one of yours, or a library’s) sees the cancellation as a panic: let a payload of typeCancelledgo on withstd::panic::resume_unwind; - a
runinsideworkmakes its own token current until it returns: give it achildof this one, so that cancelling this one stops it too; - threads that
workstarts 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
impl Clone for CancelToken
Source§impl Debug for CancelToken
impl Debug for CancelToken
Auto Trait Implementations§
impl Forget for CancelToken
impl Freeze for CancelToken
impl Move for CancelToken
impl RefUnwindSafe for CancelToken
impl Send for CancelToken
impl Sync for CancelToken
impl Unpin for CancelToken
impl UnsafeUnpin for CancelToken
impl UnwindSafe for CancelTokenwhere
Arc<Inner>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
Source§fn to_owned_obj(&self, data: FontData<'_>) -> U
fn to_owned_obj(&self, data: FontData<'_>) -> U
T, using the provided data to resolve any offsets.