Skip to content

Commit

Permalink
Eliminate references to Tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed May 21, 2018
1 parent e62263f commit 68cd090
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ extern crate quote;
## Syntax

The quote crate provides a [`quote!`] macro within which you can write Rust code
that gets packaged into a [`quote::Tokens`] and can be treated as data. You
should think of `Tokens` as representing a fragment of Rust source code. Call
`to_string()` on a `Tokens` to get back the fragment of source code as a string,
or call `into()` to stream them as a `TokenStream` back to the compiler in a
procedural macro.
that gets packaged into a [`TokenStream`] and can be treated as data. You should
think of `TokenStream` as representing a fragment of Rust source code. This type
can be returned directly back to the compiler by a procedural macro to get
compiled into the caller's crate.

[`quote::Tokens`]: https://docs.rs/quote/0.6/quote/struct.Tokens.html
[`TokenStream`]: https://docs.rs/proc-macro2/0.4/proc_macro2/struct.TokenStream.html

Within the `quote!` macro, interpolation is done with `#var`. Any type
implementing the [`quote::ToTokens`] trait can be interpolated. This includes
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ pub mod __rt {
/// The whole point.
///
/// Performs variable interpolation against the input and produces it as
/// [`Tokens`]. For returning tokens to the compiler in a procedural macro, use
/// [`TokenStream`]. For returning tokens to the compiler in a procedural macro, use
/// `into()` to build a `TokenStream`.
///
/// [`Tokens`]: struct.Tokens.html
/// [`TokenStream`]: https://docs.rs/proc-macro2/0.4/proc_macro2/struct.TokenStream.html
///
/// # Interpolation
///
Expand Down
11 changes: 9 additions & 2 deletions src/to_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ use proc_macro2::{Group, Ident, Literal, Punct, Span, TokenStream, TokenTree};
///
/// [`quote!`]: macro.quote.html
pub trait ToTokens {
/// Write `self` to the given `Tokens`.
/// 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`:
Expand Down Expand Up @@ -50,7 +57,7 @@ pub trait ToTokens {
/// ```
fn to_tokens(&self, tokens: &mut TokenStream);

/// Convert `self` directly into a `Tokens` object.
/// 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.
Expand Down
4 changes: 2 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ impl quote::ToTokens for X {
fn test_quote_impl() {
let tokens = quote! {
impl<'a, T: ToTokens> ToTokens for &'a T {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
(**self).to_tokens(tokens)
}
}
};

let expected = concat!(
"impl < 'a , T : ToTokens > ToTokens for & 'a T { ",
"fn to_tokens ( & self , tokens : & mut Tokens ) { ",
"fn to_tokens ( & self , tokens : & mut TokenStream ) { ",
"( * * self ) . to_tokens ( tokens ) ",
"} ",
"}"
Expand Down

0 comments on commit 68cd090

Please sign in to comment.