-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #5712 - alexcrichton:better-debug, r=ehuss
Improve Debug display for a few types Try to cut down on the wordiness here and make these implementations a bit more usable by default.
- Loading branch information
Showing
6 changed files
with
167 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::fmt; | ||
|
||
macro_rules! compact_debug { | ||
( | ||
impl fmt::Debug for $ty:ident { | ||
fn fmt(&$this:ident, f: &mut fmt::Formatter) -> fmt::Result { | ||
let (default, default_name) = $e:expr; | ||
[debug_the_fields($($field:ident)*)] | ||
} | ||
} | ||
) => ( | ||
|
||
impl fmt::Debug for $ty { | ||
fn fmt(&$this, f: &mut fmt::Formatter) -> fmt::Result { | ||
// Try printing a pretty version where we collapse as many fields as | ||
// possible, indicating that they're equivalent to a function call | ||
// that's hopefully enough to indicate what each value is without | ||
// actually dumping everything so verbosely. | ||
let mut s = f.debug_struct(stringify!($ty)); | ||
let (default, default_name) = $e; | ||
let mut any_default = false; | ||
|
||
// Exhaustively match so when fields are added we get a compile | ||
// failure | ||
let $ty { $($field),* } = $this; | ||
$( | ||
if *$field == default.$field { | ||
any_default = true; | ||
} else { | ||
s.field(stringify!($field), $field); | ||
} | ||
)* | ||
|
||
if any_default { | ||
s.field("..", &::macros::DisplayAsDebug(default_name)); | ||
} | ||
s.finish() | ||
} | ||
} | ||
) | ||
} | ||
|
||
pub struct DisplayAsDebug<T>(pub T); | ||
|
||
impl<T: fmt::Display> fmt::Debug for DisplayAsDebug<T> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
fmt::Display::fmt(&self.0, f) | ||
} | ||
} |