use std::fmt::{Debug, Display}; use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; use std::sync::atomic::Ordering::Relaxed; use crate::Keyable; use super::{PoisonFlag, PoisonGuard, PoisonRef}; impl<'a, Guard> PoisonRef<'a, Guard> { // This is used so that we don't keep accidentally adding the flag reference pub(super) const fn new(flag: &'a PoisonFlag, guard: Guard) -> Self { Self { guard, #[cfg(panic = "unwind")] flag, _phantom: PhantomData, } } } impl Drop for PoisonRef<'_, Guard> { fn drop(&mut self) { #[cfg(panic = "unwind")] if std::thread::panicking() { self.flag.0.store(true, Relaxed); } } } impl Debug for PoisonRef<'_, Guard> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Debug::fmt(&**self, f) } } impl Display for PoisonRef<'_, Guard> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Display::fmt(&**self, f) } } impl Deref for PoisonRef<'_, Guard> { type Target = Guard; fn deref(&self) -> &Self::Target { &self.guard } } impl DerefMut for PoisonRef<'_, Guard> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.guard } } impl AsRef for PoisonRef<'_, Guard> { fn as_ref(&self) -> &Guard { &self.guard } } impl AsMut for PoisonRef<'_, Guard> { fn as_mut(&mut self) -> &mut Guard { &mut self.guard } } impl Debug for PoisonGuard<'_, '_, Guard, Key> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Debug::fmt(&self.guard, f) } } impl Display for PoisonGuard<'_, '_, Guard, Key> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Display::fmt(&self.guard, f) } } impl, Key: Keyable> Deref for PoisonGuard<'_, '_, Guard, Key> { type Target = T; fn deref(&self) -> &Self::Target { #[allow(clippy::explicit_auto_deref)] // fixing this results in a compiler error &*self.guard.guard } } impl, Key: Keyable> DerefMut for PoisonGuard<'_, '_, Guard, Key> { fn deref_mut(&mut self) -> &mut Self::Target { #[allow(clippy::explicit_auto_deref)] // fixing this results in a compiler error &mut *self.guard.guard } } impl AsRef for PoisonGuard<'_, '_, Guard, Key> { fn as_ref(&self) -> &Guard { &self.guard.guard } } impl AsMut for PoisonGuard<'_, '_, Guard, Key> { fn as_mut(&mut self) -> &mut Guard { &mut self.guard.guard } }