Skip to content

Commit bd0d8e4

Browse files
committed
Auto merge of #22573 - nwin:impl-debug-rwlock-weak, r=Manishearth
Implements `Debug` for `RwLock` and `arc::Weak` in the same way it is implemented for `rc::Weak` (basically copy & paste). The lack of this implementation prevents the automatic implementation of `Debug` for structs containing members of these types.
2 parents bd27985 + 36ba96e commit bd0d8e4

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/liballoc/arc.rs

+7
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ pub struct Weak<T> {
139139
unsafe impl<T: Sync + Send> Send for Weak<T> { }
140140
unsafe impl<T: Sync + Send> Sync for Weak<T> { }
141141

142+
#[stable(feature = "rust1", since = "1.0.0")]
143+
impl<T: fmt::Debug> fmt::Debug for Weak<T> {
144+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
145+
write!(f, "(Weak)")
146+
}
147+
}
148+
142149
struct ArcInner<T> {
143150
strong: atomic::AtomicUsize,
144151
weak: atomic::AtomicUsize,

src/libstd/sync/mutex.rs

+14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use marker;
1515
use ops::{Deref, DerefMut};
1616
use sync::poison::{self, TryLockError, TryLockResult, LockResult};
1717
use sys_common::mutex as sys;
18+
use fmt;
1819

1920
/// A mutual exclusion primitive useful for protecting shared data
2021
///
@@ -250,6 +251,19 @@ impl<T: Send> Drop for Mutex<T> {
250251
}
251252
}
252253

254+
#[stable(feature = "rust1", since = "1.0.0")]
255+
impl<T: fmt::Debug + Send + 'static> fmt::Debug for Mutex<T> {
256+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
257+
match self.try_lock() {
258+
Ok(guard) => write!(f, "Mutex {{ data: {:?} }}", *guard),
259+
Err(TryLockError::Poisoned(err)) => {
260+
write!(f, "Mutex {{ data: Poisoned({:?}) }}", **err.get_ref())
261+
},
262+
Err(TryLockError::WouldBlock) => write!(f, "Mutex {{ <locked> }}")
263+
}
264+
}
265+
}
266+
253267
struct Dummy(UnsafeCell<()>);
254268
unsafe impl Sync for Dummy {}
255269
static DUMMY: Dummy = Dummy(UnsafeCell { value: () });

src/libstd/sync/rwlock.rs

+14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use marker;
1515
use ops::{Deref, DerefMut};
1616
use sync::poison::{self, LockResult, TryLockError, TryLockResult};
1717
use sys_common::rwlock as sys;
18+
use fmt;
1819

1920
/// A reader-writer lock
2021
///
@@ -255,6 +256,19 @@ impl<T> Drop for RwLock<T> {
255256
}
256257
}
257258

259+
#[stable(feature = "rust1", since = "1.0.0")]
260+
impl<T: fmt::Debug + Send + Sync> fmt::Debug for RwLock<T> {
261+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
262+
match self.try_read() {
263+
Ok(guard) => write!(f, "RwLock {{ data: {:?} }}", *guard),
264+
Err(TryLockError::Poisoned(err)) => {
265+
write!(f, "RwLock {{ data: Poisoned({:?}) }}", **err.get_ref())
266+
},
267+
Err(TryLockError::WouldBlock) => write!(f, "RwLock {{ <locked> }}")
268+
}
269+
}
270+
}
271+
258272
struct Dummy(UnsafeCell<()>);
259273
unsafe impl Sync for Dummy {}
260274
static DUMMY: Dummy = Dummy(UnsafeCell { value: () });

0 commit comments

Comments
 (0)