drop-guard.rs/src/lib.rs

47 lines
873 B
Rust

pub struct DropGuard<T: DropGuarded> {
inner: Option<T>,
}
impl<T: DropGuarded> DropGuard<T> {
#[inline]
pub fn unguard(mut self) -> T {
self.inner.take().expect("only None in drop")
}
}
pub trait DropGuarded {
fn cancel(self);
}
impl<T: DropGuarded> DropGuard<T> {
#[inline]
pub const fn new(guarded: T) -> Self {
Self {
inner: Some(guarded),
}
}
}
impl<T: DropGuarded> Drop for DropGuard<T> {
#[inline]
fn drop(&mut self) {
if let Some(inner) = self.inner.take() {
inner.cancel();
}
}
}
#[cfg(feature = "arbitrary")]
mod impl_arbitrary;
#[cfg(feature = "arbitrary")]
pub use impl_arbitrary::ArbitraryDropGuard;
#[cfg(feature = "async-std")]
mod impl_async_std;
#[cfg(feature = "tokio1")]
mod impl_tokio1;
#[cfg(feature = "triggered")]
mod impl_triggered;