Reduce surface area of Tokens

This commit is contained in:
David Tolnay 2017-12-31 18:16:57 -05:00
parent 1454aee700
commit 0625b2fe07
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 1 additions and 45 deletions

View File

@ -2,8 +2,7 @@ use super::ToTokens;
use std::fmt::{self, Debug, Display};
use proc_macro;
use proc_macro2::{Span, Term, TokenNode, TokenStream, TokenTree};
use proc_macro2::Delimiter;
use proc_macro2::{TokenStream, TokenTree};
/// Tokens produced by a `quote!(...)` invocation.
#[derive(Clone)]
@ -27,39 +26,6 @@ impl Tokens {
self.tts.push(token.into());
}
/// Add `tokens` into `self`.
pub fn append_tokens<T: ToTokens>(&mut self, tokens: T) {
tokens.to_tokens(self)
}
/// Add the symbol specified to this list of tokens.
pub fn append_sym(&mut self, sym: &str, span: Span) {
self.append(TokenTree {
span: span,
kind: TokenNode::Term(Term::intern(sym)),
});
}
pub fn append_delimited<F, R>(&mut self, delim: &str, span: Span, f: F) -> R
where
F: FnOnce(&mut Tokens) -> R,
{
let delim = match delim {
"(" => Delimiter::Parenthesis,
"[" => Delimiter::Bracket,
"{" => Delimiter::Brace,
" " => Delimiter::None,
_ => panic!("unknown delimiter: {}", delim),
};
let mut child = Tokens::new();
let ret = f(&mut child);
self.append(TokenTree {
span: span,
kind: TokenNode::Group(delim, child.into()),
});
ret
}
/// For use by `ToTokens` implementations.
///
/// ```

View File

@ -40,16 +40,6 @@ fn test_quote_impl() {
assert_eq!(expected, tokens.to_string());
}
#[test]
fn test_append_tokens() {
let mut tokens = quote!(let x =);
tokens.append_tokens(quote!("Hello World!";));
let expected = "let x = \"Hello World!\" ;";
assert_eq!(expected, tokens.to_string());
}
#[test]
fn test_substitution() {
let x = X;