Skip to content

Commit

Permalink
Prepare for the next proc-macro2 release
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed May 16, 2018
1 parent c1ff101 commit aa29e17
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 29 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "quote"
version = "0.5.2" # don't forget to update html_root_url, version in readme for breaking changes
version = "0.6.0" # don't forget to update html_root_url, version in readme for breaking changes
authors = ["David Tolnay <[email protected]>"]
license = "MIT/Apache-2.0"
description = "Quasi-quoting macro quote!(...)"
Expand All @@ -11,10 +11,13 @@ readme = "README.md"
include = ["Cargo.toml", "src/**/*.rs", "tests/**/*.rs", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]

[dependencies]
proc-macro2 = { version = "0.3", default-features = false }
proc-macro2 = { version = "0.4", default-features = false }

[features]
default = ["proc-macro"]
# Disabling the proc-macro feature removes the dynamic library dependency on
# libproc_macro in the rustc compiler.
proc-macro = ["proc-macro2/proc-macro"]

[patch.crates-io]
proc-macro2 = { git = 'https://github.com/alexcrichton/proc-macro2', branch = 'next' }
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Rust Quasi-Quoting
This crate provides the [`quote!`] macro for turning Rust syntax tree data
structures into tokens of source code.

[`quote!`]: https://docs.rs/quote/0.5/quote/macro.quote.html
[`quote!`]: https://docs.rs/quote/0.6/quote/macro.quote.html

Procedural macros in Rust receive a stream of tokens as input, execute arbitrary
Rust code to determine how to manipulate those tokens, and produce a stream of
Expand All @@ -33,7 +33,7 @@ first support for procedural macros in Rust 1.15.0.*

```toml
[dependencies]
quote = "0.5"
quote = "0.6"
```

```rust
Expand All @@ -50,13 +50,13 @@ should think of `Tokens` as representing a fragment of Rust source code. Call
or call `into()` to stream them as a `TokenStream` back to the compiler in a
procedural macro.

[`quote::Tokens`]: https://docs.rs/quote/0.5/quote/struct.Tokens.html
[`quote::Tokens`]: https://docs.rs/quote/0.6/quote/struct.Tokens.html

Within the `quote!` macro, interpolation is done with `#var`. Any type
implementing the [`quote::ToTokens`] trait can be interpolated. This includes
most Rust primitive types as well as most of the syntax tree types from [`syn`].

[`quote::ToTokens`]: https://docs.rs/quote/0.5/quote/trait.ToTokens.html
[`quote::ToTokens`]: https://docs.rs/quote/0.6/quote/trait.ToTokens.html
[`syn`]: https://github.com/dtolnay/syn

```rust
Expand Down Expand Up @@ -110,7 +110,7 @@ are spanned with [`Span::def_site()`].
A different span can be provided explicitly through the [`quote_spanned!`]
macro.

[`quote_spanned!`]: https://docs.rs/quote/0.5/quote/macro.quote_spanned.html
[`quote_spanned!`]: https://docs.rs/quote/0.6/quote/macro.quote_spanned.html

### Limitations

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
//! An even higher limit may be necessary for especially large invocations.
// Quote types in rustdoc of other crates get linked to here.
#![doc(html_root_url = "https://docs.rs/quote/0.5.2")]
#![doc(html_root_url = "https://docs.rs/quote/0.6.0")]

#[cfg(feature = "proc-macro")]
extern crate proc_macro;
Expand Down
14 changes: 7 additions & 7 deletions src/to_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::Tokens;

use std::borrow::Cow;

use proc_macro2::{Group, Literal, Op, Span, Term, TokenStream, TokenTree};
use proc_macro2::{Group, Literal, Punct, Span, Ident, TokenStream, TokenTree};

/// Types that can be interpolated inside a [`quote!`] invocation.
///
Expand All @@ -18,7 +18,7 @@ pub trait ToTokens {
/// use quote::{Tokens, ToTokens};
///
/// extern crate proc_macro2;
/// use proc_macro2::{TokenTree, Spacing, Span, Op};
/// use proc_macro2::{TokenTree, Spacing, Span, Punct};
///
/// pub struct Path {
/// pub global: bool,
Expand All @@ -30,8 +30,8 @@ pub trait ToTokens {
/// for (i, segment) in self.segments.iter().enumerate() {
/// if i > 0 || self.global {
/// // Double colon `::`
/// tokens.append(Op::new(':', Spacing::Joint));
/// tokens.append(Op::new(':', Spacing::Alone));
/// tokens.append(Punct::new(':', Spacing::Joint));
/// tokens.append(Punct::new(':', Spacing::Alone));
/// }
/// segment.to_tokens(tokens);
/// }
Expand Down Expand Up @@ -138,7 +138,7 @@ impl ToTokens for char {
impl ToTokens for bool {
fn to_tokens(&self, tokens: &mut Tokens) {
let word = if *self { "true" } else { "false" };
tokens.append(Term::new(word, Span::call_site()));
tokens.append(Ident::new(word, Span::call_site()));
}
}

Expand All @@ -148,13 +148,13 @@ impl ToTokens for Group {
}
}

impl ToTokens for Term {
impl ToTokens for Ident {
fn to_tokens(&self, tokens: &mut Tokens) {
tokens.append(self.clone());
}
}

impl ToTokens for Op {
impl ToTokens for Punct {
fn to_tokens(&self, tokens: &mut Tokens) {
tokens.append(self.clone());
}
Expand Down
14 changes: 8 additions & 6 deletions src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,18 @@ fn tt_eq(a: &TokenTree, b: &TokenTree) -> bool {
}
s2.next().is_none()
}
(&TokenTree::Op(ref o1), &TokenTree::Op(ref o2)) => {
o1.op() == o2.op() && match (o1.spacing(), o2.spacing()) {
(&TokenTree::Punct(ref o1), &TokenTree::Punct(ref o2)) => {
o1.as_char() == o2.as_char() && match (o1.spacing(), o2.spacing()) {
(Spacing::Alone, Spacing::Alone) | (Spacing::Joint, Spacing::Joint) => true,
_ => false,
}
}
(&TokenTree::Literal(ref l1), &TokenTree::Literal(ref l2)) => {
l1.to_string() == l2.to_string()
}
(&TokenTree::Term(ref s1), &TokenTree::Term(ref s2)) => s1.as_str() == s2.as_str(),
(&TokenTree::Ident(ref s1), &TokenTree::Ident(ref s2)) => {
s1.to_string() == s2.to_string()
}
_ => false,
}
}
Expand Down Expand Up @@ -243,16 +245,16 @@ fn tt_hash<H: Hasher>(tt: &TokenTree, h: &mut H) {
}
0xffu8.hash(h); // terminator w/ a variant we don't normally hash
}
TokenTree::Op(ref t) => {
TokenTree::Punct(ref t) => {
1u8.hash(h);
t.op().hash(h);
t.as_char().hash(h);
match t.spacing() {
Spacing::Alone => 0u8.hash(h),
Spacing::Joint => 1u8.hash(h),
}
}
TokenTree::Literal(ref lit) => (2u8, lit.to_string()).hash(h),
TokenTree::Term(ref word) => (3u8, word.as_str()).hash(h),
TokenTree::Ident(ref word) => (3u8, word.to_string()).hash(h),
}
}

Expand Down
16 changes: 8 additions & 8 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ extern crate proc_macro2;
#[macro_use]
extern crate quote;

use proc_macro2::{Span, Term};
use proc_macro2::{Span, Ident};

struct X;

impl quote::ToTokens for X {
fn to_tokens(&self, tokens: &mut quote::Tokens) {
tokens.append(Term::new("X", Span::call_site()));
tokens.append(Ident::new("X", Span::call_site()));
}
}

Expand Down Expand Up @@ -182,8 +182,8 @@ fn test_string() {

#[test]
fn test_ident() {
let foo = Term::new("Foo", Span::call_site());
let bar = Term::new(&format!("Bar{}", 7), Span::call_site());
let foo = Ident::new("Foo", Span::call_site());
let bar = Ident::new(&format!("Bar{}", 7), Span::call_site());
let tokens = quote!(struct #foo; enum #bar {});
let expected = "struct Foo ; enum Bar7 { }";
assert_eq!(expected, tokens.to_string());
Expand Down Expand Up @@ -257,9 +257,9 @@ fn test_box_str() {

#[test]
fn test_cow() {
let owned: Cow<Term> = Cow::Owned(Term::new("owned", Span::call_site()));
let owned: Cow<Ident> = Cow::Owned(Ident::new("owned", Span::call_site()));

let ident = Term::new("borrowed", Span::call_site());
let ident = Ident::new("borrowed", Span::call_site());
let borrowed = Cow::Borrowed(&ident);

let tokens = quote! { #owned #borrowed };
Expand All @@ -268,8 +268,8 @@ fn test_cow() {

#[test]
fn test_closure() {
fn field_i(i: usize) -> Term {
Term::new(&format!("__field{}", i), Span::call_site())
fn field_i(i: usize) -> Ident {
Ident::new(&format!("__field{}", i), Span::call_site())
}

let fields = (0usize..3)
Expand Down

0 comments on commit aa29e17

Please sign in to comment.