99 html_playground_url = "https://play.rust-lang.org/" ,
1010 test( attr( deny( warnings) ) )
1111) ]
12- #! [ feature ( nll ) ]
13- #![ feature( bool_to_option ) ]
12+ // We want to be able to build this crate with a stable compiler, so no
13+ // ` #![feature]` attributes should be added.
1414
1515pub use Alignment :: * ;
1616pub use Count :: * ;
@@ -22,7 +22,19 @@ use std::iter;
2222use std:: str;
2323use std:: string;
2424
25- use rustc_span:: { InnerSpan , Symbol } ;
25+ // Note: copied from rustc_span
26+ /// Range inside of a `Span` used for diagnostics when we only have access to relative positions.
27+ #[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
28+ pub struct InnerSpan {
29+ pub start : usize ,
30+ pub end : usize ,
31+ }
32+
33+ impl InnerSpan {
34+ pub fn new ( start : usize , end : usize ) -> InnerSpan {
35+ InnerSpan { start, end }
36+ }
37+ }
2638
2739/// The type of format string that we are parsing.
2840#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
@@ -57,7 +69,7 @@ pub enum Piece<'a> {
5769#[ derive( Copy , Clone , Debug , PartialEq ) ]
5870pub struct Argument < ' a > {
5971 /// Where to find this argument
60- pub position : Position ,
72+ pub position : Position < ' a > ,
6173 /// How to format the argument
6274 pub format : FormatSpec < ' a > ,
6375}
@@ -72,11 +84,11 @@ pub struct FormatSpec<'a> {
7284 /// Packed version of various flags provided.
7385 pub flags : u32 ,
7486 /// The integer precision to use.
75- pub precision : Count ,
87+ pub precision : Count < ' a > ,
7688 /// The span of the precision formatting flag (for diagnostics).
7789 pub precision_span : Option < InnerSpan > ,
7890 /// The string width requested for the resulting format.
79- pub width : Count ,
91+ pub width : Count < ' a > ,
8092 /// The span of the width formatting flag (for diagnostics).
8193 pub width_span : Option < InnerSpan > ,
8294 /// The descriptor string representing the name of the format desired for
@@ -89,16 +101,16 @@ pub struct FormatSpec<'a> {
89101
90102/// Enum describing where an argument for a format can be located.
91103#[ derive( Copy , Clone , Debug , PartialEq ) ]
92- pub enum Position {
104+ pub enum Position < ' a > {
93105 /// The argument is implied to be located at an index
94106 ArgumentImplicitlyIs ( usize ) ,
95107 /// The argument is located at a specific index given in the format
96108 ArgumentIs ( usize ) ,
97109 /// The argument has a name.
98- ArgumentNamed ( Symbol , InnerSpan ) ,
110+ ArgumentNamed ( & ' a str , InnerSpan ) ,
99111}
100112
101- impl Position {
113+ impl Position < ' _ > {
102114 pub fn index ( & self ) -> Option < usize > {
103115 match self {
104116 ArgumentIs ( i) | ArgumentImplicitlyIs ( i) => Some ( * i) ,
@@ -143,11 +155,11 @@ pub enum Flag {
143155/// A count is used for the precision and width parameters of an integer, and
144156/// can reference either an argument or a literal integer.
145157#[ derive( Copy , Clone , Debug , PartialEq ) ]
146- pub enum Count {
158+ pub enum Count < ' a > {
147159 /// The count is specified explicitly.
148160 CountIs ( usize ) ,
149161 /// The count is specified by the argument with the given name.
150- CountIsName ( Symbol , InnerSpan ) ,
162+ CountIsName ( & ' a str , InnerSpan ) ,
151163 /// The count is specified by the argument at the given index.
152164 CountIsParam ( usize ) ,
153165 /// The count is implied and cannot be explicitly specified.
@@ -489,7 +501,7 @@ impl<'a> Parser<'a> {
489501 /// integer index of an argument, a named argument, or a blank string.
490502 /// Returns `Some(parsed_position)` if the position is not implicitly
491503 /// consuming a macro argument, `None` if it's the case.
492- fn position ( & mut self ) -> Option < Position > {
504+ fn position ( & mut self ) -> Option < Position < ' a > > {
493505 if let Some ( i) = self . integer ( ) {
494506 Some ( ArgumentIs ( i) )
495507 } else {
@@ -498,7 +510,7 @@ impl<'a> Parser<'a> {
498510 let word = self . word ( ) ;
499511 let end = start + word. len ( ) ;
500512 let span = self . to_span_index ( start) . to ( self . to_span_index ( end) ) ;
501- Some ( ArgumentNamed ( Symbol :: intern ( word) , span) )
513+ Some ( ArgumentNamed ( word, span) )
502514 }
503515
504516 // This is an `ArgumentNext`.
@@ -651,7 +663,7 @@ impl<'a> Parser<'a> {
651663 /// Parses a `Count` parameter at the current position. This does not check
652664 /// for 'CountIsNextParam' because that is only used in precision, not
653665 /// width.
654- fn count ( & mut self , start : usize ) -> ( Count , Option < InnerSpan > ) {
666+ fn count ( & mut self , start : usize ) -> ( Count < ' a > , Option < InnerSpan > ) {
655667 if let Some ( i) = self . integer ( ) {
656668 if let Some ( end) = self . consume_pos ( '$' ) {
657669 let span = self . to_span_index ( start) . to ( self . to_span_index ( end + 1 ) ) ;
@@ -667,7 +679,7 @@ impl<'a> Parser<'a> {
667679 ( CountImplied , None )
668680 } else if let Some ( end) = self . consume_pos ( '$' ) {
669681 let span = self . to_span_index ( start + 1 ) . to ( self . to_span_index ( end) ) ;
670- ( CountIsName ( Symbol :: intern ( word) , span) , None )
682+ ( CountIsName ( word, span) , None )
671683 } else {
672684 self . cur = tmp;
673685 ( CountImplied , None )
@@ -723,7 +735,7 @@ impl<'a> Parser<'a> {
723735 break ;
724736 }
725737 }
726- found. then_some ( cur)
738+ if found { Some ( cur) } else { None }
727739 }
728740}
729741
0 commit comments