@@ -436,12 +436,12 @@ pub struct EscapeUnicode {
436
436
437
437
#[ derive( Clone ) ]
438
438
enum EscapeUnicodeState {
439
- Backslash ,
440
- Type ,
441
- LeftBrace ,
442
- Value ,
443
- RightBrace ,
444
439
Done ,
440
+ RightBrace ,
441
+ Value ,
442
+ LeftBrace ,
443
+ Type ,
444
+ Backslash ,
445
445
}
446
446
447
447
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -479,16 +479,9 @@ impl Iterator for EscapeUnicode {
479
479
}
480
480
}
481
481
482
+ #[ inline]
482
483
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
483
- let n = match self . state {
484
- EscapeUnicodeState :: Backslash => 5 ,
485
- EscapeUnicodeState :: Type => 4 ,
486
- EscapeUnicodeState :: LeftBrace => 3 ,
487
- EscapeUnicodeState :: Value => 2 ,
488
- EscapeUnicodeState :: RightBrace => 1 ,
489
- EscapeUnicodeState :: Done => 0 ,
490
- } ;
491
- let n = n + self . offset ;
484
+ let n = self . len ( ) ;
492
485
( n, Some ( n) )
493
486
}
494
487
@@ -511,7 +504,20 @@ impl Iterator for EscapeUnicode {
511
504
}
512
505
513
506
#[ stable( feature = "rust1" , since = "1.7.0" ) ]
514
- impl ExactSizeIterator for EscapeUnicode { }
507
+ impl ExactSizeIterator for EscapeUnicode {
508
+ #[ inline]
509
+ fn len ( & self ) -> usize {
510
+ // The match is a single memory access with no branching
511
+ self . offset + self . state {
512
+ EscapeUnicodeState :: Done => 0 ,
513
+ EscapeUnicodeState :: RightBrace => 1 ,
514
+ EscapeUnicodeState :: Value => 2 ,
515
+ EscapeUnicodeState :: LeftBrace => 3 ,
516
+ EscapeUnicodeState :: Type => 4 ,
517
+ EscapeUnicodeState :: Backslash => 5 ,
518
+ }
519
+ }
520
+ }
515
521
516
522
/// An iterator that yields the literal escape code of a `char`.
517
523
///
@@ -528,9 +534,9 @@ pub struct EscapeDefault {
528
534
529
535
#[ derive( Clone ) ]
530
536
enum EscapeDefaultState {
531
- Backslash ( char ) ,
532
- Char ( char ) ,
533
537
Done ,
538
+ Char ( char ) ,
539
+ Backslash ( char ) ,
534
540
Unicode ( EscapeUnicode ) ,
535
541
}
536
542
@@ -553,13 +559,10 @@ impl Iterator for EscapeDefault {
553
559
}
554
560
}
555
561
562
+ #[ inline]
556
563
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
557
- match self . state {
558
- EscapeDefaultState :: Char ( _) => ( 1 , Some ( 1 ) ) ,
559
- EscapeDefaultState :: Backslash ( _) => ( 2 , Some ( 2 ) ) ,
560
- EscapeDefaultState :: Unicode ( ref iter) => iter. size_hint ( ) ,
561
- EscapeDefaultState :: Done => ( 0 , Some ( 0 ) ) ,
562
- }
564
+ let n = self . len ( ) ;
565
+ ( n, Some ( n) )
563
566
}
564
567
565
568
#[ inline]
@@ -605,4 +608,13 @@ impl Iterator for EscapeDefault {
605
608
}
606
609
607
610
#[ stable( feature = "rust1" , since = "1.7.0" ) ]
608
- impl ExactSizeIterator for EscapeDefault { }
611
+ impl ExactSizeIterator for EscapeDefault {
612
+ fn len ( & self ) -> usize {
613
+ match self . state {
614
+ EscapeDefaultState :: Done => 0 ,
615
+ EscapeDefaultState :: Char ( _) => 1 ,
616
+ EscapeDefaultState :: Backslash ( _) => 2 ,
617
+ EscapeDefaultState :: Unicode ( ref iter) => iter. len ( ) ,
618
+ }
619
+ }
620
+ }
0 commit comments