usesuper::TokenStreamExt; use alloc::borrow::Cow; use alloc::rc::Rc; use core::iter; use proc_macro2::{Group, Ident, Literal, Punct, Span, TokenStream, TokenTree};
/// Types that can be interpolated inside a `quote!` invocation. /// /// [`quote!`]: macro.quote.html pubtrait ToTokens { /// Write `self` to the given `TokenStream`. /// /// The token append methods provided by the [`TokenStreamExt`] extension /// trait may be useful for implementing `ToTokens`. /// /// [`TokenStreamExt`]: trait.TokenStreamExt.html /// /// # Example /// /// Example implementation for a struct representing Rust paths like /// `std::cmp::PartialEq`: /// /// ``` /// use proc_macro2::{TokenTree, Spacing, Span, Punct, TokenStream}; /// use quote::{TokenStreamExt, ToTokens}; /// /// pub struct Path { /// pub global: bool, /// pub segments: Vec<PathSegment>, /// } /// /// impl ToTokens for Path { /// fn to_tokens(&self, tokens: &mut TokenStream) { /// for (i, segment) in self.segments.iter().enumerate() { /// if i > 0 || self.global { /// // Double colon `::` /// tokens.append(Punct::new(':', Spacing::Joint)); /// tokens.append(Punct::new(':', Spacing::Alone)); /// } /// segment.to_tokens(tokens); /// } /// } /// } /// # /// # pub struct PathSegment; /// # /// # impl ToTokens for PathSegment { /// # fn to_tokens(&self, tokens: &mut TokenStream) { /// # unimplemented!() /// # } /// # } /// ``` fn to_tokens(&self, tokens: &mut TokenStream);
/// Convert `self` directly into a `TokenStream` object. /// /// This method is implicitly implemented using `to_tokens`, and acts as a /// convenience method for consumers of the `ToTokens` trait. fn to_token_stream(&self) -> TokenStream { letmut tokens = TokenStream::new(); self.to_tokens(&mut tokens);
tokens
}
/// Convert `self` directly into a `TokenStream` object. /// /// This method is implicitly implemented using `to_tokens`, and acts as a /// convenience method for consumers of the `ToTokens` trait. fn into_token_stream(self) -> TokenStream where Self: Sized,
{ self.to_token_stream()
}
}
impl<'a, T: ?Sized + ToTokens> ToTokens for &'a T { fn to_tokens(&self, tokens: &mut TokenStream) {
(**self).to_tokens(tokens);
}
}
impl<'a, T: ?Sized + ToTokens> ToTokens for &'a mut T { fn to_tokens(&self, tokens: &mut TokenStream) {
(**self).to_tokens(tokens);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.