From 11b88075440f1b141e4a18c7d506df0fc0eceb71 Mon Sep 17 00:00:00 2001 From: Fergus Mitchell Date: Mon, 24 Apr 2023 14:39:42 +0100 Subject: [PATCH] sync: improve Debug impl for `RwLock` (#5647) --- tokio/src/sync/rwlock.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tokio/src/sync/rwlock.rs b/tokio/src/sync/rwlock.rs index c332ede2bbe..dd4928546fc 100644 --- a/tokio/src/sync/rwlock.rs +++ b/tokio/src/sync/rwlock.rs @@ -85,7 +85,6 @@ const MAX_READS: u32 = 10; /// [`RwLockWriteGuard`]: struct@RwLockWriteGuard /// [`Send`]: trait@std::marker::Send /// [_write-preferring_]: https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock#Priority_policies -#[derive(Debug)] pub struct RwLock { #[cfg(all(tokio_unstable, feature = "tracing"))] resource_span: tracing::Span, @@ -1095,3 +1094,17 @@ where Self::new(T::default()) } } + +impl std::fmt::Debug for RwLock +where + T: std::fmt::Debug, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut d = f.debug_struct("RwLock"); + match self.try_read() { + Ok(inner) => d.field("data", &&*inner), + Err(_) => d.field("data", &format_args!("")), + }; + d.finish() + } +}