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 authored and dtolnay committed May 17, 2018
1 parent c1ff101 commit 9604c63
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 256 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ rust:

script:
- cargo test
- cargo test --no-default-features
8 changes: 1 addition & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,4 @@ 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 }

[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"]
proc-macro2 = { version = "0.4", default-features = false }
30 changes: 15 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ extern crate proc_macro;
extern crate proc_macro2;

mod tokens;
pub use tokens::Tokens;
pub use tokens::TokenStreamExt;

mod to_tokens;
pub use to_tokens::ToTokens;
Expand All @@ -111,9 +111,9 @@ pub mod __rt {
pub use proc_macro2::*;

// Not public API.
pub fn parse(tokens: &mut ::Tokens, span: Span, s: &str) {
pub fn parse(tokens: &mut TokenStream, span: Span, s: &str) {
let s: TokenStream = s.parse().expect("invalid token stream");
tokens.append_all(s.into_iter().map(|mut t| {
tokens.extend(s.into_iter().map(|mut t| {
t.set_span(span);
t
}));
Expand Down Expand Up @@ -266,8 +266,8 @@ macro_rules! quote {
/// # extern crate quote;
/// # extern crate proc_macro2;
/// #
/// # use quote::{Tokens, ToTokens};
/// # use proc_macro2::Span;
/// # use quote::{TokenStreamExt, ToTokens};
/// # use proc_macro2::{Span, TokenStream};
/// #
/// # struct Type;
/// #
Expand All @@ -278,7 +278,7 @@ macro_rules! quote {
/// # }
/// #
/// # impl ToTokens for Type {
/// # fn to_tokens(&self, _tokens: &mut Tokens) {}
/// # fn to_tokens(&self, _tokens: &mut TokenStream) {}
/// # }
/// #
/// # fn main() {
Expand Down Expand Up @@ -314,7 +314,7 @@ macro_rules! quote {
macro_rules! quote_spanned {
($span:expr=> $($tt:tt)*) => {
{
let mut _s = $crate::Tokens::new();
let mut _s = $crate::__rt::TokenStream::empty();
let _span = $span;
quote_each_token!(_s _span $($tt)*);
_s
Expand Down Expand Up @@ -452,13 +452,13 @@ macro_rules! quote_each_token {

($tokens:ident $span:ident # [ $($inner:tt)* ] $($rest:tt)*) => {
quote_each_token!($tokens $span #);
$tokens.append({
$tokens.extend({
let mut g = $crate::__rt::Group::new(
$crate::__rt::Delimiter::Bracket,
quote_spanned!($span=> $($inner)*).into(),
);
g.set_span($span);
g
Some($crate::__rt::TokenTree::from(g))
});
quote_each_token!($tokens $span $($rest)*);
};
Expand All @@ -469,37 +469,37 @@ macro_rules! quote_each_token {
};

($tokens:ident $span:ident ( $($first:tt)* ) $($rest:tt)*) => {
$tokens.append({
$tokens.extend({
let mut g = $crate::__rt::Group::new(
$crate::__rt::Delimiter::Parenthesis,
quote_spanned!($span=> $($first)*).into(),
);
g.set_span($span);
g
Some($crate::__rt::TokenTree::from(g))
});
quote_each_token!($tokens $span $($rest)*);
};

($tokens:ident $span:ident [ $($first:tt)* ] $($rest:tt)*) => {
$tokens.append({
$tokens.extend({
let mut g = $crate::__rt::Group::new(
$crate::__rt::Delimiter::Bracket,
quote_spanned!($span=> $($first)*).into(),
);
g.set_span($span);
g
Some($crate::__rt::TokenTree::from(g))
});
quote_each_token!($tokens $span $($rest)*);
};

($tokens:ident $span:ident { $($first:tt)* } $($rest:tt)*) => {
$tokens.append({
$tokens.extend({
let mut g = $crate::__rt::Group::new(
$crate::__rt::Delimiter::Brace,
quote_spanned!($span=> $($first)*).into(),
);
g.set_span($span);
g
Some($crate::__rt::TokenTree::from(g))
});
quote_each_token!($tokens $span $($rest)*);
};
Expand Down
58 changes: 29 additions & 29 deletions src/to_tokens.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::Tokens;
use super::TokenStreamExt;

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 @@ -15,23 +15,23 @@ pub trait ToTokens {
///
/// ```
/// extern crate quote;
/// use quote::{Tokens, ToTokens};
/// use quote::{TokenStreamExt, ToTokens};
///
/// extern crate proc_macro2;
/// use proc_macro2::{TokenTree, Spacing, Span, Op};
/// use proc_macro2::{TokenTree, Spacing, Span, Punct, TokenStream};
///
/// pub struct Path {
/// pub global: bool,
/// pub segments: Vec<PathSegment>,
/// }
///
/// impl ToTokens for Path {
/// fn to_tokens(&self, tokens: &mut Tokens) {
/// fn to_tokens(&self, tokens: &mut TokenStream) {
/// 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 All @@ -41,71 +41,71 @@ pub trait ToTokens {
/// # pub struct PathSegment;
/// #
/// # impl ToTokens for PathSegment {
/// # fn to_tokens(&self, tokens: &mut Tokens) {
/// # fn to_tokens(&self, tokens: &mut TokenStream) {
/// # unimplemented!()
/// # }
/// # }
/// #
/// # fn main() {}
/// ```
fn to_tokens(&self, tokens: &mut Tokens);
fn to_tokens(&self, tokens: &mut TokenStream);

/// Convert `self` directly into a `Tokens` object.
///
/// This method is implicitly implemented using `to_tokens`, and acts as a
/// convenience method for consumers of the `ToTokens` trait.
fn into_tokens(self) -> Tokens
fn into_token_stream(self) -> TokenStream
where
Self: Sized,
{
let mut tokens = Tokens::new();
let mut tokens = TokenStream::empty();
self.to_tokens(&mut tokens);
tokens
}
}

impl<'a, T: ?Sized + ToTokens> ToTokens for &'a T {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
(**self).to_tokens(tokens);
}
}

impl<'a, T: ?Sized + ToOwned + ToTokens> ToTokens for Cow<'a, T> {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
(**self).to_tokens(tokens);
}
}

impl<T: ?Sized + ToTokens> ToTokens for Box<T> {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
(**self).to_tokens(tokens);
}
}

impl<T: ToTokens> ToTokens for Option<T> {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
if let Some(ref t) = *self {
t.to_tokens(tokens);
}
}
}

impl ToTokens for str {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(Literal::string(self));
}
}

impl ToTokens for String {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.as_str().to_tokens(tokens);
}
}

macro_rules! primitive {
($($t:ident => $name:ident)*) => ($(
impl ToTokens for $t {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(Literal::$name(*self));
}
}
Expand All @@ -130,50 +130,50 @@ primitive! {
}

impl ToTokens for char {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append(Literal::character(*self));
}
}

impl ToTokens for bool {
fn to_tokens(&self, tokens: &mut Tokens) {
fn to_tokens(&self, tokens: &mut TokenStream) {
let word = if *self { "true" } else { "false" };
tokens.append(Term::new(word, Span::call_site()));
tokens.append(Ident::new(word, Span::call_site()));
}
}

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

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

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

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

impl ToTokens for TokenTree {
fn to_tokens(&self, dst: &mut Tokens) {
fn to_tokens(&self, dst: &mut TokenStream) {
dst.append(self.clone());
}
}

impl ToTokens for TokenStream {
fn to_tokens(&self, dst: &mut Tokens) {
fn to_tokens(&self, dst: &mut TokenStream) {
dst.append_all(self.clone().into_iter());
}
}
Loading

0 comments on commit 9604c63

Please sign in to comment.