Skip to content

Commit bec2347

Browse files
committed
[tokio-util] AbortOnDropHandle::spawn shortcut
I believe this is 99% of `AbortOnDropHandle` use cases.
1 parent 925c614 commit bec2347

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

tokio-util/src/task/abort_on_drop.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ impl<T> AbortOnDropHandle<T> {
3030
Self(handle)
3131
}
3232

33+
/// Shortcut for [`tokio::spawn`] followed by [`Self::new`].
34+
///
35+
/// # Examples
36+
///
37+
/// ```
38+
/// # use tokio_util::task::AbortOnDropHandle;
39+
/// # #[tokio::main]
40+
/// # async fn main() {
41+
/// let _handle = AbortOnDropHandle::spawn(async move {
42+
/// // ...
43+
/// });
44+
/// # }
45+
/// ```
46+
#[track_caller]
47+
pub fn spawn<F>(future: F) -> Self
48+
where
49+
F: Future<Output = T> + Send + 'static,
50+
F::Output: Send + 'static,
51+
{
52+
Self::new(tokio::spawn(future))
53+
}
54+
3355
/// Abort the task associated with this handle,
3456
/// equivalent to [`JoinHandle::abort`].
3557
pub fn abort(&self) {

0 commit comments

Comments
 (0)