#![doc = include_str!("../README.md")]
#![allow(drop_bounds)]
#![allow(clippy::declare_interior_mutable_const)]
#![warn(missing_docs)]
#![no_std]
#[cfg(feature = "shrinkwraprs")]
#[macro_use]
extern crate shrinkwraprs;
#[cfg(feature = "std")]
extern crate std;
pub mod prelude;
pub mod drop_strategies;
mod infallible;
pub use anyhow::Error;
use core::sync::atomic::Ordering;
pub use infallible::Infallible;
#[cfg(any(feature = "global", feature = "thread-local"))]
mod global_crate_root;
#[cfg(any(feature = "global", feature = "thread-local"))]
pub use global_crate_root::*;
#[cfg(not(any(feature = "global", feature = "thread-local")))]
pub use self::PureTryDrop as TryDrop;
#[cfg(any(feature = "global", feature = "thread-local"))]
pub use self::ImpureTryDrop as TryDrop;
#[cfg(any(feature = "__tests", test))]
pub mod test_utils;
#[cfg(any(feature = "global", feature = "thread-local"))]
pub mod handlers;
pub mod adapters;
use adapters::DropAdapter;
#[allow(dead_code)]
const LOAD_ORDERING: Ordering = Ordering::Acquire;
#[allow(dead_code)]
const STORE_ORDERING: Ordering = Ordering::Release;
pub trait PureTryDrop {
type Error: Into<anyhow::Error>;
type FallbackTryDropStrategy: TryDropStrategy;
type TryDropStrategy: FallibleTryDropStrategy;
fn fallback_try_drop_strategy(&self) -> &Self::FallbackTryDropStrategy;
fn try_drop_strategy(&self) -> &Self::TryDropStrategy;
fn adapt(self) -> DropAdapter<Self>
where
Self: Sized,
{
DropAdapter(self)
}
unsafe fn try_drop(&mut self) -> Result<(), Self::Error>;
}
#[cfg(any(feature = "global", feature = "thread-local"))]
pub trait ImpureTryDrop {
type Error: Into<anyhow::Error>;
unsafe fn try_drop(&mut self) -> Result<(), Self::Error>;
}
pub trait FallibleTryDropStrategy {
type Error: Into<anyhow::Error>;
fn try_handle_error(&self, error: anyhow::Error) -> Result<(), Self::Error>;
}
pub trait DynFallibleTryDropStrategy {
fn dyn_try_handle_error(&self, error: anyhow::Error) -> anyhow::Result<()>;
}
impl<T: FallibleTryDropStrategy> DynFallibleTryDropStrategy for T {
fn dyn_try_handle_error(&self, error: anyhow::Error) -> anyhow::Result<()> {
self.try_handle_error(error).map_err(Into::into)
}
}
#[cfg(feature = "global")]
#[cfg(not(feature = "downcast-rs"))]
pub trait GlobalDynFallibleTryDropStrategy: ThreadSafe + DynFallibleTryDropStrategy {}
#[cfg(feature = "global")]
#[cfg(feature = "downcast-rs")]
pub trait GlobalDynFallibleTryDropStrategy:
ThreadSafe + downcast_rs::DowncastSync + DynFallibleTryDropStrategy
{
}
#[cfg(feature = "global")]
#[cfg(feature = "downcast-rs")]
downcast_rs::impl_downcast!(sync GlobalDynFallibleTryDropStrategy);
#[cfg(feature = "global")]
impl<T: ThreadSafe + DynFallibleTryDropStrategy> GlobalDynFallibleTryDropStrategy for T {}
#[cfg(feature = "thread-local")]
pub trait ThreadLocalFallibleTryDropStrategy: DynFallibleTryDropStrategy + 'static {}
#[cfg(feature = "thread-local")]
impl<T: DynFallibleTryDropStrategy + 'static> ThreadLocalFallibleTryDropStrategy for T {}
pub trait TryDropStrategy {
fn handle_error(&self, error: anyhow::Error);
}
impl<TDS: TryDropStrategy> FallibleTryDropStrategy for TDS {
type Error = Infallible;
fn try_handle_error(&self, error: anyhow::Error) -> Result<(), Self::Error> {
self.handle_error(error);
Ok(())
}
}
#[cfg(feature = "global")]
#[cfg(not(feature = "downcast-rs"))]
pub trait GlobalTryDropStrategy: ThreadSafe + TryDropStrategy {}
#[cfg(feature = "global")]
#[cfg(feature = "downcast-rs")]
pub trait GlobalTryDropStrategy: ThreadSafe + downcast_rs::DowncastSync + TryDropStrategy {}
#[cfg(feature = "global")]
#[cfg(feature = "downcast-rs")]
downcast_rs::impl_downcast!(sync GlobalTryDropStrategy);
#[cfg(feature = "global")]
impl<T: ThreadSafe + TryDropStrategy> GlobalTryDropStrategy for T {}
#[cfg(feature = "thread-local")]
pub trait ThreadLocalTryDropStrategy: TryDropStrategy + 'static {}
#[cfg(feature = "thread-local")]
impl<T: TryDropStrategy + 'static> ThreadLocalTryDropStrategy for T {}
pub trait ThreadSafe: Send + Sync + 'static {}
impl<T: Send + Sync + 'static> ThreadSafe for T {}
pub unsafe trait RepeatableTryDrop: PureTryDrop {
fn safe_try_drop(&mut self) -> Result<(), Self::Error> {
unsafe { self.try_drop() }
}
}