Add append_separated

This commit is contained in:
David Tolnay 2016-09-04 13:28:30 -07:00
parent 5232bc9bb8
commit 21d0539fcf
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 14 additions and 6 deletions

View File

@ -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)*);
};

View File

@ -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<T, I>(&mut self, iter: I, sep: &str)
where T: ToTokens,
I: IntoIterator<Item = T>
{
for (i, token) in iter.into_iter().enumerate() {
if i > 0 {
self.append(sep);
}
token.to_tokens(self);
}
}
}
impl Default for Tokens {