From 21d0539fcf698af343e1e80516208dcc3c3b9d15 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 4 Sep 2016 13:28:30 -0700 Subject: [PATCH] Add append_separated --- src/lib.rs | 7 +------ src/tokens.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5913cc4..68d2664 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,12 +43,7 @@ macro_rules! quote_each_token { }; ($tokens:ident # ( $first:ident ) $sep:tt * $($rest:tt)*) => { - for (_i, _v) in $first.iter().enumerate() { - if _i > 0 { - $tokens.append(stringify!($sep)); - } - _v.to_tokens(&mut $tokens); - } + $tokens.append_separated($first, stringify!($sep)); quote_each_token!($tokens $($rest)*); }; diff --git a/src/tokens.rs b/src/tokens.rs index 1f972f6..27c891b 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -1,3 +1,4 @@ +use super::ToTokens; use std::fmt::{self, Display}; #[derive(Debug)] @@ -12,6 +13,18 @@ impl Tokens { self.0.push_str(token); self.0.push(' '); } + + pub fn append_separated(&mut self, iter: I, sep: &str) + where T: ToTokens, + I: IntoIterator + { + for (i, token) in iter.into_iter().enumerate() { + if i > 0 { + self.append(sep); + } + token.to_tokens(self); + } + } } impl Default for Tokens {