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>`
This commit is contained in:
commit
d9f1d6b7f6
|
@ -371,6 +371,9 @@ pub fn syntax_expander_table() -> SyntaxEnv {
|
||||||
syntax_expanders.insert(intern("quote_ty"),
|
syntax_expanders.insert(intern("quote_ty"),
|
||||||
builtin_normal_expander(
|
builtin_normal_expander(
|
||||||
ext::quote::expand_quote_ty));
|
ext::quote::expand_quote_ty));
|
||||||
|
syntax_expanders.insert(intern("quote_method"),
|
||||||
|
builtin_normal_expander(
|
||||||
|
ext::quote::expand_quote_method));
|
||||||
syntax_expanders.insert(intern("quote_item"),
|
syntax_expanders.insert(intern("quote_item"),
|
||||||
builtin_normal_expander(
|
builtin_normal_expander(
|
||||||
ext::quote::expand_quote_item));
|
ext::quote::expand_quote_item));
|
||||||
|
|
|
@ -54,9 +54,10 @@ pub mod rt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToTokens for Vec<TokenTree> {
|
impl<T: ToTokens> ToTokens for Vec<T> {
|
||||||
fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
|
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
|
||||||
(*self).clone()
|
let a = self.iter().flat_map(|t| t.to_tokens(cx).move_iter());
|
||||||
|
FromIterator::from_iter(a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,6 +68,15 @@ pub mod rt {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: ToTokens> ToTokens for Option<T> {
|
||||||
|
fn to_tokens(&self, cx: &ExtCtxt) -> Vec<TokenTree> {
|
||||||
|
match self {
|
||||||
|
&Some(ref t) => t.to_tokens(cx),
|
||||||
|
&None => Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Should be (when bugs in default methods are fixed):
|
/* Should be (when bugs in default methods are fixed):
|
||||||
|
|
||||||
trait ToSource : ToTokens {
|
trait ToSource : ToTokens {
|
||||||
|
@ -133,11 +143,18 @@ pub mod rt {
|
||||||
impl_to_source!(ast::Arg, arg_to_string)
|
impl_to_source!(ast::Arg, arg_to_string)
|
||||||
impl_to_source!(Generics, generics_to_string)
|
impl_to_source!(Generics, generics_to_string)
|
||||||
impl_to_source!(Gc<ast::Item>, item_to_string)
|
impl_to_source!(Gc<ast::Item>, item_to_string)
|
||||||
|
impl_to_source!(Gc<ast::Method>, method_to_string)
|
||||||
impl_to_source!(Gc<ast::Expr>, expr_to_string)
|
impl_to_source!(Gc<ast::Expr>, expr_to_string)
|
||||||
impl_to_source!(Gc<ast::Pat>, pat_to_string)
|
impl_to_source!(Gc<ast::Pat>, pat_to_string)
|
||||||
impl_to_source_slice!(ast::Ty, ", ")
|
impl_to_source_slice!(ast::Ty, ", ")
|
||||||
impl_to_source_slice!(Gc<ast::Item>, "\n\n")
|
impl_to_source_slice!(Gc<ast::Item>, "\n\n")
|
||||||
|
|
||||||
|
impl ToSource for ast::Attribute_ {
|
||||||
|
fn to_source(&self) -> String {
|
||||||
|
pprust::attribute_to_string(&dummy_spanned(*self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> ToSource for &'a str {
|
impl<'a> ToSource for &'a str {
|
||||||
fn to_source(&self) -> String {
|
fn to_source(&self) -> String {
|
||||||
let lit = dummy_spanned(ast::LitStr(
|
let lit = dummy_spanned(ast::LitStr(
|
||||||
|
@ -222,6 +239,7 @@ pub mod rt {
|
||||||
impl_to_tokens!(ast::Ident)
|
impl_to_tokens!(ast::Ident)
|
||||||
impl_to_tokens!(Gc<ast::Item>)
|
impl_to_tokens!(Gc<ast::Item>)
|
||||||
impl_to_tokens!(Gc<ast::Pat>)
|
impl_to_tokens!(Gc<ast::Pat>)
|
||||||
|
impl_to_tokens!(Gc<ast::Method>)
|
||||||
impl_to_tokens_lifetime!(&'a [Gc<ast::Item>])
|
impl_to_tokens_lifetime!(&'a [Gc<ast::Item>])
|
||||||
impl_to_tokens!(ast::Ty)
|
impl_to_tokens!(ast::Ty)
|
||||||
impl_to_tokens_lifetime!(&'a [ast::Ty])
|
impl_to_tokens_lifetime!(&'a [ast::Ty])
|
||||||
|
@ -229,6 +247,7 @@ pub mod rt {
|
||||||
impl_to_tokens!(Gc<ast::Expr>)
|
impl_to_tokens!(Gc<ast::Expr>)
|
||||||
impl_to_tokens!(ast::Block)
|
impl_to_tokens!(ast::Block)
|
||||||
impl_to_tokens!(ast::Arg)
|
impl_to_tokens!(ast::Arg)
|
||||||
|
impl_to_tokens!(ast::Attribute_)
|
||||||
impl_to_tokens_lifetime!(&'a str)
|
impl_to_tokens_lifetime!(&'a str)
|
||||||
impl_to_tokens!(())
|
impl_to_tokens!(())
|
||||||
impl_to_tokens!(char)
|
impl_to_tokens!(char)
|
||||||
|
@ -336,6 +355,16 @@ pub fn expand_quote_ty(cx: &mut ExtCtxt,
|
||||||
base::MacExpr::new(expanded)
|
base::MacExpr::new(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn expand_quote_method(cx: &mut ExtCtxt,
|
||||||
|
sp: Span,
|
||||||
|
tts: &[ast::TokenTree])
|
||||||
|
-> Box<base::MacResult> {
|
||||||
|
let e_param_colons = cx.expr_none(sp);
|
||||||
|
let expanded = expand_parse_call(cx, sp, "parse_method",
|
||||||
|
vec!(e_param_colons), tts);
|
||||||
|
base::MacExpr::new(expanded)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn expand_quote_stmt(cx: &mut ExtCtxt,
|
pub fn expand_quote_stmt(cx: &mut ExtCtxt,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
tts: &[ast::TokenTree])
|
tts: &[ast::TokenTree])
|
||||||
|
|
Loading…
Reference in New Issue