Skip to content

Commit 5402abc

Browse files
committed
Improve Debug implementations of Mutex and RwLock.
They now show the poison flag and use debug_non_exhaustive.
1 parent feaac19 commit 5402abc

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

library/std/src/sync/mutex.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,13 @@ impl<T: ?Sized + Default> Default for Mutex<T> {
441441
#[stable(feature = "rust1", since = "1.0.0")]
442442
impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
443443
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
444+
let mut d = f.debug_struct("Mutex");
444445
match self.try_lock() {
445-
Ok(guard) => f.debug_struct("Mutex").field("data", &&*guard).finish(),
446+
Ok(guard) => {
447+
d.field("data", &&*guard);
448+
}
446449
Err(TryLockError::Poisoned(err)) => {
447-
f.debug_struct("Mutex").field("data", &&**err.get_ref()).finish()
450+
d.field("data", &&**err.get_ref());
448451
}
449452
Err(TryLockError::WouldBlock) => {
450453
struct LockedPlaceholder;
@@ -453,10 +456,11 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
453456
f.write_str("<locked>")
454457
}
455458
}
456-
457-
f.debug_struct("Mutex").field("data", &LockedPlaceholder).finish()
459+
d.field("data", &LockedPlaceholder);
458460
}
459461
}
462+
d.field("poisoned", &self.poison.get());
463+
d.finish_non_exhaustive()
460464
}
461465
}
462466

library/std/src/sync/rwlock.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,13 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for RwLock<T> {
422422
#[stable(feature = "rust1", since = "1.0.0")]
423423
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
424424
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
425+
let mut d = f.debug_struct("RwLock");
425426
match self.try_read() {
426-
Ok(guard) => f.debug_struct("RwLock").field("data", &&*guard).finish(),
427+
Ok(guard) => {
428+
d.field("data", &&*guard);
429+
}
427430
Err(TryLockError::Poisoned(err)) => {
428-
f.debug_struct("RwLock").field("data", &&**err.get_ref()).finish()
431+
d.field("data", &&**err.get_ref());
429432
}
430433
Err(TryLockError::WouldBlock) => {
431434
struct LockedPlaceholder;
@@ -434,10 +437,11 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
434437
f.write_str("<locked>")
435438
}
436439
}
437-
438-
f.debug_struct("RwLock").field("data", &LockedPlaceholder).finish()
440+
d.field("data", &LockedPlaceholder);
439441
}
440442
}
443+
d.field("poisoned", &self.poison.get());
444+
d.finish_non_exhaustive()
441445
}
442446
}
443447

0 commit comments

Comments
 (0)