From 614951142627294161f093adcddaeee299edad69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Wed, 27 Sep 2023 08:25:29 -0400 Subject: [PATCH] feat: Implement Debug trait for sync variants FrozenMap and FrozenVec - partially contributes to #32, - Implements `Debug` trait for those structures where checking for reentrancy is straightforward. --- src/sync.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/sync.rs b/src/sync.rs index a551b9a..c936846 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -11,6 +11,7 @@ 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; @@ -27,6 +28,26 @@ 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::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 +408,26 @@ 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.map.try_read() { + Ok(guard) => { + d.field("vec", &&*guard); + }, + 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 {