pub struct CancellationToken { /* private fields */ }Expand description
A thread-safe cancellation token that can be shared across threads.
This token allows cooperative cancellation by providing a way to signal cancellation and check cancellation status. The token can be cloned and shared across multiple threads, with all clones sharing the same cancellation state.
§Example
use reth_cli_util::cancellation::CancellationToken;
use std::{thread, time::Duration};
let token = CancellationToken::new();
let worker_token = token.clone();
let handle = thread::spawn(move || {
while !worker_token.is_cancelled() {
// Do work...
thread::sleep(Duration::from_millis(100));
}
});
// Cancel from main thread
token.cancel();
handle.join().unwrap();Implementations§
Source§impl CancellationToken
impl CancellationToken
Sourcepub fn cancel(&self)
pub fn cancel(&self)
Signals cancellation to all holders of this token and its clones.
Once cancelled, the token cannot be reset. This operation is thread-safe and can be called multiple times without issue.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Checks whether cancellation has been requested.
Returns true if cancel has been called on this token
or any of its clones.
Sourcepub fn drop_guard(&self) -> CancellationGuard
pub fn drop_guard(&self) -> CancellationGuard
Creates a guard that automatically cancels this token when dropped.
This is useful for ensuring cancellation happens when a scope exits, either normally or via panic.
§Example
use reth_cli_util::cancellation::CancellationToken;
let token = CancellationToken::new();
{
let _guard = token.drop_guard();
assert!(!token.is_cancelled());
// Guard dropped here, triggering cancellation
}
assert!(token.is_cancelled());Trait Implementations§
Source§impl Clone for CancellationToken
impl Clone for CancellationToken
Source§fn clone(&self) -> CancellationToken
fn clone(&self) -> CancellationToken
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for CancellationToken
impl Debug for CancellationToken
Auto Trait Implementations§
impl Freeze for CancellationToken
impl RefUnwindSafe for CancellationToken
impl Send for CancellationToken
impl Sync for CancellationToken
impl Unpin for CancellationToken
impl UnwindSafe for CancellationToken
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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 moreLayout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 8 bytes