diff --git a/src/condvar.rs b/src/condvar.rs index d6efab3c..902bc057 100644 --- a/src/condvar.rs +++ b/src/condvar.rs @@ -7,7 +7,7 @@ use std::sync::atomic::{AtomicPtr, Ordering}; use std::time::{Duration, Instant}; -use std::ptr; +use std::{ptr, fmt}; use parking_lot_core::{self, ParkResult, RequeueOp, UnparkResult, DEFAULT_PARK_TOKEN}; use mutex::{guard_lock, MutexGuard}; use raw_mutex::{RawMutex, TOKEN_HANDOFF, TOKEN_NORMAL}; @@ -354,6 +354,12 @@ impl Default for Condvar { } } +impl fmt::Debug for Condvar { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.pad("Condvar { .. }") + } +} + #[cfg(test)] mod tests { use std::sync::mpsc::channel; @@ -510,4 +516,10 @@ mod tests { let _ = c.wait_for(&mut m3.lock(), Duration::from_millis(1)); } + + #[test] + fn test_debug_condvar() { + let c = Condvar::new(); + assert_eq!(format!("{:?}", c), "Condvar { .. }"); + } } diff --git a/src/mutex.rs b/src/mutex.rs index 6ed176d1..065ad7ff 100644 --- a/src/mutex.rs +++ b/src/mutex.rs @@ -278,8 +278,10 @@ impl Default for Mutex { impl fmt::Debug for Mutex { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.try_lock() { - Some(guard) => write!(f, "Mutex {{ data: {:?} }}", &*guard), - None => write!(f, "Mutex {{ }}"), + Some(guard) => f.debug_struct("Mutex") + .field("data", &&*guard) + .finish(), + None => f.pad("Mutex { }"), } } } @@ -532,4 +534,21 @@ mod tests { let mutex = Mutex::new(()); sync(mutex.lock()); } + + #[test] + fn test_mutex_debug() { + let mutex = Mutex::new(vec![0u8, 10]); + + assert_eq!(format!("{:?}", mutex), "Mutex { data: [0, 10] }"); + assert_eq!(format!("{:#?}", mutex), +"Mutex { + data: [ + 0, + 10 + ] +}" + ); + let _lock = mutex.lock(); + assert_eq!(format!("{:?}", mutex), "Mutex { }"); + } } diff --git a/src/once.rs b/src/once.rs index 3ad34d28..869f25bd 100644 --- a/src/once.rs +++ b/src/once.rs @@ -345,7 +345,9 @@ impl Default for Once { impl fmt::Debug for Once { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "Once {{ state: {:?} }}", &self.state()) + f.debug_struct("Once") + .field("state", &self.state()) + .finish() } } @@ -470,4 +472,16 @@ mod tests { assert!(t1.join().is_ok()); assert!(t2.join().is_ok()); } + + #[test] + fn test_once_debug() { + static O: Once = ONCE_INIT; + + assert_eq!(format!("{:?}", O), "Once { state: New }"); + assert_eq!(format!("{:#?}", O), +"Once { + state: New +}" + ); + } } diff --git a/src/remutex.rs b/src/remutex.rs index d11533ec..2157997a 100644 --- a/src/remutex.rs +++ b/src/remutex.rs @@ -214,8 +214,10 @@ impl Default for ReentrantMutex { impl fmt::Debug for ReentrantMutex { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.try_lock() { - Some(guard) => write!(f, "ReentrantMutex {{ data: {:?} }}", &*guard), - None => write!(f, "ReentrantMutex {{ }}"), + Some(guard) => f.debug_struct("ReentrantMutex") + .field("data", &&*guard) + .finish(), + None => f.pad("ReentrantMutex { }"), } } } @@ -335,4 +337,19 @@ mod tests { .unwrap(); let _lock3 = m.try_lock(); } + + #[test] + fn test_reentrant_mutex_debug() { + let mutex = ReentrantMutex::new(vec![0u8, 10]); + + assert_eq!(format!("{:?}", mutex), "ReentrantMutex { data: [0, 10] }"); + assert_eq!(format!("{:#?}", mutex), +"ReentrantMutex { + data: [ + 0, + 10 + ] +}" + ); + } } diff --git a/src/rwlock.rs b/src/rwlock.rs index 852442e8..c2ddc11c 100644 --- a/src/rwlock.rs +++ b/src/rwlock.rs @@ -729,8 +729,10 @@ impl Default for RwLock { impl fmt::Debug for RwLock { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.try_read() { - Some(guard) => write!(f, "RwLock {{ data: {:?} }}", &*guard), - None => write!(f, "RwLock {{ }}"), + Some(guard) => f.debug_struct("RwLock") + .field("data", &&*guard) + .finish(), + None => f.pad("RwLock { }"), } } } @@ -1440,4 +1442,21 @@ mod tests { // A normal read would block here since there is a pending writer let _lock2 = arc.read_recursive(); } + + #[test] + fn test_rwlock_debug() { + let x = RwLock::new(vec![0u8, 10]); + + assert_eq!(format!("{:?}", x), "RwLock { data: [0, 10] }"); + assert_eq!(format!("{:#?}", x), +"RwLock { + data: [ + 0, + 10 + ] +}" + ); + let _lock = x.write(); + assert_eq!(format!("{:?}", x), "RwLock { }"); + } }