sync: improve Debug impl for RwLock (#5647)

This commit is contained in:
Fergus Mitchell
2023-04-24 15:39:42 +02:00
committed by GitHub
parent e789b61424
commit 11b8807544
+14 -1
View File
@@ -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<T: ?Sized> {
#[cfg(all(tokio_unstable, feature = "tracing"))]
resource_span: tracing::Span,
@@ -1095,3 +1094,17 @@ where
Self::new(T::default())
}
}
impl<T: ?Sized> std::fmt::Debug for RwLock<T>
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!("<locked>")),
};
d.finish()
}
}