Skip to main content

drop_handle/
lib.rs

1//! `DropHandle` is a handle that aborts the task when dropped.
2//!
3//! The task will only be aborted when the last `DropHandle` is dropped, so you can clone it to keep the task alive.
4//! This is useful for tasks that should be automatically cleaned up when they are no longer needed, without having to manually call `abort()`.
5//!
6//! Example usage:
7//! ```
8//! use drop_handle::DropHandle;
9//! use tokio::time::{sleep, Duration};
10//!
11//! #[tokio::main]
12//! async fn main() {
13//!     let drop_handle: DropHandle = tokio::spawn(async {
14//!         loop {
15//!             println!("Task is running...");
16//!             sleep(Duration::from_secs(1)).await;
17//!         }
18//!     })
19//!     .into();
20//!     // The task will be automatically aborted when `drop_handle` goes out of scope.
21//! }
22//! ```
23
24use std::{ops::Deref, sync::Arc};
25use tokio::task::{AbortHandle, JoinHandle};
26use tracing::debug;
27
28#[cfg(test)]
29mod tests;
30
31/// A handle that aborts the task when dropped.
32///
33/// The task will only be aborted when the last `DropHandle` is dropped, so you can clone it to keep the task alive.
34/// This is useful for tasks that should be automatically cleaned up when they are no longer needed, without having to manually call `abort()`.
35///
36/// Example usage:
37/// ```
38/// use drop_handle::DropHandle;
39/// use tokio::time::{sleep, Duration};
40///
41/// #[tokio::main]
42/// async fn main() {
43///     let drop_handle: DropHandle = tokio::spawn(async {
44///         loop {
45///             println!("Task is running...");
46///             sleep(Duration::from_secs(1)).await;
47///         }
48///     })
49///     .into();
50///     // The task will be automatically aborted when `drop_handle` goes out of scope.
51/// }
52/// ```
53#[derive(Clone, Debug)]
54pub struct DropHandle(Arc<DropHandleInner>);
55
56#[derive(Debug)]
57struct DropHandleInner(AbortHandle);
58
59impl Deref for DropHandleInner {
60    type Target = AbortHandle;
61
62    fn deref(&self) -> &AbortHandle {
63        &self.0
64    }
65}
66
67impl Drop for DropHandleInner {
68    fn drop(&mut self) {
69        debug!("drop final DropHandle: abort task {:?}", self.0.id());
70        self.0.abort();
71    }
72}
73
74impl Deref for DropHandle {
75    type Target = AbortHandle;
76
77    fn deref(&self) -> &AbortHandle {
78        &self.0.0
79    }
80}
81
82impl From<AbortHandle> for DropHandle {
83    fn from(value: AbortHandle) -> Self {
84        debug!("create DropHandle for task {:?}", value.id());
85        Self(Arc::new(DropHandleInner(value)))
86    }
87}
88
89impl<T> From<JoinHandle<T>> for DropHandle {
90    fn from(value: JoinHandle<T>) -> Self {
91        value.abort_handle().into()
92    }
93}