@@ -502,9 +502,7 @@ impl fmt::Debug for TokenTree {
502
502
/// `Delimiter`s.
503
503
#[ derive( Clone ) ]
504
504
pub struct Group {
505
- delimiter : Delimiter ,
506
- stream : TokenStream ,
507
- span : Span ,
505
+ inner : imp:: Group ,
508
506
}
509
507
510
508
/// Describes how a sequence of token trees is delimited.
@@ -527,36 +525,73 @@ pub enum Delimiter {
527
525
}
528
526
529
527
impl Group {
528
+ fn _new ( inner : imp:: Group ) -> Self {
529
+ Group {
530
+ inner : inner,
531
+ }
532
+ }
533
+
534
+ fn _new_stable ( inner : stable:: Group ) -> Self {
535
+ Group {
536
+ inner : inner. into ( ) ,
537
+ }
538
+ }
539
+
530
540
/// Creates a new `Group` with the given delimiter and token stream.
531
541
///
532
542
/// This constructor will set the span for this group to
533
543
/// `Span::call_site()`. To change the span you can use the `set_span`
534
544
/// method below.
535
545
pub fn new ( delimiter : Delimiter , stream : TokenStream ) -> Group {
536
546
Group {
537
- delimiter : delimiter,
538
- stream : stream,
539
- span : Span :: call_site ( ) ,
547
+ inner : imp:: Group :: new ( delimiter, stream. inner ) ,
540
548
}
541
549
}
542
550
543
551
/// Returns the delimiter of this `Group`
544
552
pub fn delimiter ( & self ) -> Delimiter {
545
- self . delimiter
553
+ self . inner . delimiter ( )
546
554
}
547
555
548
556
/// Returns the `TokenStream` of tokens that are delimited in this `Group`.
549
557
///
550
558
/// Note that the returned token stream does not include the delimiter
551
559
/// returned above.
552
560
pub fn stream ( & self ) -> TokenStream {
553
- self . stream . clone ( )
561
+ TokenStream :: _new ( self . inner . stream ( ) )
554
562
}
555
563
556
564
/// Returns the span for the delimiters of this token stream, spanning the
557
565
/// entire `Group`.
566
+ ///
567
+ /// ```text
568
+ /// pub fn span(&self) -> Span {
569
+ /// ^^^^^^^
570
+ /// ```
558
571
pub fn span ( & self ) -> Span {
559
- self . span
572
+ Span :: _new ( self . inner . span ( ) )
573
+ }
574
+
575
+ /// Returns the span pointing to the opening delimiter of this group.
576
+ ///
577
+ /// ```text
578
+ /// pub fn span_open(&self) -> Span {
579
+ /// ^
580
+ /// ```
581
+ #[ cfg( procmacro2_semver_exempt) ]
582
+ pub fn span_open ( & self ) -> Span {
583
+ Span :: _new ( self . inner . span_open ( ) )
584
+ }
585
+
586
+ /// Returns the span pointing to the closing delimiter of this group.
587
+ ///
588
+ /// ```text
589
+ /// pub fn span_close(&self) -> Span {
590
+ /// ^
591
+ /// ```
592
+ #[ cfg( procmacro2_semver_exempt) ]
593
+ pub fn span_close ( & self ) -> Span {
594
+ Span :: _new ( self . inner . span_close ( ) )
560
595
}
561
596
562
597
/// Configures the span for this `Group`'s delimiters, but not its internal
@@ -566,38 +601,22 @@ impl Group {
566
601
/// by this group, but rather it will only set the span of the delimiter
567
602
/// tokens at the level of the `Group`.
568
603
pub fn set_span ( & mut self , span : Span ) {
569
- self . span = span ;
604
+ self . inner . set_span ( span. inner )
570
605
}
571
606
}
572
607
573
608
/// Prints the group as a string that should be losslessly convertible back
574
609
/// into the same group (modulo spans), except for possibly `TokenTree::Group`s
575
610
/// with `Delimiter::None` delimiters.
576
611
impl fmt:: Display for Group {
577
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
578
- let ( left, right) = match self . delimiter {
579
- Delimiter :: Parenthesis => ( "(" , ")" ) ,
580
- Delimiter :: Brace => ( "{" , "}" ) ,
581
- Delimiter :: Bracket => ( "[" , "]" ) ,
582
- Delimiter :: None => ( "" , "" ) ,
583
- } ;
584
-
585
- f. write_str ( left) ?;
586
- self . stream . fmt ( f) ?;
587
- f. write_str ( right) ?;
588
-
589
- Ok ( ( ) )
612
+ fn fmt ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
613
+ fmt:: Display :: fmt ( & self . inner , formatter)
590
614
}
591
615
}
592
616
593
617
impl fmt:: Debug for Group {
594
- fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
595
- let mut debug = fmt. debug_struct ( "Group" ) ;
596
- debug. field ( "delimiter" , & self . delimiter ) ;
597
- debug. field ( "stream" , & self . stream ) ;
598
- #[ cfg( procmacro2_semver_exempt) ]
599
- debug. field ( "span" , & self . span ) ;
600
- debug. finish ( )
618
+ fn fmt ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
619
+ fmt:: Debug :: fmt ( & self . inner , formatter)
601
620
}
602
621
}
603
622
0 commit comments