@@ -413,12 +413,12 @@ pub struct EscapeUnicode {
413
413
414
414
#[ derive( Clone , Debug ) ]
415
415
enum EscapeUnicodeState {
416
- Backslash ,
417
- Type ,
418
- LeftBrace ,
419
- Value ,
420
- RightBrace ,
421
416
Done ,
417
+ RightBrace ,
418
+ Value ,
419
+ LeftBrace ,
420
+ Type ,
421
+ Backslash ,
422
422
}
423
423
424
424
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -457,16 +457,9 @@ impl Iterator for EscapeUnicode {
457
457
}
458
458
}
459
459
460
+ #[ inline]
460
461
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
461
- let n = match self . state {
462
- EscapeUnicodeState :: Backslash => 5 ,
463
- EscapeUnicodeState :: Type => 4 ,
464
- EscapeUnicodeState :: LeftBrace => 3 ,
465
- EscapeUnicodeState :: Value => 2 ,
466
- EscapeUnicodeState :: RightBrace => 1 ,
467
- EscapeUnicodeState :: Done => 0 ,
468
- } ;
469
- let n = n + self . hex_digit_idx ;
462
+ let n = self . len ( ) ;
470
463
( n, Some ( n) )
471
464
}
472
465
@@ -489,7 +482,20 @@ impl Iterator for EscapeUnicode {
489
482
}
490
483
491
484
#[ stable( feature = "exact_size_escape" , since = "1.11.0" ) ]
492
- impl ExactSizeIterator for EscapeUnicode { }
485
+ impl ExactSizeIterator for EscapeUnicode {
486
+ #[ inline]
487
+ fn len ( & self ) -> usize {
488
+ // The match is a single memory access with no branching
489
+ self . hex_digit_idx + match self . state {
490
+ EscapeUnicodeState :: Done => 0 ,
491
+ EscapeUnicodeState :: RightBrace => 1 ,
492
+ EscapeUnicodeState :: Value => 2 ,
493
+ EscapeUnicodeState :: LeftBrace => 3 ,
494
+ EscapeUnicodeState :: Type => 4 ,
495
+ EscapeUnicodeState :: Backslash => 5 ,
496
+ }
497
+ }
498
+ }
493
499
494
500
/// An iterator that yields the literal escape code of a `char`.
495
501
///
@@ -506,9 +512,9 @@ pub struct EscapeDefault {
506
512
507
513
#[ derive( Clone , Debug ) ]
508
514
enum EscapeDefaultState {
509
- Backslash ( char ) ,
510
- Char ( char ) ,
511
515
Done ,
516
+ Char ( char ) ,
517
+ Backslash ( char ) ,
512
518
Unicode ( EscapeUnicode ) ,
513
519
}
514
520
@@ -531,13 +537,10 @@ impl Iterator for EscapeDefault {
531
537
}
532
538
}
533
539
540
+ #[ inline]
534
541
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
535
- match self . state {
536
- EscapeDefaultState :: Char ( _) => ( 1 , Some ( 1 ) ) ,
537
- EscapeDefaultState :: Backslash ( _) => ( 2 , Some ( 2 ) ) ,
538
- EscapeDefaultState :: Unicode ( ref iter) => iter. size_hint ( ) ,
539
- EscapeDefaultState :: Done => ( 0 , Some ( 0 ) ) ,
540
- }
542
+ let n = self . len ( ) ;
543
+ ( n, Some ( n) )
541
544
}
542
545
543
546
#[ inline]
@@ -583,7 +586,16 @@ impl Iterator for EscapeDefault {
583
586
}
584
587
585
588
#[ stable( feature = "exact_size_escape" , since = "1.11.0" ) ]
586
- impl ExactSizeIterator for EscapeDefault { }
589
+ impl ExactSizeIterator for EscapeDefault {
590
+ fn len ( & self ) -> usize {
591
+ match self . state {
592
+ EscapeDefaultState :: Done => 0 ,
593
+ EscapeDefaultState :: Char ( _) => 1 ,
594
+ EscapeDefaultState :: Backslash ( _) => 2 ,
595
+ EscapeDefaultState :: Unicode ( ref iter) => iter. len ( ) ,
596
+ }
597
+ }
598
+ }
587
599
588
600
/// An iterator over `u8` entries represending the UTF-8 encoding of a `char`
589
601
/// value.
0 commit comments