//! An [`AbortOnDropHandle`] is like a [`JoinHandle`], except that it //! will abort the task as soon as it is dropped.
use tokio::task::{AbortHandle, JoinError, JoinHandle};
use std::{
future::Future,
mem::ManuallyDrop,
pin::Pin,
task::{Context, Poll},
};
/// A wrapper around a [`tokio::task::JoinHandle`], /// which [aborts] the task when it is dropped. /// /// [aborts]: tokio::task::JoinHandle::abort #[must_use = "Dropping the handle aborts the task immediately"] pubstruct AbortOnDropHandle<T>(JoinHandle<T>);
impl<T> Drop for AbortOnDropHandle<T> { fn drop(&mutself) { self.0.abort()
}
}
impl<T> AbortOnDropHandle<T> { /// Create an [`AbortOnDropHandle`] from a [`JoinHandle`]. pubfn new(handle: JoinHandle<T>) -> Self { Self(handle)
}
/// Abort the task associated with this handle, /// equivalent to [`JoinHandle::abort`]. pubfn abort(&self) { self.0.abort()
}
/// Checks if the task associated with this handle is finished, /// equivalent to [`JoinHandle::is_finished`]. pubfn is_finished(&self) -> bool { self.0.is_finished()
}
/// Returns a new [`AbortHandle`] that can be used to remotely abort this task, /// equivalent to [`JoinHandle::abort_handle`]. pubfn abort_handle(&self) -> AbortHandle { self.0.abort_handle()
}
/// Cancels aborting on drop and returns the original [`JoinHandle`]. pubfn 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) }
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.