1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
use proc_macro::{Ident, Literal, TokenStream, TokenTree}; use std::iter; pub(crate) trait ToTokens { fn to_tokens(&self, tokens: &mut TokenStream); fn to_token_stream(&self) -> TokenStream { let mut tokens = TokenStream::new(); self.to_tokens(&mut tokens); tokens } } impl ToTokens for Ident { fn to_tokens(&self, tokens: &mut TokenStream) { tokens.extend(iter::once(TokenTree::Ident(self.clone()))); } } impl ToTokens for Literal { fn to_tokens(&self, tokens: &mut TokenStream) { tokens.extend(iter::once(TokenTree::Literal(self.clone()))); } } impl ToTokens for TokenTree { fn to_tokens(&self, tokens: &mut TokenStream) { tokens.extend(iter::once(self.clone())); } } impl ToTokens for TokenStream { fn to_tokens(&self, tokens: &mut TokenStream) { tokens.extend(self.clone()); } }