drop-guard.rs/src/impl_tokio1.rs

46 lines
1.1 KiB
Rust

#[cfg(feature = "tokio1-task")]
mod impl_task {
use crate::{DropGuard, DropGuarded};
use tokio1::task::{JoinError, JoinHandle};
impl<T> DropGuarded for JoinHandle<T> {
#[inline]
fn cancel(self) {
self.abort();
}
}
#[cfg(feature = "tokio1-task")]
impl<T> core::future::Future for DropGuard<JoinHandle<T>> {
type Output = Result<T, JoinError>;
fn poll(
mut self: core::pin::Pin<&mut Self>,
cx: &mut core::task::Context<'_>,
) -> core::task::Poll<Self::Output> {
let handle = (*self).inner.as_mut().expect("can only be None in drop");
core::pin::Pin::new(handle).poll(cx)
}
}
}
#[cfg(feature = "tokio1-sync")]
mod impl_sync {
use crate::DropGuarded;
impl DropGuarded for tokio1::sync::oneshot::Sender<()> {
#[inline]
fn cancel(self) {
let _ = self.send(());
}
}
#[cfg(feature = "std")]
impl DropGuarded for std::sync::Arc<tokio1::sync::Semaphore> {
#[inline]
fn cancel(self) {
self.close();
}
}
}