@@ -31,7 +31,8 @@ use tokenstream::{self, TokenStream, TokenTree};
31
31
32
32
use std:: ascii;
33
33
use std:: io:: { self , Write , Read } ;
34
- use std:: iter;
34
+ use std:: iter:: { self , Peekable } ;
35
+ use std:: vec;
35
36
36
37
pub enum AnnNode < ' a > {
37
38
NodeIdent ( & ' a ast:: Ident ) ,
@@ -53,18 +54,12 @@ pub struct NoAnn;
53
54
54
55
impl PpAnn for NoAnn { }
55
56
56
- #[ derive( Copy , Clone ) ]
57
- pub struct CurrentCommentAndLiteral {
58
- pub cur_cmnt : usize ,
59
- pub cur_lit : usize ,
60
- }
61
-
62
57
pub struct State < ' a > {
63
58
pub s : pp:: Printer < ' a > ,
64
59
cm : Option < & ' a CodeMap > ,
65
60
comments : Option < Vec < comments:: Comment > > ,
66
- literals : Option < Vec < comments:: Literal > > ,
67
- cur_cmnt_and_lit : CurrentCommentAndLiteral ,
61
+ literals : Peekable < vec :: IntoIter < comments:: Literal > > ,
62
+ cur_cmnt : usize ,
68
63
boxes : Vec < pp:: Breaks > ,
69
64
ann : & ' a ( PpAnn +' a ) ,
70
65
}
@@ -80,11 +75,8 @@ pub fn rust_printer_annotated<'a>(writer: Box<Write+'a>,
80
75
s : pp:: mk_printer ( writer, DEFAULT_COLUMNS ) ,
81
76
cm : None ,
82
77
comments : None ,
83
- literals : None ,
84
- cur_cmnt_and_lit : CurrentCommentAndLiteral {
85
- cur_cmnt : 0 ,
86
- cur_lit : 0
87
- } ,
78
+ literals : vec ! [ ] . into_iter ( ) . peekable ( ) ,
79
+ cur_cmnt : 0 ,
88
80
boxes : Vec :: new ( ) ,
89
81
ann : ann,
90
82
}
@@ -160,11 +152,8 @@ impl<'a> State<'a> {
160
152
s : pp:: mk_printer ( out, DEFAULT_COLUMNS ) ,
161
153
cm : Some ( cm) ,
162
154
comments : comments,
163
- literals : literals,
164
- cur_cmnt_and_lit : CurrentCommentAndLiteral {
165
- cur_cmnt : 0 ,
166
- cur_lit : 0
167
- } ,
155
+ literals : literals. unwrap_or_default ( ) . into_iter ( ) . peekable ( ) ,
156
+ cur_cmnt : 0 ,
168
157
boxes : Vec :: new ( ) ,
169
158
ann : ann,
170
159
}
@@ -451,8 +440,9 @@ pub trait PrintState<'a> {
451
440
fn writer ( & mut self ) -> & mut pp:: Printer < ' a > ;
452
441
fn boxes ( & mut self ) -> & mut Vec < pp:: Breaks > ;
453
442
fn comments ( & mut self ) -> & mut Option < Vec < comments:: Comment > > ;
454
- fn cur_cmnt_and_lit ( & mut self ) -> & mut CurrentCommentAndLiteral ;
455
- fn literals ( & self ) -> & Option < Vec < comments:: Literal > > ;
443
+ fn cur_cmnt ( & mut self ) -> & mut usize ;
444
+ fn cur_lit ( & mut self ) -> Option < & comments:: Literal > ;
445
+ fn bump_lit ( & mut self ) -> Option < comments:: Literal > ;
456
446
457
447
fn word_space ( & mut self , w : & str ) -> io:: Result < ( ) > {
458
448
self . writer ( ) . word ( w) ?;
@@ -518,31 +508,24 @@ pub trait PrintState<'a> {
518
508
}
519
509
520
510
fn next_lit ( & mut self , pos : BytePos ) -> Option < comments:: Literal > {
521
- let mut cur_lit = self . cur_cmnt_and_lit ( ) . cur_lit ;
522
-
523
- let mut result = None ;
511
+ while let Some ( ltrl) = self . cur_lit ( ) . cloned ( ) {
512
+ if ltrl. pos > pos { break ; }
524
513
525
- if let Some ( ref lits) = * self . literals ( ) {
526
- while cur_lit < lits. len ( ) {
527
- let ltrl = ( * lits) [ cur_lit] . clone ( ) ;
528
- if ltrl. pos > pos { break ; }
529
- cur_lit += 1 ;
530
- if ltrl. pos == pos {
531
- result = Some ( ltrl) ;
532
- break ;
533
- }
514
+ // we don't need the value here since we're forced to clone cur_lit
515
+ // due to lack of NLL.
516
+ self . bump_lit ( ) ;
517
+ if ltrl. pos == pos {
518
+ return Some ( ltrl) ;
534
519
}
535
520
}
536
521
537
- self . cur_cmnt_and_lit ( ) . cur_lit = cur_lit;
538
- result
522
+ None
539
523
}
540
524
541
525
fn maybe_print_comment ( & mut self , pos : BytePos ) -> io:: Result < ( ) > {
542
526
while let Some ( ref cmnt) = self . next_comment ( ) {
543
527
if cmnt. pos < pos {
544
528
self . print_comment ( cmnt) ?;
545
- self . cur_cmnt_and_lit ( ) . cur_cmnt += 1 ;
546
529
} else {
547
530
break
548
531
}
@@ -552,7 +535,7 @@ pub trait PrintState<'a> {
552
535
553
536
fn print_comment ( & mut self ,
554
537
cmnt : & comments:: Comment ) -> io:: Result < ( ) > {
555
- match cmnt. style {
538
+ let r = match cmnt. style {
556
539
comments:: Mixed => {
557
540
assert_eq ! ( cmnt. lines. len( ) , 1 ) ;
558
541
self . writer ( ) . zerobreak ( ) ?;
@@ -600,11 +583,18 @@ pub trait PrintState<'a> {
600
583
}
601
584
self . writer ( ) . hardbreak ( )
602
585
}
586
+ } ;
587
+ match r {
588
+ Ok ( ( ) ) => {
589
+ * self . cur_cmnt ( ) = * self . cur_cmnt ( ) + 1 ;
590
+ Ok ( ( ) )
591
+ }
592
+ Err ( e) => Err ( e) ,
603
593
}
604
594
}
605
595
606
596
fn next_comment ( & mut self ) -> Option < comments:: Comment > {
607
- let cur_cmnt = self . cur_cmnt_and_lit ( ) . cur_cmnt ;
597
+ let cur_cmnt = * self . cur_cmnt ( ) ;
608
598
match * self . comments ( ) {
609
599
Some ( ref cmnts) => {
610
600
if cur_cmnt < cmnts. len ( ) {
@@ -619,8 +609,8 @@ pub trait PrintState<'a> {
619
609
620
610
fn print_literal ( & mut self , lit : & ast:: Lit ) -> io:: Result < ( ) > {
621
611
self . maybe_print_comment ( lit. span . lo ) ?;
622
- if let Some ( ref ltrl) = self . next_lit ( lit. span . lo ) {
623
- return self . writer ( ) . word ( & ( * ltrl) . lit ) ;
612
+ if let Some ( ltrl) = self . next_lit ( lit. span . lo ) {
613
+ return self . writer ( ) . word ( & ltrl. lit ) ;
624
614
}
625
615
match lit. node {
626
616
ast:: LitKind :: Str ( st, style) => self . print_string ( & st. as_str ( ) , style) ,
@@ -860,12 +850,16 @@ impl<'a> PrintState<'a> for State<'a> {
860
850
& mut self . comments
861
851
}
862
852
863
- fn cur_cmnt_and_lit ( & mut self ) -> & mut CurrentCommentAndLiteral {
864
- & mut self . cur_cmnt_and_lit
853
+ fn cur_cmnt ( & mut self ) -> & mut usize {
854
+ & mut self . cur_cmnt
855
+ }
856
+
857
+ fn cur_lit ( & mut self ) -> Option < & comments:: Literal > {
858
+ self . literals . peek ( )
865
859
}
866
860
867
- fn literals ( & self ) -> & Option < Vec < comments:: Literal > > {
868
- & self . literals
861
+ fn bump_lit ( & mut self ) -> Option < comments:: Literal > {
862
+ self . literals . next ( )
869
863
}
870
864
}
871
865
@@ -3021,7 +3015,6 @@ impl<'a> State<'a> {
3021
3015
let next = next_pos. unwrap_or ( cmnt. pos + BytePos ( 1 ) ) ;
3022
3016
if span. hi < cmnt. pos && cmnt. pos < next && span_line. line == comment_line. line {
3023
3017
self . print_comment ( cmnt) ?;
3024
- self . cur_cmnt_and_lit . cur_cmnt += 1 ;
3025
3018
}
3026
3019
}
3027
3020
Ok ( ( ) )
@@ -3035,7 +3028,6 @@ impl<'a> State<'a> {
3035
3028
}
3036
3029
while let Some ( ref cmnt) = self . next_comment ( ) {
3037
3030
self . print_comment ( cmnt) ?;
3038
- self . cur_cmnt_and_lit . cur_cmnt += 1 ;
3039
3031
}
3040
3032
Ok ( ( ) )
3041
3033
}
0 commit comments