pub struct WaitToken { /* private fields */ }Expand description
WaitToken (aka CancellationToken) is an object used for ‘cancellation’ events.
Example:
// Create a new token
let cancellation = WaitToken::new();
// Clone token to use it in another thread. You can call cancel on this object too!
let cancellation_clone = cancellation.clone();
tokio::spawn(async move {
// wait for token to cancel
cancellation_clone.await;
println!("Finished");
})
// Child tokens will cancel when parent is cancelled.
// 'cancel()' on child tokens will not cancel parent
let child = cancellation.make_child_token();
tokio::spawn(async move {
// Wait for token to cancel
child.await;
println!("Child is finished");
})
println!("Cancelled");
cancellation.cancel();
// This await is just for demonstration. No need to await after cancel
// Note that 'await' takes by-value so you may need to clone
cancellation.clone().await;
// Undo cancellation of token
cancellation.reset();
// This await will not return because token is not cancelled anymore
cancellation.clone().await;
// This is unreachable
println!("Something went wrong!");Implementations§
Source§impl WaitToken
impl WaitToken
Sourcepub fn spawn_terminator(&self) -> JoinHandle<Result<(), Error>>
pub fn spawn_terminator(&self) -> JoinHandle<Result<(), Error>>
Spawns background fetch to listem SIGTERM & SIGINT events and calls cancel
Sourcepub fn cancel(&self) -> CancelState
pub fn cancel(&self) -> CancelState
Set state to ‘cancelled’. Futures waiting for token will complete and streams terminated.
pub fn is_cancelled(&self) -> bool
pub fn cancelled(&self) -> WaitTokenCancelled ⓘ
Sourcepub fn until_cancel(&self) -> TakeUntil<Repeat<()>, WaitTokenCancelled>
pub fn until_cancel(&self) -> TakeUntil<Repeat<()>, WaitTokenCancelled>
Make a stream that will emit items until token is cancelled
pub fn on_cancel(&self) -> Once<WaitTokenCancelled>
pub async fn on_cancel_then<Fut: Future + Send>(self, fut: Fut) -> Fut::Output
Sourcepub fn repeat_until_cancel(&self, frq: Duration) -> WaitTokenRepeat<Self>
pub fn repeat_until_cancel(&self, frq: Duration) -> WaitTokenRepeat<Self>
Make an object that will wait for delay every iteration and return false if token is cancelled. Common usage is with while loop.
Example:
let mut repeater = cancellation.repeat_until_cancel(Duration::from_secs(1));
while repeater.next().await {
do_something();
}Sourcepub async fn run_fn<Fut: Future>(&self, fut: Fut) -> Option<Fut::Output>
pub async fn run_fn<Fut: Future>(&self, fut: Fut) -> Option<Fut::Output>
Run future. Future will be aborted when token is cancelled
Sourcepub fn make_child_token(&self) -> Self
pub fn make_child_token(&self) -> Self
Create a sub-token that will cancel if current token cancelled.
Cancel on child token will not cancel parent
Sourcepub fn guard(&self) -> WaitTokenGuard
pub fn guard(&self) -> WaitTokenGuard
Create a guard that will cancel wait token on drop
Trait Implementations§
Auto Trait Implementations§
impl Freeze for WaitToken
impl RefUnwindSafe for WaitToken
impl Send for WaitToken
impl Sync for WaitToken
impl Unpin for WaitToken
impl UnsafeUnpin for WaitToken
impl UnwindSafe for WaitToken
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
Mutably borrows from an owned value. Read more