async-std support and refactoring
This commit is contained in:
parent
d39c021aa6
commit
e989f04651
14
Cargo.toml
14
Cargo.toml
|
@ -5,12 +5,20 @@ edition = "2021"
|
|||
license = "MIT OR Apache-2.0"
|
||||
|
||||
[features]
|
||||
# keeping this in default to maintain semver compatibility
|
||||
default = [ "arbitrary" ]
|
||||
|
||||
arbitrary = [ ]
|
||||
|
||||
std = [ ]
|
||||
|
||||
async-std = [ "dep:async-std", "async-std/alloc", "async-std/default" ]
|
||||
tokio1 = [ "dep:tokio1" ]
|
||||
tokio1-sync = [ "tokio1", "tokio1/sync" ]
|
||||
tokio1-task = [ "tokio1", "tokio1/rt", "dep:futures-lite" ]
|
||||
tokio1-task = [ "tokio1", "tokio1/rt" ]
|
||||
triggered = [ "dep:triggered" ]
|
||||
|
||||
[dependencies]
|
||||
futures-lite = { version = "1.12", default-features = false, optional = true }
|
||||
async-std = { version = "1", default-features = false, optional = true }
|
||||
tokio1 = { package = "tokio", version = "1", default-features = false, optional = true }
|
||||
triggered = { version = "0.1.2", optional = true }
|
||||
triggered = { version = "0.1.2", default-features = false, optional = true }
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
use crate::DropGuarded;
|
||||
|
||||
pub struct ArbitraryDropGuard<F: FnOnce() -> ()>(Option<F>);
|
||||
|
||||
impl<F: FnOnce() -> ()> ArbitraryDropGuard<F> {
|
||||
#[inline]
|
||||
pub const fn new(f: F) -> Self {
|
||||
Self(Some(f))
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: FnOnce() -> ()> DropGuarded for ArbitraryDropGuard<F> {
|
||||
fn cancel(mut self) {
|
||||
if let Some(f) = self.0.take() {
|
||||
f();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
use async_std::task::JoinHandle;
|
||||
|
||||
use crate::{DropGuard, DropGuarded};
|
||||
|
||||
impl<T> DropGuarded for JoinHandle<T> {
|
||||
#[inline]
|
||||
fn cancel(self) {
|
||||
let _ = JoinHandle::<T>::cancel(self);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> core::future::Future for DropGuard<JoinHandle<T>> {
|
||||
type Output = T;
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
use crate::DropGuarded;
|
||||
|
||||
#[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")]
|
||||
impl DropGuarded for tokio1::sync::oneshot::Sender<()> {
|
||||
#[inline]
|
||||
fn cancel(self) {
|
||||
let _ = self.send(());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "tokio1-sync", feature = "std"))]
|
||||
impl DropGuarded for std::sync::Arc<tokio1::sync::Semaphore> {
|
||||
#[inline]
|
||||
fn cancel(self) {
|
||||
self.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
use crate::DropGuarded;
|
||||
|
||||
impl DropGuarded for triggered::Trigger {
|
||||
#[inline]
|
||||
fn cancel(self) {
|
||||
self.trigger();
|
||||
}
|
||||
}
|
76
src/lib.rs
76
src/lib.rs
|
@ -13,59 +13,9 @@ pub trait DropGuarded {
|
|||
fn cancel(self);
|
||||
}
|
||||
|
||||
#[cfg(feature = "tokio1-task")]
|
||||
impl<T> DropGuarded for tokio1::task::JoinHandle<T> {
|
||||
#[inline]
|
||||
fn cancel(self) {
|
||||
self.abort();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "tokio1-sync")]
|
||||
impl DropGuarded for tokio1::sync::oneshot::Sender<()> {
|
||||
#[inline]
|
||||
fn cancel(self) {
|
||||
let _ = self.send(());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "tokio1-sync")]
|
||||
impl DropGuarded for std::sync::Arc<tokio1::sync::Semaphore> {
|
||||
#[inline]
|
||||
fn cancel(self) {
|
||||
self.close();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "triggered")]
|
||||
impl DropGuarded for triggered::Trigger {
|
||||
#[inline]
|
||||
fn cancel(self) {
|
||||
self.trigger();
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(transparent)]
|
||||
pub struct ArbitraryDropGuard<F: FnOnce() -> ()>(Option<F>);
|
||||
|
||||
impl<F: FnOnce() -> ()> ArbitraryDropGuard<F> {
|
||||
#[inline]
|
||||
pub fn new(f: F) -> Self {
|
||||
Self(Some(f))
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: FnOnce() -> ()> DropGuarded for ArbitraryDropGuard<F> {
|
||||
fn cancel(mut self) {
|
||||
if let Some(f) = self.0.take() {
|
||||
f();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: DropGuarded> DropGuard<T> {
|
||||
#[inline]
|
||||
pub fn new(guarded: T) -> Self {
|
||||
pub const fn new(guarded: T) -> Self {
|
||||
Self {
|
||||
inner: Some(guarded),
|
||||
}
|
||||
|
@ -81,16 +31,16 @@ impl<T: DropGuarded> Drop for DropGuard<T> {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "tokio1-task")]
|
||||
impl<T> std::future::Future for DropGuard<tokio1::task::JoinHandle<T>> {
|
||||
type Output = Result<T, tokio1::task::JoinError>;
|
||||
#[cfg(feature = "arbitrary")]
|
||||
mod impl_arbitrary;
|
||||
#[cfg(feature = "arbitrary")]
|
||||
pub use impl_arbitrary::ArbitraryDropGuard;
|
||||
|
||||
fn poll(
|
||||
mut self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<Self::Output> {
|
||||
use futures_lite::future::FutureExt;
|
||||
let handle = (*self).inner.as_mut().expect("can only be None in drop");
|
||||
handle.poll(cx)
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "async-std")]
|
||||
mod impl_async_std;
|
||||
|
||||
#[cfg(feature = "tokio1")]
|
||||
mod impl_tokio1;
|
||||
|
||||
#[cfg(feature = "triggered")]
|
||||
mod impl_triggered;
|
||||
|
|
Loading…
Reference in New Issue