pub struct DropHandle(/* private fields */);Expand description
A handle that aborts the task when dropped.
The task will only be aborted when the last DropHandle is dropped, so you can clone it to keep the task alive.
This is useful for tasks that should be automatically cleaned up when they are no longer needed, without having to manually call abort().
Example usage:
use drop_handle::DropHandle;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() {
let drop_handle: DropHandle = tokio::spawn(async {
loop {
println!("Task is running...");
sleep(Duration::from_secs(1)).await;
}
})
.into();
// The task will be automatically aborted when `drop_handle` goes out of scope.
}Methods from Deref<Target = AbortHandle>§
Sourcepub fn abort(&self)
pub fn abort(&self)
Abort the task associated with the handle.
Awaiting a cancelled task might complete as usual if the task was
already completed at the time it was cancelled, but most likely it
will fail with a cancelled JoinError.
If the task was already cancelled, such as by JoinHandle::abort,
this method will do nothing.
Be aware that tasks spawned using spawn_blocking cannot be aborted
because they are not async. If you call abort on a spawn_blocking
task, then this will not have any effect, and the task will continue
running normally. The exception is if the task has not started running
yet; in that case, calling abort may prevent the task from starting.
See also the module level docs for more information on cancellation.
Sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Checks if the task associated with this AbortHandle has finished.
Please note that this method can return false even if abort has been
called on the task. This is because the cancellation process may take
some time, and this method does not return true until it has
completed.
Trait Implementations§
Source§impl Clone for DropHandle
impl Clone for DropHandle
Source§fn clone(&self) -> DropHandle
fn clone(&self) -> DropHandle
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for DropHandle
impl Debug for DropHandle
Source§impl Deref for DropHandle
impl Deref for DropHandle
Source§type Target = AbortHandle
type Target = AbortHandle
Source§fn deref(&self) -> &AbortHandle
fn deref(&self) -> &AbortHandle
Source§impl Drop for DropHandle
When the last DropHandle is dropped, the task will be aborted.
impl Drop for DropHandle
When the last DropHandle is dropped, the task will be aborted.