From 662ae239c7c8746f14713e23e546a7572cbec0fe Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Thu, 30 Jul 2020 23:03:25 +0300 Subject: [PATCH 1/4] Improve Debug impl for Mutex --- tokio/src/sync/mutex.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs index 642058be626..757dc9e27be 100644 --- a/tokio/src/sync/mutex.rs +++ b/tokio/src/sync/mutex.rs @@ -115,7 +115,6 @@ use std::sync::Arc; /// [`std::sync::Mutex`]: struct@std::sync::Mutex /// [`Send`]: trait@std::marker::Send /// [`lock`]: method@Mutex::lock -#[derive(Debug)] pub struct Mutex { s: semaphore::Semaphore, c: UnsafeCell, @@ -373,6 +372,18 @@ where } } +impl std::fmt::Debug for Mutex +where + T: std::fmt::Debug, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self.try_lock() { + Ok(inner) => f.debug_struct("Mutex").field("inner", &*inner).finish(), + Err(_) => f.debug_struct("Mutex").field("inner", &"").finish(), + } + } +} + // === impl MutexGuard === impl Drop for MutexGuard<'_, T> { From c8fb370560bad8dc4460a1d6e432f9d5db29333a Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Fri, 31 Jul 2020 14:37:08 +0300 Subject: [PATCH 2/4] improve code --- tokio/src/sync/mutex.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tokio/src/sync/mutex.rs b/tokio/src/sync/mutex.rs index 757dc9e27be..df348457fec 100644 --- a/tokio/src/sync/mutex.rs +++ b/tokio/src/sync/mutex.rs @@ -377,10 +377,12 @@ where T: std::fmt::Debug, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut d = f.debug_struct("Mutex"); match self.try_lock() { - Ok(inner) => f.debug_struct("Mutex").field("inner", &*inner).finish(), - Err(_) => f.debug_struct("Mutex").field("inner", &"").finish(), - } + Ok(inner) => d.field("data", &*inner), + Err(_) => d.field("data", &format_args!("")), + }; + d.finish() } } From f6f2006d582ea136f2c462e9fa09f9ef541870ca Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Fri, 31 Jul 2020 14:42:48 +0300 Subject: [PATCH 3/4] add test --- tokio/tests/sync_mutex.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tokio/tests/sync_mutex.rs b/tokio/tests/sync_mutex.rs index 444ebd6a22d..ca1a5cff60e 100644 --- a/tokio/tests/sync_mutex.rs +++ b/tokio/tests/sync_mutex.rs @@ -152,3 +152,12 @@ async fn debug_format() { let m = Mutex::new(s.to_string()); assert_eq!(format!("{:?}", s), format!("{:?}", m.lock().await)); } + +#[tokio::test] +async fn mutex_debug() { + let s = "data"; + let m = Mutex::new(s.to_string()); + assert_eq!(format!("{:?}", m), r#"Mutex { data: "data" }"#); + let _guard = m.lock().await; + assert_eq!(format!("{:?}", m), r#"Mutex { data: }"#) +} \ No newline at end of file From 7d339bde2b299ca861f51f33d539a358ab1b55db Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Fri, 31 Jul 2020 15:44:23 +0300 Subject: [PATCH 4/4] fmt --- tokio/tests/sync_mutex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/tests/sync_mutex.rs b/tokio/tests/sync_mutex.rs index ca1a5cff60e..96194b31d9d 100644 --- a/tokio/tests/sync_mutex.rs +++ b/tokio/tests/sync_mutex.rs @@ -160,4 +160,4 @@ async fn mutex_debug() { assert_eq!(format!("{:?}", m), r#"Mutex { data: "data" }"#); let _guard = m.lock().await; assert_eq!(format!("{:?}", m), r#"Mutex { data: }"#) -} \ No newline at end of file +}