Skip to content
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

Refactor to use debug_struct in several Debug impls #44775

Merged
merged 1 commit into from
Oct 10, 2017
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
15 changes: 2 additions & 13 deletions src/librustc_data_structures/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//! be indexed by the direction (see the type `Direction`).

use bitvec::BitVector;
use std::fmt::{Formatter, Error, Debug};
use std::fmt::Debug;
use std::usize;
use snapshot_vec::{SnapshotVec, SnapshotVecDelegate};

Expand All @@ -48,6 +48,7 @@ pub struct Node<N> {
pub data: N,
}

#[derive(Debug)]
pub struct Edge<E> {
next_edge: [EdgeIndex; 2], // see module comment
source: NodeIndex,
Expand All @@ -69,18 +70,6 @@ impl<N> SnapshotVecDelegate for Edge<N> {
fn reverse(_: &mut Vec<Edge<N>>, _: ()) {}
}

impl<E: Debug> Debug for Edge<E> {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f,
"Edge {{ next_edge: [{:?}, {:?}], source: {:?}, target: {:?}, data: {:?} }}",
self.next_edge[0],
self.next_edge[1],
self.source,
self.target,
self.data)
}
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub struct NodeIndex(pub usize);

Expand Down
24 changes: 3 additions & 21 deletions src/libstd/sync/mpsc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ impl<T> Drop for Sender<T> {
#[stable(feature = "mpsc_debug", since = "1.8.0")]
impl<T> fmt::Debug for Sender<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Sender {{ .. }}")
f.debug_struct("Sender").finish()
}
}

Expand Down Expand Up @@ -1049,7 +1049,7 @@ impl<T> Drop for SyncSender<T> {
#[stable(feature = "mpsc_debug", since = "1.8.0")]
impl<T> fmt::Debug for SyncSender<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "SyncSender {{ .. }}")
f.debug_struct("SyncSender").finish()
}
}

Expand Down Expand Up @@ -1551,7 +1551,7 @@ impl<T> Drop for Receiver<T> {
#[stable(feature = "mpsc_debug", since = "1.8.0")]
impl<T> fmt::Debug for Receiver<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Receiver {{ .. }}")
f.debug_struct("Receiver").finish()
}
}

Expand Down Expand Up @@ -3009,22 +3009,4 @@ mod sync_tests {
repro()
}
}

#[test]
fn fmt_debug_sender() {
let (tx, _) = channel::<i32>();
assert_eq!(format!("{:?}", tx), "Sender { .. }");
}

#[test]
fn fmt_debug_recv() {
let (_, rx) = channel::<i32>();
assert_eq!(format!("{:?}", rx), "Receiver { .. }");
}

#[test]
fn fmt_debug_sync_sender() {
let (tx, _) = sync_channel::<i32>(1);
assert_eq!(format!("{:?}", tx), "SyncSender { .. }");
}
}
18 changes: 2 additions & 16 deletions src/libstd/sync/mpsc/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,13 @@ impl Iterator for Packets {

impl fmt::Debug for Select {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Select {{ .. }}")
f.debug_struct("Select").finish()
}
}

impl<'rx, T:Send+'rx> fmt::Debug for Handle<'rx, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Handle {{ .. }}")
f.debug_struct("Handle").finish()
}
}

Expand Down Expand Up @@ -774,18 +774,4 @@ mod tests {
}
}
}

#[test]
fn fmt_debug_select() {
let sel = Select::new();
assert_eq!(format!("{:?}", sel), "Select { .. }");
}

#[test]
fn fmt_debug_handle() {
let (_, rx) = channel::<i32>();
let sel = Select::new();
let handle = sel.handle(&rx);
assert_eq!(format!("{:?}", handle), "Handle { .. }");
}
}
13 changes: 10 additions & 3 deletions src/libstd/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,18 @@ 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() {
Ok(guard) => write!(f, "Mutex {{ data: {:?} }}", &*guard),
Ok(guard) => f.debug_struct("Mutex").field("data", &&*guard).finish(),
Err(TryLockError::Poisoned(err)) => {
write!(f, "Mutex {{ data: Poisoned({:?}) }}", &**err.get_ref())
f.debug_struct("Mutex").field("data", &&**err.get_ref()).finish()
},
Err(TryLockError::WouldBlock) => write!(f, "Mutex {{ <locked> }}")
Err(TryLockError::WouldBlock) => {
struct LockedPlaceholder;
impl fmt::Debug for LockedPlaceholder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("<locked>") }
}

f.debug_struct("Mutex").field("data", &LockedPlaceholder).finish()
}
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/libstd/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,18 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop 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() {
Ok(guard) => write!(f, "RwLock {{ data: {:?} }}", &*guard),
Ok(guard) => f.debug_struct("RwLock").field("data", &&*guard).finish(),
Err(TryLockError::Poisoned(err)) => {
write!(f, "RwLock {{ data: Poisoned({:?}) }}", &**err.get_ref())
f.debug_struct("RwLock").field("data", &&**err.get_ref()).finish()
},
Err(TryLockError::WouldBlock) => write!(f, "RwLock {{ <locked> }}")
Err(TryLockError::WouldBlock) => {
struct LockedPlaceholder;
impl fmt::Debug for LockedPlaceholder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("<locked>") }
}

f.debug_struct("RwLock").field("data", &LockedPlaceholder).finish()
}
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/libstd/sys_common/remutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,18 @@ impl<T> Drop for ReentrantMutex<T> {
impl<T: fmt::Debug + 'static> fmt::Debug for ReentrantMutex<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.try_lock() {
Ok(guard) => write!(f, "ReentrantMutex {{ data: {:?} }}", &*guard),
Ok(guard) => f.debug_struct("ReentrantMutex").field("data", &*guard).finish(),
Err(TryLockError::Poisoned(err)) => {
write!(f, "ReentrantMutex {{ data: Poisoned({:?}) }}", &**err.get_ref())
f.debug_struct("ReentrantMutex").field("data", &**err.get_ref()).finish()
},
Err(TryLockError::WouldBlock) => write!(f, "ReentrantMutex {{ <locked> }}")
Err(TryLockError::WouldBlock) => {
struct LockedPlaceholder;
impl fmt::Debug for LockedPlaceholder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("<locked>") }
}

f.debug_struct("ReentrantMutex").field("data", &LockedPlaceholder).finish()
}
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/libsyntax_pos/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,11 @@ impl serialize::UseSpecializedDecodable for Span {
}

fn default_span_debug(span: Span, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Span {{ lo: {:?}, hi: {:?}, ctxt: {:?} }}",
span.lo(), span.hi(), span.ctxt())
f.debug_struct("Span")
.field("lo", &span.lo())
.field("hi", &span.hi())
.field("ctxt", &span.ctxt())
.finish()
}

impl fmt::Debug for Span {
Expand Down