From 7b557119883685e541d555135fb81c0dc10e2752 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 17 Jan 2019 18:57:46 +0100 Subject: [PATCH 1/2] Make MutexGuard's Debug implementation more useful. Fixes #57702. --- src/libstd/sync/mutex.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index 856bb26042490..e4cc29792072f 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -450,9 +450,13 @@ impl<'a, T: ?Sized> Drop for MutexGuard<'a, T> { #[stable(feature = "std_debug", since = "1.16.0")] impl<'a, T: ?Sized + fmt::Debug> fmt::Debug for MutexGuard<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("MutexGuard") - .field("lock", &self.__lock) - .finish() + struct MutexFmt<'a, T: ?Sized>(&'a MutexGuard<'a, T>); + impl<'a, T: ?Sized + fmt::Debug> fmt::Debug for MutexFmt<'a, T> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("Mutex").field("data", &&*self.0).finish() + } + } + f.debug_struct("MutexGuard").field("lock", &MutexFmt(self)).finish() } } From 2e9deed9f130d2342bec2a55358305cb7137605e Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 17 Jan 2019 20:03:59 +0100 Subject: [PATCH 2/2] Simplify Debug implementation of MutexGuard. Just transparently print the guarded data, instead of wrapping it in `MutexGuard { lock: Mutex { data: ... } }`. --- src/libstd/sync/mutex.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index e4cc29792072f..59829db23cbc2 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -450,13 +450,7 @@ impl<'a, T: ?Sized> Drop for MutexGuard<'a, T> { #[stable(feature = "std_debug", since = "1.16.0")] impl<'a, T: ?Sized + fmt::Debug> fmt::Debug for MutexGuard<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - struct MutexFmt<'a, T: ?Sized>(&'a MutexGuard<'a, T>); - impl<'a, T: ?Sized + fmt::Debug> fmt::Debug for MutexFmt<'a, T> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("Mutex").field("data", &&*self.0).finish() - } - } - f.debug_struct("MutexGuard").field("lock", &MutexFmt(self)).finish() + fmt::Debug::fmt(&**self, f) } }