Skip to content

Commit d9f1d6b

Browse files
committed
auto merge of #15732 : bgamari/rust/to-tokens, r=alexcrichton
Here I add a `ToTokens` impl for `Attribute_` and `Option<T>`, as well as generalize the impl for `Vec<T>`
2 parents 5980aa0 + e4f8cec commit d9f1d6b

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/libsyntax/ext/base.rs

+3
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ pub fn syntax_expander_table() -> SyntaxEnv {
371371
syntax_expanders.insert(intern("quote_ty"),
372372
builtin_normal_expander(
373373
ext::quote::expand_quote_ty));
374+
syntax_expanders.insert(intern("quote_method"),
375+
builtin_normal_expander(
376+
ext::quote::expand_quote_method));
374377
syntax_expanders.insert(intern("quote_item"),
375378
builtin_normal_expander(
376379
ext::quote::expand_quote_item));

src/libsyntax/ext/quote.rs

+32-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ pub mod rt {
5454
}
5555
}
5656

57-
impl ToTokens for Vec<TokenTree> {
58-
fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
59-
(*self).clone()
57+
impl<T: ToTokens> ToTokens for Vec<T> {
58+
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
59+
let a = self.iter().flat_map(|t| t.to_tokens(cx).move_iter());
60+
FromIterator::from_iter(a)
6061
}
6162
}
6263

@@ -67,6 +68,15 @@ pub mod rt {
6768
}
6869
}
6970

71+
impl<T: ToTokens> ToTokens for Option<T> {
72+
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
73+
match self {
74+
&Some(ref t) => t.to_tokens(cx),
75+
&None => Vec::new(),
76+
}
77+
}
78+
}
79+
7080
/* Should be (when bugs in default methods are fixed):
7181
7282
trait ToSource : ToTokens {
@@ -133,11 +143,18 @@ pub mod rt {
133143
impl_to_source!(ast::Arg, arg_to_string)
134144
impl_to_source!(Generics, generics_to_string)
135145
impl_to_source!(Gc<ast::Item>, item_to_string)
146+
impl_to_source!(Gc<ast::Method>, method_to_string)
136147
impl_to_source!(Gc<ast::Expr>, expr_to_string)
137148
impl_to_source!(Gc<ast::Pat>, pat_to_string)
138149
impl_to_source_slice!(ast::Ty, ", ")
139150
impl_to_source_slice!(Gc<ast::Item>, "\n\n")
140151

152+
impl ToSource for ast::Attribute_ {
153+
fn to_source(&self) -> String {
154+
pprust::attribute_to_string(&dummy_spanned(*self))
155+
}
156+
}
157+
141158
impl<'a> ToSource for &'a str {
142159
fn to_source(&self) -> String {
143160
let lit = dummy_spanned(ast::LitStr(
@@ -222,13 +239,15 @@ pub mod rt {
222239
impl_to_tokens!(ast::Ident)
223240
impl_to_tokens!(Gc<ast::Item>)
224241
impl_to_tokens!(Gc<ast::Pat>)
242+
impl_to_tokens!(Gc<ast::Method>)
225243
impl_to_tokens_lifetime!(&'a [Gc<ast::Item>])
226244
impl_to_tokens!(ast::Ty)
227245
impl_to_tokens_lifetime!(&'a [ast::Ty])
228246
impl_to_tokens!(Generics)
229247
impl_to_tokens!(Gc<ast::Expr>)
230248
impl_to_tokens!(ast::Block)
231249
impl_to_tokens!(ast::Arg)
250+
impl_to_tokens!(ast::Attribute_)
232251
impl_to_tokens_lifetime!(&'a str)
233252
impl_to_tokens!(())
234253
impl_to_tokens!(char)
@@ -336,6 +355,16 @@ pub fn expand_quote_ty(cx: &mut ExtCtxt,
336355
base::MacExpr::new(expanded)
337356
}
338357

358+
pub fn expand_quote_method(cx: &mut ExtCtxt,
359+
sp: Span,
360+
tts: &[ast::TokenTree])
361+
-> Box<base::MacResult> {
362+
let e_param_colons = cx.expr_none(sp);
363+
let expanded = expand_parse_call(cx, sp, "parse_method",
364+
vec!(e_param_colons), tts);
365+
base::MacExpr::new(expanded)
366+
}
367+
339368
pub fn expand_quote_stmt(cx: &mut ExtCtxt,
340369
sp: Span,
341370
tts: &[ast::TokenTree])

0 commit comments

Comments
 (0)