diff --git a/src/sync.rs b/src/sync.rs index a551b9a..467ce05 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -11,10 +11,12 @@ use std::alloc::Layout; use std::borrow::Borrow; use std::collections::BTreeMap; use std::collections::HashMap; +use std::fmt; use std::hash::Hash; use std::iter::{FromIterator, IntoIterator}; use std::ops::Index; +use std::sync::TryLockError; use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicPtr; use std::sync::atomic::AtomicUsize; @@ -27,6 +29,30 @@ pub struct FrozenMap { map: RwLock>, } +impl fmt::Debug for FrozenMap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut d = f.debug_struct("FrozenMap"); + match self.map.try_read() { + Ok(guard) => { + d.field("map", &&*guard); + }, + Err(TryLockError::Poisoned(err)) => { + d.field("map", &&**err.get_ref()); + } + Err(TryLockError::WouldBlock) => { + struct LockedPlaceholder; + impl fmt::Debug for LockedPlaceholder { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("") + } + } + d.field("map", &LockedPlaceholder); + }, + } + d.finish_non_exhaustive() + } +} + impl Default for FrozenMap { fn default() -> Self { Self { @@ -387,6 +413,30 @@ pub struct FrozenVec { vec: RwLock>, } +impl fmt::Debug for FrozenVec { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut d = f.debug_struct("FrozenVec"); + match self.vec.try_read() { + Ok(guard) => { + d.field("vec", &&*guard); + }, + Err(TryLockError::Poisoned(err)) => { + d.field("vec", &&**err.get_ref()); + } + Err(TryLockError::WouldBlock) => { + struct LockedPlaceholder; + impl fmt::Debug for LockedPlaceholder { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("") + } + } + d.field("vec", &LockedPlaceholder); + }, + } + d.finish_non_exhaustive() + } +} + impl FrozenVec { /// Returns the number of elements in the vector. pub fn len(&self) -> usize {