#[cfg(feature = "tokio1-task")] pub use join_handle_ext::*; #[cfg(feature = "tokio1-sync")] pub use sender_ext::*; #[cfg(all(feature = "tokio1-sync", feature = "std"))] pub use semaphore_ext::*; #[cfg(feature = "tokio1-task")] mod join_handle_ext { use tokio1::task::{JoinError, JoinHandle}; use crate::DropFn; crate::macros::join_handle_ext!(JoinHandleExt for JoinHandle, AbortOnDrop, abort, |T| Result); } #[cfg(feature = "tokio1-sync")] mod sender_ext { use tokio1::sync::oneshot::Sender; use crate::{DropFn, DropGuard}; pub struct SendOnDrop(T); impl DropFn for SendOnDrop { type Data = Sender; fn on_drop(self, data: Self::Data) { _ = data.send(self.0); } } trait Sealed {} pub trait SenderExt { type Value; /// Wraps the sender in a [`DropGuard`] that will [`send`](Sender::send) the given `value` on drop. fn send_on_drop(self, value: Self::Value) -> DropGuard>; } impl Sealed for Sender {} impl SenderExt for Sender { type Value = T; fn send_on_drop(self, value: T) -> DropGuard> { DropGuard::with_data(SendOnDrop(value), self) } } } #[cfg(all(feature = "tokio1-sync", feature = "std"))] mod semaphore_ext { use std::marker::PhantomData; use std::ops::Deref; use tokio1::sync::Semaphore; use crate::{DropFn, DropGuard}; pub struct CloseOnDrop(PhantomData T>); impl DropFn for CloseOnDrop where T: Deref, { type Data = T; fn on_drop(self, data: Self::Data) { data.close(); } } trait Sealed {} pub trait SemaphoreExt: Sized + Deref { /// Wraps the semaphore in a [`DropGuard`] that will call [`Semaphore::close`] on drop. fn close_on_drop(self) -> DropGuard>; } impl Sealed for T where T: Deref {} impl SemaphoreExt for T where T: Deref, { fn close_on_drop(self) -> DropGuard> { DropGuard::with_data(CloseOnDrop(PhantomData), self) } } }