Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 { .. }");
}
}
23 changes: 21 additions & 2 deletions src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,10 @@ impl<T: ?Sized + Default> Default for Mutex<T> {
impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.try_lock() {
Some(guard) => write!(f, "Mutex {{ data: {:?} }}", &*guard),
None => write!(f, "Mutex {{ <locked> }}"),
Some(guard) => f.debug_struct("Mutex")
.field("data", &&*guard)
.finish(),
None => f.pad("Mutex { <locked> }"),
}
}
}
Expand Down Expand Up @@ -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 { <locked> }");
}
}
16 changes: 15 additions & 1 deletion src/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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
}"
);
}
}
21 changes: 19 additions & 2 deletions src/remutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,10 @@ impl<T: ?Sized + Default> Default for ReentrantMutex<T> {
impl<T: ?Sized + fmt::Debug> fmt::Debug for ReentrantMutex<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.try_lock() {
Some(guard) => write!(f, "ReentrantMutex {{ data: {:?} }}", &*guard),
None => write!(f, "ReentrantMutex {{ <locked> }}"),
Some(guard) => f.debug_struct("ReentrantMutex")
.field("data", &&*guard)
.finish(),
None => f.pad("ReentrantMutex { <locked> }"),
}
}
}
Expand Down Expand Up @@ -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
]
}"
);
}
}
23 changes: 21 additions & 2 deletions src/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,10 @@ impl<T: ?Sized + Default> Default for RwLock<T> {
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.try_read() {
Some(guard) => write!(f, "RwLock {{ data: {:?} }}", &*guard),
None => write!(f, "RwLock {{ <locked> }}"),
Some(guard) => f.debug_struct("RwLock")
.field("data", &&*guard)
.finish(),
None => f.pad("RwLock { <locked> }"),
}
}
}
Expand Down Expand Up @@ -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 { <locked> }");
}
}