Skip to content

Commit 23a99f4

Browse files
committed
Rollup merge of rust-lang#44775 - MaloJaffre:debug-struct, r=sfackler
Refactor to use `debug_struct` in several Debug impls Also use `pad` and derive `Debug` for `Edge`. Fixes rust-lang#44771.
2 parents 0217315 + 679457a commit 23a99f4

File tree

7 files changed

+42
-61
lines changed

7 files changed

+42
-61
lines changed

src/librustc_data_structures/graph/mod.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//! be indexed by the direction (see the type `Direction`).
3232
3333
use bitvec::BitVector;
34-
use std::fmt::{Formatter, Error, Debug};
34+
use std::fmt::Debug;
3535
use std::usize;
3636
use snapshot_vec::{SnapshotVec, SnapshotVecDelegate};
3737

@@ -48,6 +48,7 @@ pub struct Node<N> {
4848
pub data: N,
4949
}
5050

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

72-
impl<E: Debug> Debug for Edge<E> {
73-
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
74-
write!(f,
75-
"Edge {{ next_edge: [{:?}, {:?}], source: {:?}, target: {:?}, data: {:?} }}",
76-
self.next_edge[0],
77-
self.next_edge[1],
78-
self.source,
79-
self.target,
80-
self.data)
81-
}
82-
}
83-
8473
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
8574
pub struct NodeIndex(pub usize);
8675

src/libstd/sync/mpsc/mod.rs

+3-21
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ impl<T> Drop for Sender<T> {
919919
#[stable(feature = "mpsc_debug", since = "1.8.0")]
920920
impl<T> fmt::Debug for Sender<T> {
921921
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
922-
write!(f, "Sender {{ .. }}")
922+
f.debug_struct("Sender").finish()
923923
}
924924
}
925925

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

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

@@ -3009,22 +3009,4 @@ mod sync_tests {
30093009
repro()
30103010
}
30113011
}
3012-
3013-
#[test]
3014-
fn fmt_debug_sender() {
3015-
let (tx, _) = channel::<i32>();
3016-
assert_eq!(format!("{:?}", tx), "Sender { .. }");
3017-
}
3018-
3019-
#[test]
3020-
fn fmt_debug_recv() {
3021-
let (_, rx) = channel::<i32>();
3022-
assert_eq!(format!("{:?}", rx), "Receiver { .. }");
3023-
}
3024-
3025-
#[test]
3026-
fn fmt_debug_sync_sender() {
3027-
let (tx, _) = sync_channel::<i32>(1);
3028-
assert_eq!(format!("{:?}", tx), "SyncSender { .. }");
3029-
}
30303012
}

src/libstd/sync/mpsc/select.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,13 @@ impl Iterator for Packets {
354354

355355
impl fmt::Debug for Select {
356356
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
357-
write!(f, "Select {{ .. }}")
357+
f.debug_struct("Select").finish()
358358
}
359359
}
360360

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

@@ -774,18 +774,4 @@ mod tests {
774774
}
775775
}
776776
}
777-
778-
#[test]
779-
fn fmt_debug_select() {
780-
let sel = Select::new();
781-
assert_eq!(format!("{:?}", sel), "Select { .. }");
782-
}
783-
784-
#[test]
785-
fn fmt_debug_handle() {
786-
let (_, rx) = channel::<i32>();
787-
let sel = Select::new();
788-
let handle = sel.handle(&rx);
789-
assert_eq!(format!("{:?}", handle), "Handle { .. }");
790-
}
791777
}

src/libstd/sync/mutex.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,18 @@ impl<T: ?Sized + Default> Default for Mutex<T> {
394394
impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
395395
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
396396
match self.try_lock() {
397-
Ok(guard) => write!(f, "Mutex {{ data: {:?} }}", &*guard),
397+
Ok(guard) => f.debug_struct("Mutex").field("data", &&*guard).finish(),
398398
Err(TryLockError::Poisoned(err)) => {
399-
write!(f, "Mutex {{ data: Poisoned({:?}) }}", &**err.get_ref())
399+
f.debug_struct("Mutex").field("data", &&**err.get_ref()).finish()
400400
},
401-
Err(TryLockError::WouldBlock) => write!(f, "Mutex {{ <locked> }}")
401+
Err(TryLockError::WouldBlock) => {
402+
struct LockedPlaceholder;
403+
impl fmt::Debug for LockedPlaceholder {
404+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("<locked>") }
405+
}
406+
407+
f.debug_struct("Mutex").field("data", &LockedPlaceholder).finish()
408+
}
402409
}
403410
}
404411
}

src/libstd/sync/rwlock.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,18 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for RwLock<T> {
428428
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
429429
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
430430
match self.try_read() {
431-
Ok(guard) => write!(f, "RwLock {{ data: {:?} }}", &*guard),
431+
Ok(guard) => f.debug_struct("RwLock").field("data", &&*guard).finish(),
432432
Err(TryLockError::Poisoned(err)) => {
433-
write!(f, "RwLock {{ data: Poisoned({:?}) }}", &**err.get_ref())
433+
f.debug_struct("RwLock").field("data", &&**err.get_ref()).finish()
434434
},
435-
Err(TryLockError::WouldBlock) => write!(f, "RwLock {{ <locked> }}")
435+
Err(TryLockError::WouldBlock) => {
436+
struct LockedPlaceholder;
437+
impl fmt::Debug for LockedPlaceholder {
438+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("<locked>") }
439+
}
440+
441+
f.debug_struct("RwLock").field("data", &LockedPlaceholder).finish()
442+
}
436443
}
437444
}
438445
}

src/libstd/sys_common/remutex.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,18 @@ impl<T> Drop for ReentrantMutex<T> {
116116
impl<T: fmt::Debug + 'static> fmt::Debug for ReentrantMutex<T> {
117117
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
118118
match self.try_lock() {
119-
Ok(guard) => write!(f, "ReentrantMutex {{ data: {:?} }}", &*guard),
119+
Ok(guard) => f.debug_struct("ReentrantMutex").field("data", &*guard).finish(),
120120
Err(TryLockError::Poisoned(err)) => {
121-
write!(f, "ReentrantMutex {{ data: Poisoned({:?}) }}", &**err.get_ref())
121+
f.debug_struct("ReentrantMutex").field("data", &**err.get_ref()).finish()
122122
},
123-
Err(TryLockError::WouldBlock) => write!(f, "ReentrantMutex {{ <locked> }}")
123+
Err(TryLockError::WouldBlock) => {
124+
struct LockedPlaceholder;
125+
impl fmt::Debug for LockedPlaceholder {
126+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str("<locked>") }
127+
}
128+
129+
f.debug_struct("ReentrantMutex").field("data", &LockedPlaceholder).finish()
130+
}
124131
}
125132
}
126133
}

src/libsyntax_pos/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,11 @@ impl serialize::UseSpecializedDecodable for Span {
339339
}
340340

341341
fn default_span_debug(span: Span, f: &mut fmt::Formatter) -> fmt::Result {
342-
write!(f, "Span {{ lo: {:?}, hi: {:?}, ctxt: {:?} }}",
343-
span.lo(), span.hi(), span.ctxt())
342+
f.debug_struct("Span")
343+
.field("lo", &span.lo())
344+
.field("hi", &span.hi())
345+
.field("ctxt", &span.ctxt())
346+
.finish()
344347
}
345348

346349
impl fmt::Debug for Span {

0 commit comments

Comments
 (0)