Skip to content

Commit

Permalink
sync: improve Debug impl for RwLock (#5647)
Browse files Browse the repository at this point in the history
  • Loading branch information
FHTMitchell authored Apr 24, 2023
1 parent e789b61 commit 11b8807
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tokio/src/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
}
}

0 comments on commit 11b8807

Please sign in to comment.