Skip to content

Track upstream proc_macro changes #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "proc-macro2"
version = "0.3.8" # remember to update html_root_url
version = "0.4.0" # remember to update html_root_url
authors = ["Alex Crichton <[email protected]>"]
license = "MIT/Apache-2.0"
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ itself. Usage is done via:

```toml
[dependencies]
proc-macro2 = "0.3"
proc-macro2 = "0.4"
```

followed by
Expand Down Expand Up @@ -57,7 +57,7 @@ You can enable this feature via:

```toml
[dependencies]
proc-macro2 = { version = "0.3", features = ["nightly"] }
proc-macro2 = { version = "0.4", features = ["nightly"] }
```


Expand Down
118 changes: 79 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@
//! [ts]: https://doc.rust-lang.org/proc_macro/struct.TokenStream.html

// Proc-macro2 types in rustdoc of other crates get linked to here.
#![doc(html_root_url = "https://docs.rs/proc-macro2/0.3.8")]
#![doc(html_root_url = "https://docs.rs/proc-macro2/0.4.0")]
#![cfg_attr(feature = "nightly", feature(proc_macro))]

#[cfg(feature = "proc-macro")]
extern crate proc_macro;
extern crate unicode_xid;

use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::iter::FromIterator;
use std::marker;
use std::rc::Rc;
Expand Down Expand Up @@ -127,6 +129,12 @@ impl From<TokenStream> for proc_macro::TokenStream {
}
}

impl Extend<TokenTree> for TokenStream {
fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, streams: I) {
self.inner.extend(streams)
}
}

impl FromIterator<TokenTree> for TokenStream {
fn from_iter<I: IntoIterator<Item = TokenTree>>(streams: I) -> Self {
TokenStream::_new(streams.into_iter().collect())
Expand Down Expand Up @@ -284,26 +292,26 @@ impl fmt::Debug for Span {
#[derive(Clone)]
pub enum TokenTree {
Group(Group),
Term(Term),
Op(Op),
Ident(Ident),
Punct(Punct),
Literal(Literal),
}

impl TokenTree {
pub fn span(&self) -> Span {
match *self {
TokenTree::Group(ref t) => t.span(),
TokenTree::Term(ref t) => t.span(),
TokenTree::Op(ref t) => t.span(),
TokenTree::Ident(ref t) => t.span(),
TokenTree::Punct(ref t) => t.span(),
TokenTree::Literal(ref t) => t.span(),
}
}

pub fn set_span(&mut self, span: Span) {
match *self {
TokenTree::Group(ref mut t) => t.set_span(span),
TokenTree::Term(ref mut t) => t.set_span(span),
TokenTree::Op(ref mut t) => t.set_span(span),
TokenTree::Ident(ref mut t) => t.set_span(span),
TokenTree::Punct(ref mut t) => t.set_span(span),
TokenTree::Literal(ref mut t) => t.set_span(span),
}
}
Expand All @@ -315,15 +323,15 @@ impl From<Group> for TokenTree {
}
}

impl From<Term> for TokenTree {
fn from(g: Term) -> TokenTree {
TokenTree::Term(g)
impl From<Ident> for TokenTree {
fn from(g: Ident) -> TokenTree {
TokenTree::Ident(g)
}
}

impl From<Op> for TokenTree {
fn from(g: Op) -> TokenTree {
TokenTree::Op(g)
impl From<Punct> for TokenTree {
fn from(g: Punct) -> TokenTree {
TokenTree::Punct(g)
}
}

Expand All @@ -337,8 +345,8 @@ impl fmt::Display for TokenTree {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
TokenTree::Group(ref t) => t.fmt(f),
TokenTree::Term(ref t) => t.fmt(f),
TokenTree::Op(ref t) => t.fmt(f),
TokenTree::Ident(ref t) => t.fmt(f),
TokenTree::Punct(ref t) => t.fmt(f),
TokenTree::Literal(ref t) => t.fmt(f),
}
}
Expand All @@ -350,8 +358,8 @@ impl fmt::Debug for TokenTree {
// so don't bother with an extra layer of indirection
match *self {
TokenTree::Group(ref t) => t.fmt(f),
TokenTree::Term(ref t) => t.fmt(f),
TokenTree::Op(ref t) => t.fmt(f),
TokenTree::Ident(ref t) => t.fmt(f),
TokenTree::Punct(ref t) => t.fmt(f),
TokenTree::Literal(ref t) => t.fmt(f),
}
}
Expand Down Expand Up @@ -415,8 +423,8 @@ impl fmt::Debug for Group {
}
}

#[derive(Copy, Clone)]
pub struct Op {
#[derive(Clone)]
pub struct Punct {
op: char,
spacing: Spacing,
span: Span,
Expand All @@ -428,16 +436,16 @@ pub enum Spacing {
Joint,
}

impl Op {
pub fn new(op: char, spacing: Spacing) -> Op {
Op {
impl Punct {
pub fn new(op: char, spacing: Spacing) -> Punct {
Punct {
op: op,
spacing: spacing,
span: Span::call_site(),
}
}

pub fn op(&self) -> char {
pub fn as_char(&self) -> char {
self.op
}

Expand All @@ -454,15 +462,15 @@ impl Op {
}
}

impl fmt::Display for Op {
impl fmt::Display for Punct {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.op.fmt(f)
}
}

impl fmt::Debug for Op {
impl fmt::Debug for Punct {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let mut debug = fmt.debug_struct("Op");
let mut debug = fmt.debug_struct("Punct");
debug.field("op", &self.op);
debug.field("spacing", &self.spacing);
#[cfg(procmacro2_semver_exempt)]
Expand All @@ -471,26 +479,31 @@ impl fmt::Debug for Op {
}
}

#[derive(Copy, Clone)]
pub struct Term {
inner: imp::Term,
#[derive(Clone)]
pub struct Ident {
inner: imp::Ident,
_marker: marker::PhantomData<Rc<()>>,
}

impl Term {
fn _new(inner: imp::Term) -> Term {
Term {
impl Ident {
fn _new(inner: imp::Ident) -> Ident {
Ident {
inner: inner,
_marker: marker::PhantomData,
}
}

pub fn new(string: &str, span: Span) -> Term {
Term::_new(imp::Term::new(string, span.inner))
pub fn new(string: &str, span: Span) -> Ident {
Ident::_new(imp::Ident::new(string, span.inner))
}

pub fn as_str(&self) -> &str {
self.inner.as_str()
#[cfg(procmacro2_semver_exempt)]
pub fn new_raw(string: &str, span: Span) -> Ident {
Ident::_new_raw(string, span)
}

fn _new_raw(string: &str, span: Span) -> Ident {
Ident::_new(imp::Ident::new_raw(string, span.inner))
}

pub fn span(&self) -> Span {
Expand All @@ -502,13 +515,40 @@ impl Term {
}
}

impl fmt::Display for Term {
impl PartialEq for Ident {
fn eq(&self, other: &Ident) -> bool{
self.to_string() == other.to_string()
}
}

impl Eq for Ident {
}

impl PartialOrd for Ident {
fn partial_cmp(&self, other: &Ident) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Ident {
fn cmp(&self, other: &Ident) -> Ordering {
self.to_string().cmp(&other.to_string())
}
}

impl Hash for Ident {
fn hash<H: Hasher>(&self, hasher: &mut H) {
self.to_string().hash(hasher)
}
}

impl fmt::Display for Ident {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.as_str().fmt(f)
self.inner.fmt(f)
}
}

impl fmt::Debug for Term {
impl fmt::Debug for Ident {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.inner.fmt(f)
}
Expand Down
Loading