task: add AbortOnDropHandle::detach (#7400)

This commit is contained in:
Geoffry Song
2025-06-10 09:35:48 +02:00
committed by GitHub
parent 714e5b571f
commit 912b862a05
2 changed files with 23 additions and 1 deletions
+10
View File
@@ -5,6 +5,7 @@ use tokio::task::{AbortHandle, JoinError, JoinHandle};
use std::{
future::Future,
mem::ManuallyDrop,
pin::Pin,
task::{Context, Poll},
};
@@ -46,6 +47,15 @@ impl<T> AbortOnDropHandle<T> {
pub fn abort_handle(&self) -> AbortHandle {
self.0.abort_handle()
}
/// Cancels aborting on drop and returns the original [`JoinHandle`].
pub fn detach(self) -> JoinHandle<T> {
// Avoid invoking `AbortOnDropHandle`'s `Drop` impl
let this = ManuallyDrop::new(self);
// SAFETY: `&this.0` is a reference, so it is certainly initialized, and
// it won't be double-dropped because it's in a `ManuallyDrop`
unsafe { std::ptr::read(&this.0) }
}
}
impl<T> Future for AbortOnDropHandle<T> {