From 603fa7a98ccedad0201164fa0b03c745d25b955b Mon Sep 17 00:00:00 2001 From: Mica White Date: Wed, 13 Mar 2024 16:50:37 -0400 Subject: add Debug to WriteLock --- src/rwlock/write_lock.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/rwlock/write_lock.rs b/src/rwlock/write_lock.rs index 0275a70..d7333ae 100644 --- a/src/rwlock/write_lock.rs +++ b/src/rwlock/write_lock.rs @@ -6,9 +6,25 @@ use crate::key::Keyable; use super::{RwLock, RwLockWriteGuard, RwLockWriteRef, WriteLock}; -impl<'a, T: ?Sized, R> Debug for WriteLock<'a, T, R> { +impl<'a, T: ?Sized + Debug, R: RawRwLock> Debug for WriteLock<'a, T, R> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(&format!("WriteLock<{}>", std::any::type_name::())) + // safety: this is just a try lock, and the value is dropped + // immediately after, so there's no risk of blocking ourselves + // or any other threads + if let Some(value) = unsafe { self.try_lock_no_key() } { + f.debug_struct("WriteLock").field("data", &&*value).finish() + } else { + struct LockedPlaceholder; + impl Debug for LockedPlaceholder { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("") + } + } + + f.debug_struct("ReadLock") + .field("data", &LockedPlaceholder) + .finish() + } } } -- cgit v1.2.3