From bef084bb724c8885a3045183f44492ff1016ba6c Mon Sep 17 00:00:00 2001 From: Koxiaet <38139193+Koxiaet@users.noreply.github.com> Date: Thu, 3 Sep 2020 14:10:34 +0100 Subject: [PATCH] Improve fallback Display impl for TokenStream This also fixes an issue where `quote!((1 + 2)).to_string()` outputs `( 1 + 2 )` in fallback mode but `(1 + 2)` in regular mode. --- src/fallback.rs | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/src/fallback.rs b/src/fallback.rs index 23250c97..c95cb3cb 100644 --- a/src/fallback.rs +++ b/src/fallback.rs @@ -157,29 +157,14 @@ impl Display for TokenStream { } joint = false; match tt { - TokenTree::Group(tt) => { - let (start, end) = match tt.delimiter() { - Delimiter::Parenthesis => ("(", ")"), - Delimiter::Brace => ("{", "}"), - Delimiter::Bracket => ("[", "]"), - Delimiter::None => ("", ""), - }; - if tt.stream().into_iter().next().is_none() { - write!(f, "{} {}", start, end)? - } else { - write!(f, "{} {} {}", start, tt.stream(), end)? - } - } - TokenTree::Ident(tt) => write!(f, "{}", tt)?, + TokenTree::Group(tt) => Display::fmt(tt, f), + TokenTree::Ident(tt) => Display::fmt(tt, f), TokenTree::Punct(tt) => { - write!(f, "{}", tt.as_char())?; - match tt.spacing() { - Spacing::Alone => {} - Spacing::Joint => joint = true, - } + joint = tt.spacing() == Spacing::Joint; + Display::fmt(tt, f) } - TokenTree::Literal(tt) => write!(f, "{}", tt)?, - } + TokenTree::Literal(tt) => Display::fmt(tt, f), + }? } Ok(())