pub trait CancellationToken {
// Required methods
fn error_if_cancelled(&self) -> Result<(), CancellationError>;
fn on_cancellation<'a>(
&self,
func: &'a mut CancellationFunc<'a>,
) -> CancellationGuard<'a>;
}Expand description
A CancellationToken provides information whether a flow of execution is expected
to be cancelled.
There are 2 ways to interact with CancellationToken:
- The token can be queried on whether the flow is cancelled
- A callback can be registered if thhe flow is cancelled.
Required Methods§
Sourcefn error_if_cancelled(&self) -> Result<(), CancellationError>
fn error_if_cancelled(&self) -> Result<(), CancellationError>
Performs a one-time check whether the flow of execution is cancelled.
Returns an error if cancellation is initiated
Sourcefn on_cancellation<'a>(
&self,
func: &'a mut CancellationFunc<'a>,
) -> CancellationGuard<'a>
fn on_cancellation<'a>( &self, func: &'a mut CancellationFunc<'a>, ) -> CancellationGuard<'a>
Registers a cancellation handler, which will be invoked when the execution flow is cancelled. The cancellation handler can be called from any thread which initiates the cancellation. If the flow is already cancelled when this function is called, the cancellation handler will be called synchronously. The function returns a guard which can be used to unregister the cancellation handler. After the guard is dropped, the handler is guaranteed not be called anymore.