use crate::runtime::task::{Header, RawTask};
use std::fmt;
use std::panic::{RefUnwindSafe, UnwindSafe};
#[cfg_attr(docsrs, doc(cfg(feature = "rt")))]
pub struct AbortHandle {
raw: RawTask,
}
impl AbortHandle {
pub(super) fn new(raw: RawTask) -> Self {
Self { raw }
}
pub fn abort(&self) {
self.raw.remote_abort();
}
pub fn is_finished(&self) -> bool {
let state = self.raw.state().load();
state.is_complete()
}
#[cfg(tokio_unstable)]
#[cfg_attr(docsrs, doc(cfg(tokio_unstable)))]
pub fn id(&self) -> super::Id {
unsafe { Header::get_id(self.raw.header_ptr()) }
}
}
unsafe impl Send for AbortHandle {}
unsafe impl Sync for AbortHandle {}
impl UnwindSafe for AbortHandle {}
impl RefUnwindSafe for AbortHandle {}
impl fmt::Debug for AbortHandle {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let id_ptr = unsafe { Header::get_id_ptr(self.raw.header_ptr()) };
let id = unsafe { id_ptr.as_ref() };
fmt.debug_struct("AbortHandle").field("id", id).finish()
}
}
impl Drop for AbortHandle {
fn drop(&mut self) {
self.raw.drop_abort_handle();
}
}