diff --git a/tokio-util/src/task/abort_on_drop.rs b/tokio-util/src/task/abort_on_drop.rs index e0353ac85c8..9304b701fad 100644 --- a/tokio-util/src/task/abort_on_drop.rs +++ b/tokio-util/src/task/abort_on_drop.rs @@ -30,6 +30,28 @@ impl AbortOnDropHandle { Self(handle) } + /// Shortcut for [`tokio::spawn`] followed by [`Self::new`]. + /// + /// # Examples + /// + /// ``` + /// # use tokio_util::task::AbortOnDropHandle; + /// # #[tokio::main] + /// # async fn main() { + /// let _handle = AbortOnDropHandle::spawn(async move { + /// // ... + /// }); + /// # } + /// ``` + #[track_caller] + pub fn spawn(future: F) -> Self + where + F: Future + Send + 'static, + F::Output: Send + 'static, + { + Self::new(tokio::spawn(future)) + } + /// Abort the task associated with this handle, /// equivalent to [`JoinHandle::abort`]. pub fn abort(&self) {