4343//! [ts]: https://doc.rust-lang.org/proc_macro/struct.TokenStream.html
4444
4545// Proc-macro2 types in rustdoc of other crates get linked to here.
46- #![ doc( html_root_url = "https://docs.rs/proc-macro2/0.3.8 " ) ]
46+ #![ doc( html_root_url = "https://docs.rs/proc-macro2/0.4.0 " ) ]
4747#![ cfg_attr( feature = "nightly" , feature( proc_macro) ) ]
4848
4949#[ cfg( feature = "proc-macro" ) ]
5050extern crate proc_macro;
5151extern crate unicode_xid;
5252
53+ use std:: cmp:: Ordering ;
5354use std:: fmt;
55+ use std:: hash:: { Hash , Hasher } ;
5456use std:: iter:: FromIterator ;
5557use std:: marker;
5658use std:: rc:: Rc ;
@@ -127,6 +129,12 @@ impl From<TokenStream> for proc_macro::TokenStream {
127129 }
128130}
129131
132+ impl Extend < TokenTree > for TokenStream {
133+ fn extend < I : IntoIterator < Item = TokenTree > > ( & mut self , streams : I ) {
134+ self . inner . extend ( streams)
135+ }
136+ }
137+
130138impl FromIterator < TokenTree > for TokenStream {
131139 fn from_iter < I : IntoIterator < Item = TokenTree > > ( streams : I ) -> Self {
132140 TokenStream :: _new ( streams. into_iter ( ) . collect ( ) )
@@ -284,26 +292,26 @@ impl fmt::Debug for Span {
284292#[ derive( Clone ) ]
285293pub enum TokenTree {
286294 Group ( Group ) ,
287- Term ( Term ) ,
288- Op ( Op ) ,
295+ Ident ( Ident ) ,
296+ Punct ( Punct ) ,
289297 Literal ( Literal ) ,
290298}
291299
292300impl TokenTree {
293301 pub fn span ( & self ) -> Span {
294302 match * self {
295303 TokenTree :: Group ( ref t) => t. span ( ) ,
296- TokenTree :: Term ( ref t) => t. span ( ) ,
297- TokenTree :: Op ( ref t) => t. span ( ) ,
304+ TokenTree :: Ident ( ref t) => t. span ( ) ,
305+ TokenTree :: Punct ( ref t) => t. span ( ) ,
298306 TokenTree :: Literal ( ref t) => t. span ( ) ,
299307 }
300308 }
301309
302310 pub fn set_span ( & mut self , span : Span ) {
303311 match * self {
304312 TokenTree :: Group ( ref mut t) => t. set_span ( span) ,
305- TokenTree :: Term ( ref mut t) => t. set_span ( span) ,
306- TokenTree :: Op ( ref mut t) => t. set_span ( span) ,
313+ TokenTree :: Ident ( ref mut t) => t. set_span ( span) ,
314+ TokenTree :: Punct ( ref mut t) => t. set_span ( span) ,
307315 TokenTree :: Literal ( ref mut t) => t. set_span ( span) ,
308316 }
309317 }
@@ -315,15 +323,15 @@ impl From<Group> for TokenTree {
315323 }
316324}
317325
318- impl From < Term > for TokenTree {
319- fn from ( g : Term ) -> TokenTree {
320- TokenTree :: Term ( g)
326+ impl From < Ident > for TokenTree {
327+ fn from ( g : Ident ) -> TokenTree {
328+ TokenTree :: Ident ( g)
321329 }
322330}
323331
324- impl From < Op > for TokenTree {
325- fn from ( g : Op ) -> TokenTree {
326- TokenTree :: Op ( g)
332+ impl From < Punct > for TokenTree {
333+ fn from ( g : Punct ) -> TokenTree {
334+ TokenTree :: Punct ( g)
327335 }
328336}
329337
@@ -337,8 +345,8 @@ impl fmt::Display for TokenTree {
337345 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
338346 match * self {
339347 TokenTree :: Group ( ref t) => t. fmt ( f) ,
340- TokenTree :: Term ( ref t) => t. fmt ( f) ,
341- TokenTree :: Op ( ref t) => t. fmt ( f) ,
348+ TokenTree :: Ident ( ref t) => t. fmt ( f) ,
349+ TokenTree :: Punct ( ref t) => t. fmt ( f) ,
342350 TokenTree :: Literal ( ref t) => t. fmt ( f) ,
343351 }
344352 }
@@ -350,8 +358,8 @@ impl fmt::Debug for TokenTree {
350358 // so don't bother with an extra layer of indirection
351359 match * self {
352360 TokenTree :: Group ( ref t) => t. fmt ( f) ,
353- TokenTree :: Term ( ref t) => t. fmt ( f) ,
354- TokenTree :: Op ( ref t) => t. fmt ( f) ,
361+ TokenTree :: Ident ( ref t) => t. fmt ( f) ,
362+ TokenTree :: Punct ( ref t) => t. fmt ( f) ,
355363 TokenTree :: Literal ( ref t) => t. fmt ( f) ,
356364 }
357365 }
@@ -415,8 +423,8 @@ impl fmt::Debug for Group {
415423 }
416424}
417425
418- #[ derive( Copy , Clone ) ]
419- pub struct Op {
426+ #[ derive( Clone ) ]
427+ pub struct Punct {
420428 op : char ,
421429 spacing : Spacing ,
422430 span : Span ,
@@ -428,16 +436,16 @@ pub enum Spacing {
428436 Joint ,
429437}
430438
431- impl Op {
432- pub fn new ( op : char , spacing : Spacing ) -> Op {
433- Op {
439+ impl Punct {
440+ pub fn new ( op : char , spacing : Spacing ) -> Punct {
441+ Punct {
434442 op : op,
435443 spacing : spacing,
436444 span : Span :: call_site ( ) ,
437445 }
438446 }
439447
440- pub fn op ( & self ) -> char {
448+ pub fn as_char ( & self ) -> char {
441449 self . op
442450 }
443451
@@ -454,15 +462,15 @@ impl Op {
454462 }
455463}
456464
457- impl fmt:: Display for Op {
465+ impl fmt:: Display for Punct {
458466 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
459467 self . op . fmt ( f)
460468 }
461469}
462470
463- impl fmt:: Debug for Op {
471+ impl fmt:: Debug for Punct {
464472 fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
465- let mut debug = fmt. debug_struct ( "Op " ) ;
473+ let mut debug = fmt. debug_struct ( "Punct " ) ;
466474 debug. field ( "op" , & self . op ) ;
467475 debug. field ( "spacing" , & self . spacing ) ;
468476 #[ cfg( procmacro2_semver_exempt) ]
@@ -471,26 +479,31 @@ impl fmt::Debug for Op {
471479 }
472480}
473481
474- #[ derive( Copy , Clone ) ]
475- pub struct Term {
476- inner : imp:: Term ,
482+ #[ derive( Clone ) ]
483+ pub struct Ident {
484+ inner : imp:: Ident ,
477485 _marker : marker:: PhantomData < Rc < ( ) > > ,
478486}
479487
480- impl Term {
481- fn _new ( inner : imp:: Term ) -> Term {
482- Term {
488+ impl Ident {
489+ fn _new ( inner : imp:: Ident ) -> Ident {
490+ Ident {
483491 inner : inner,
484492 _marker : marker:: PhantomData ,
485493 }
486494 }
487495
488- pub fn new ( string : & str , span : Span ) -> Term {
489- Term :: _new ( imp:: Term :: new ( string, span. inner ) )
496+ pub fn new ( string : & str , span : Span ) -> Ident {
497+ Ident :: _new ( imp:: Ident :: new ( string, span. inner ) )
490498 }
491499
492- pub fn as_str ( & self ) -> & str {
493- self . inner . as_str ( )
500+ #[ cfg( procmacro2_semver_exempt) ]
501+ pub fn new_raw ( string : & str , span : Span ) -> Ident {
502+ Ident :: _new_raw ( string, span)
503+ }
504+
505+ fn _new_raw ( string : & str , span : Span ) -> Ident {
506+ Ident :: _new ( imp:: Ident :: new_raw ( string, span. inner ) )
494507 }
495508
496509 pub fn span ( & self ) -> Span {
@@ -502,13 +515,40 @@ impl Term {
502515 }
503516}
504517
505- impl fmt:: Display for Term {
518+ impl PartialEq for Ident {
519+ fn eq ( & self , other : & Ident ) -> bool {
520+ self . to_string ( ) == other. to_string ( )
521+ }
522+ }
523+
524+ impl Eq for Ident {
525+ }
526+
527+ impl PartialOrd for Ident {
528+ fn partial_cmp ( & self , other : & Ident ) -> Option < Ordering > {
529+ Some ( self . cmp ( other) )
530+ }
531+ }
532+
533+ impl Ord for Ident {
534+ fn cmp ( & self , other : & Ident ) -> Ordering {
535+ self . to_string ( ) . cmp ( & other. to_string ( ) )
536+ }
537+ }
538+
539+ impl Hash for Ident {
540+ fn hash < H : Hasher > ( & self , hasher : & mut H ) {
541+ self . to_string ( ) . hash ( hasher)
542+ }
543+ }
544+
545+ impl fmt:: Display for Ident {
506546 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
507- self . as_str ( ) . fmt ( f)
547+ self . inner . fmt ( f)
508548 }
509549}
510550
511- impl fmt:: Debug for Term {
551+ impl fmt:: Debug for Ident {
512552 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
513553 self . inner . fmt ( f)
514554 }
0 commit comments