Skip to content

Commit

Permalink
Add append_separated
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Sep 4, 2016
1 parent 5232bc9 commit 21d0539
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)*);
};

Expand Down
13 changes: 13 additions & 0 deletions src/tokens.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::ToTokens;
use std::fmt::{self, Display};

#[derive(Debug)]
Expand All @@ -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 {
Expand Down

0 comments on commit 21d0539

Please sign in to comment.