Skip to main content

Crate drop_handle

Crate drop_handle 

Source
Expand description

DropHandle is 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.
}

Structsยง

DropHandle
A handle that aborts the task when dropped.