use std::fmt;
#[allow(dead_code)]
#[derive(Debug)]
enum PrettyLock<T: fmt::Debug> {
Locked,
Unlocked { data: T, poisoned: bool },
Poisoned { data: T },
}
#[repr(transparent)]
pub struct AsPrettyRwLock<'this, T>(pub &'this std::sync::RwLock<T>);
impl<'this, T: fmt::Debug> fmt::Debug for AsPrettyRwLock<'this, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let val = match self.0.try_read() {
Ok(guard) => PrettyLock::Unlocked {
data: guard,
poisoned: false,
},
Err(std::sync::TryLockError::Poisoned(err)) => PrettyLock::Poisoned {
data: err.into_inner(),
},
Err(std::sync::TryLockError::WouldBlock) => PrettyLock::Locked,
};
#[allow(unused_qualifications)]
if std::mem::size_of::<T>() > 1 {
fmt::Debug::fmt(&val, f)
} else {
write!(f, "{val:?}")
}
}
}
#[repr(transparent)]
pub struct AsPrettyMutex<'this, T>(pub &'this std::sync::Mutex<T>);
impl<'this, T: fmt::Debug> fmt::Debug for AsPrettyMutex<'this, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let val = match self.0.try_lock() {
Ok(guard) => PrettyLock::Unlocked {
data: guard,
poisoned: false,
},
Err(std::sync::TryLockError::Poisoned(err)) => PrettyLock::Poisoned {
data: err.into_inner(),
},
Err(std::sync::TryLockError::WouldBlock) => PrettyLock::Locked,
};
#[allow(unused_qualifications)]
if std::mem::size_of::<T>() > 1 {
fmt::Debug::fmt(&val, f)
} else {
write!(f, "{val:?}")
}
}
}