Split async-std support and refactor

This commit is contained in:
Michael Pfaff 2022-06-15 16:47:49 -04:00
parent cb7b6446de
commit ba59933810
Signed by: michael
GPG Key ID: CF402C4A012AA9D4
3 changed files with 37 additions and 32 deletions

View File

@ -12,7 +12,8 @@ arbitrary = [ ]
std = [ ] std = [ ]
async-std = [ "dep:async-std", "async-std/alloc", "async-std/default" ] async-std = [ "dep:async-std" ]
async-std-task = [ "async-std", "async-std/alloc", "async-std/default" ]
tokio1 = [ "dep:tokio1" ] tokio1 = [ "dep:tokio1" ]
tokio1-sync = [ "tokio1", "tokio1/sync" ] tokio1-sync = [ "tokio1", "tokio1/sync" ]
tokio1-task = [ "tokio1", "tokio1/rt" ] tokio1-task = [ "tokio1", "tokio1/rt" ]

View File

@ -1,22 +1,25 @@
use async_std::task::JoinHandle; #[cfg(feature = "async-std-task")]
mod impl_task {
use async_std::task::JoinHandle;
use crate::{DropGuard, DropGuarded}; use crate::{DropGuard, DropGuarded};
impl<T> DropGuarded for JoinHandle<T> { impl<T> DropGuarded for JoinHandle<T> {
#[inline] #[inline]
fn cancel(self) { fn cancel(self) {
let _ = JoinHandle::<T>::cancel(self); let _ = JoinHandle::<T>::cancel(self);
} }
} }
impl<T> core::future::Future for DropGuard<JoinHandle<T>> { impl<T> core::future::Future for DropGuard<JoinHandle<T>> {
type Output = T; type Output = T;
fn poll( fn poll(
mut self: core::pin::Pin<&mut Self>, mut self: core::pin::Pin<&mut Self>,
cx: &mut core::task::Context<'_>, cx: &mut core::task::Context<'_>,
) -> core::task::Poll<Self::Output> { ) -> core::task::Poll<Self::Output> {
let handle = (*self).inner.as_mut().expect("can only be None in drop"); let handle = (*self).inner.as_mut().expect("can only be None in drop");
core::pin::Pin::new(handle).poll(cx) core::pin::Pin::new(handle).poll(cx)
}
} }
} }

View File

@ -1,5 +1,3 @@
use crate::DropGuarded;
#[cfg(feature = "tokio1-task")] #[cfg(feature = "tokio1-task")]
mod impl_task { mod impl_task {
use crate::{DropGuard, DropGuarded}; use crate::{DropGuard, DropGuarded};
@ -28,17 +26,20 @@ mod impl_task {
} }
#[cfg(feature = "tokio1-sync")] #[cfg(feature = "tokio1-sync")]
impl DropGuarded for tokio1::sync::oneshot::Sender<()> { mod impl_sync {
#[inline] use crate::DropGuarded;
fn cancel(self) {
let _ = self.send(());
}
}
#[cfg(all(feature = "tokio1-sync", feature = "std"))] impl DropGuarded for tokio1::sync::oneshot::Sender<()> {
impl DropGuarded for std::sync::Arc<tokio1::sync::Semaphore> { #[inline]
#[inline] fn cancel(self) {
fn cancel(self) { let _ = self.send(());
self.close(); }
}
#[cfg(feature = "std")]
impl DropGuarded for std::sync::Arc<tokio1::sync::Semaphore> {
#[inline]
fn cancel(self) {
self.close();
}
} }
} }