// SPDX-License-Identifier: Apache-2.0 OR MIT12use super::ToTokens;3use core::iter;4use proc_macro2::{TokenStream, TokenTree};56/// TokenStream extension trait with methods for appending tokens.7///8/// This trait is sealed and cannot be implemented outside of the `quote` crate.9pub trait TokenStreamExt: private::Sealed {10/// For use by `ToTokens` implementations.11///12/// Appends the token specified to this list of tokens.13fn append<U>(&mut self, token: U)14where15U: Into<TokenTree>;1617/// For use by `ToTokens` implementations.18///19/// ```20/// # use quote::{quote, TokenStreamExt, ToTokens};21/// # use proc_macro2::TokenStream;22/// #23/// struct X;24///25/// impl ToTokens for X {26/// fn to_tokens(&self, tokens: &mut TokenStream) {27/// tokens.append_all(&[true, false]);28/// }29/// }30///31/// let tokens = quote!(#X);32/// assert_eq!(tokens.to_string(), "true false");33/// ```34fn append_all<I>(&mut self, iter: I)35where36I: IntoIterator,37I::Item: ToTokens;3839/// For use by `ToTokens` implementations.40///41/// Appends all of the items in the iterator `I`, separated by the tokens42/// `U`.43fn append_separated<I, U>(&mut self, iter: I, op: U)44where45I: IntoIterator,46I::Item: ToTokens,47U: ToTokens;4849/// For use by `ToTokens` implementations.50///51/// Appends all tokens in the iterator `I`, appending `U` after each52/// element, including after the last element of the iterator.53fn append_terminated<I, U>(&mut self, iter: I, term: U)54where55I: IntoIterator,56I::Item: ToTokens,57U: ToTokens;58}5960impl TokenStreamExt for TokenStream {61fn append<U>(&mut self, token: U)62where63U: Into<TokenTree>,64{65self.extend(iter::once(token.into()));66}6768fn append_all<I>(&mut self, iter: I)69where70I: IntoIterator,71I::Item: ToTokens,72{73for token in iter {74token.to_tokens(self);75}76}7778fn append_separated<I, U>(&mut self, iter: I, op: U)79where80I: IntoIterator,81I::Item: ToTokens,82U: ToTokens,83{84for (i, token) in iter.into_iter().enumerate() {85if i > 0 {86op.to_tokens(self);87}88token.to_tokens(self);89}90}9192fn append_terminated<I, U>(&mut self, iter: I, term: U)93where94I: IntoIterator,95I::Item: ToTokens,96U: ToTokens,97{98for token in iter {99token.to_tokens(self);100term.to_tokens(self);101}102}103}104105mod private {106use proc_macro2::TokenStream;107108pub trait Sealed {}109110impl Sealed for TokenStream {}111}112113114