Improve Debug representations
diff --git a/src/lib.rs b/src/lib.rs
index 7b9a919..21becec 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -251,7 +251,7 @@
}
}
-#[derive(Clone, Debug)]
+#[derive(Clone)]
pub enum TokenTree {
Group(Group),
Term(Term),
@@ -314,7 +314,20 @@
}
}
-#[derive(Clone, Debug)]
+impl fmt::Debug for TokenTree {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ // Each of these has the name in the struct type in the derived debug,
+ // so don't bother with an extra layer of indirection
+ match *self {
+ TokenTree::Group(ref t) => t.fmt(f),
+ TokenTree::Term(ref t) => t.fmt(f),
+ TokenTree::Op(ref t) => t.fmt(f),
+ TokenTree::Literal(ref t) => t.fmt(f),
+ }
+ }
+}
+
+#[derive(Clone)]
pub struct Group {
delimiter: Delimiter,
stream: TokenStream,
@@ -361,7 +374,18 @@
}
}
-#[derive(Copy, Clone, Debug)]
+impl fmt::Debug for Group {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ let mut debug = fmt.debug_struct("Group");
+ debug.field("delimiter", &self.delimiter);
+ debug.field("stream", &self.stream);
+ #[cfg(procmacro2_semver_exempt)]
+ debug.field("span", &self.span);
+ debug.finish()
+ }
+}
+
+#[derive(Copy, Clone)]
pub struct Op {
op: char,
spacing: Spacing,
@@ -406,6 +430,17 @@
}
}
+impl fmt::Debug for Op {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ let mut debug = fmt.debug_struct("Op");
+ debug.field("op", &self.op);
+ debug.field("spacing", &self.spacing);
+ #[cfg(procmacro2_semver_exempt)]
+ debug.field("span", &self.span);
+ debug.finish()
+ }
+}
+
#[derive(Copy, Clone)]
pub struct Term {
inner: imp::Term,
diff --git a/src/stable.rs b/src/stable.rs
index ad9870c..1ca48a5 100644
--- a/src/stable.rs
+++ b/src/stable.rs
@@ -16,7 +16,7 @@
use {Delimiter, Group, Op, Spacing, TokenTree};
-#[derive(Clone, Debug)]
+#[derive(Clone)]
pub struct TokenStream {
inner: Vec<TokenTree>,
}
@@ -111,6 +111,13 @@
}
}
+impl fmt::Debug for TokenStream {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ f.write_str("TokenStream ")?;
+ f.debug_list().entries(self.clone()).finish()
+ }
+}
+
#[cfg(feature = "proc-macro")]
impl From<::proc_macro::TokenStream> for TokenStream {
fn from(inner: ::proc_macro::TokenStream) -> TokenStream {
@@ -314,7 +321,7 @@
}
}
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Span {
#[cfg(procmacro2_semver_exempt)]
lo: u32,
@@ -393,6 +400,16 @@
}
}
+impl fmt::Debug for Span {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ #[cfg(procmacro2_semver_exempt)]
+ return write!(f, "bytes({}..{})", self.lo, self.hi);
+
+ #[cfg(not(procmacro2_semver_exempt))]
+ write!(f, "Span")
+ }
+}
+
#[derive(Copy, Clone)]
pub struct Term {
intern: usize,
@@ -466,7 +483,11 @@
impl fmt::Debug for Term {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- f.debug_tuple("Term").field(&self.as_str()).finish()
+ let mut debug = f.debug_struct("Term");
+ debug.field("sym", &format_args!("{}", self.as_str()));
+ #[cfg(procmacro2_semver_exempt)]
+ debug.field("span", &self.span);
+ debug.finish()
}
}
@@ -508,7 +529,7 @@
}
}
-#[derive(Clone, Debug)]
+#[derive(Clone)]
pub struct Literal {
text: String,
span: Span,
@@ -629,6 +650,16 @@
}
}
+impl fmt::Debug for Literal {
+ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+ let mut debug = fmt.debug_struct("Literal");
+ debug.field("lit", &format_args!("{}", self.text));
+ #[cfg(procmacro2_semver_exempt)]
+ debug.field("span", &self.span);
+ debug.finish()
+ }
+}
+
fn token_stream(mut input: Cursor) -> PResult<::TokenStream> {
let mut trees = Vec::new();
loop {