Skip to content

Implement Debug for RwLock and arc::Weak #22573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2015
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
7 changes: 7 additions & 0 deletions src/liballoc/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ pub struct Weak<T> {
unsafe impl<T: Sync + Send> Send for Weak<T> { }
unsafe impl<T: Sync + Send> Sync for Weak<T> { }

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: fmt::Debug> fmt::Debug for Weak<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "(Weak)")
}
}

struct ArcInner<T> {
strong: atomic::AtomicUsize,
weak: atomic::AtomicUsize,
Expand Down
14 changes: 14 additions & 0 deletions src/libstd/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use marker;
use ops::{Deref, DerefMut};
use sync::poison::{self, TryLockError, TryLockResult, LockResult};
use sys_common::mutex as sys;
use fmt;

/// A mutual exclusion primitive useful for protecting shared data
///
Expand Down Expand Up @@ -252,6 +253,19 @@ impl<T: Send + 'static> Drop for Mutex<T> {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: fmt::Debug + Send + 'static> fmt::Debug for Mutex<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.try_lock() {
Ok(guard) => write!(f, "Mutex {{ data: {:?} }}", *guard),
Err(TryLockError::Poisoned(err)) => {
write!(f, "Mutex {{ data: Poisoned({:?}) }}", **err.get_ref())
},
Err(TryLockError::WouldBlock) => write!(f, "Mutex {{ <locked> }}")
}
}
}

struct Dummy(UnsafeCell<()>);
unsafe impl Sync for Dummy {}
static DUMMY: Dummy = Dummy(UnsafeCell { value: () });
Expand Down
14 changes: 14 additions & 0 deletions src/libstd/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use marker;
use ops::{Deref, DerefMut};
use sync::poison::{self, LockResult, TryLockError, TryLockResult};
use sys_common::rwlock as sys;
use fmt;

/// A reader-writer lock
///
Expand Down Expand Up @@ -258,6 +259,19 @@ impl<T> Drop for RwLock<T> {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: fmt::Debug + Send + Sync> fmt::Debug for RwLock<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.try_read() {
Ok(guard) => write!(f, "RwLock {{ data: {:?} }}", *guard),
Err(TryLockError::Poisoned(err)) => {
write!(f, "RwLock {{ data: Poisoned({:?}) }}", **err.get_ref())
},
Err(TryLockError::WouldBlock) => write!(f, "RwLock {{ <locked> }}")
}
}
}

struct Dummy(UnsafeCell<()>);
unsafe impl Sync for Dummy {}
static DUMMY: Dummy = Dummy(UnsafeCell { value: () });
Expand Down