@@ -403,61 +403,35 @@ impl<A: Step + Clone> Iterator for StepBy<A, ops::RangeInclusive<A>> {
403
403
404
404
#[ inline]
405
405
fn next ( & mut self ) -> Option < A > {
406
- use ops:: RangeInclusive :: * ;
407
-
408
- // this function has a sort of odd structure due to borrowck issues
409
- // we may need to replace self.range, so borrows of start and end need to end early
410
-
411
- let ( finishing, n) = match self . range {
412
- Empty { .. } => return None , // empty iterators yield no values
413
-
414
- NonEmpty { ref mut start, ref mut end } => {
415
- let rev = self . step_by . is_negative ( ) ;
416
-
417
- // march start towards (maybe past!) end and yield the old value
418
- if ( rev && start >= end) ||
419
- ( !rev && start <= end)
420
- {
421
- match start. step ( & self . step_by ) {
422
- Some ( mut n) => {
423
- mem:: swap ( start, & mut n) ;
424
- ( None , Some ( n) ) // yield old value, remain non-empty
425
- } ,
426
- None => {
427
- let mut n = end. clone ( ) ;
428
- mem:: swap ( start, & mut n) ;
429
- ( None , Some ( n) ) // yield old value, remain non-empty
430
- }
431
- }
432
- } else {
433
- // found range in inconsistent state (start at or past end), so become empty
434
- ( Some ( end. replace_zero ( ) ) , None )
435
- }
436
- }
437
- } ;
406
+ let rev = self . step_by . is_negative ( ) ;
438
407
439
- // turn into an empty iterator if we've reached the end
440
- if let Some ( end) = finishing {
441
- self . range = Empty { at : end } ;
408
+ if ( rev && self . range . start >= self . range . end ) ||
409
+ ( !rev && self . range . start <= self . range . end )
410
+ {
411
+ match self . range . start . step ( & self . step_by ) {
412
+ Some ( n) => {
413
+ Some ( mem:: replace ( & mut self . range . start , n) )
414
+ } ,
415
+ None => {
416
+ let last = self . range . start . replace_one ( ) ;
417
+ self . range . end . replace_zero ( ) ;
418
+ self . step_by . replace_one ( ) ;
419
+ Some ( last)
420
+ } ,
421
+ }
422
+ }
423
+ else {
424
+ None
442
425
}
443
-
444
- n
445
426
}
446
427
447
428
#[ inline]
448
429
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
449
- use ops:: RangeInclusive :: * ;
450
-
451
- match self . range {
452
- Empty { .. } => ( 0 , Some ( 0 ) ) ,
453
-
454
- NonEmpty { ref start, ref end } =>
455
- match Step :: steps_between ( start,
456
- end,
457
- & self . step_by ) {
458
- Some ( hint) => ( hint. saturating_add ( 1 ) , hint. checked_add ( 1 ) ) ,
459
- None => ( 0 , None )
460
- }
430
+ match Step :: steps_between ( & self . range . start ,
431
+ & self . range . end ,
432
+ & self . step_by ) {
433
+ Some ( hint) => ( hint. saturating_add ( 1 ) , hint. checked_add ( 1 ) ) ,
434
+ None => ( 0 , None )
461
435
}
462
436
}
463
437
}
@@ -583,56 +557,27 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> where
583
557
584
558
#[ inline]
585
559
fn next ( & mut self ) -> Option < A > {
586
- use ops:: RangeInclusive :: * ;
587
-
588
- // this function has a sort of odd structure due to borrowck issues
589
- // we may need to replace self, so borrows of self.start and self.end need to end early
590
-
591
- let ( finishing, n) = match * self {
592
- Empty { .. } => ( None , None ) , // empty iterators yield no values
593
-
594
- NonEmpty { ref mut start, ref mut end } => {
595
- if start == end {
596
- ( Some ( end. replace_one ( ) ) , Some ( start. replace_one ( ) ) )
597
- } else if start < end {
598
- let mut n = start. add_one ( ) ;
599
- mem:: swap ( & mut n, start) ;
600
-
601
- // if the iterator is done iterating, it will change from
602
- // NonEmpty to Empty to avoid unnecessary drops or clones,
603
- // we'll reuse either start or end (they are equal now, so
604
- // it doesn't matter which) to pull out end, we need to swap
605
- // something back in
606
-
607
- ( if n == * end { Some ( end. replace_one ( ) ) } else { None } ,
608
- // ^ are we done yet?
609
- Some ( n) ) // < the value to output
610
- } else {
611
- ( Some ( start. replace_one ( ) ) , None )
612
- }
613
- }
614
- } ;
615
-
616
- // turn into an empty iterator if this is the last value
617
- if let Some ( end) = finishing {
618
- * self = Empty { at : end } ;
560
+ use cmp:: Ordering :: * ;
561
+
562
+ match self . start . partial_cmp ( & self . end ) {
563
+ Some ( Less ) => {
564
+ let n = self . start . add_one ( ) ;
565
+ Some ( mem:: replace ( & mut self . start , n) )
566
+ } ,
567
+ Some ( Equal ) => {
568
+ let last = self . start . replace_one ( ) ;
569
+ self . end . replace_zero ( ) ;
570
+ Some ( last)
571
+ } ,
572
+ _ => None ,
619
573
}
620
-
621
- n
622
574
}
623
575
624
576
#[ inline]
625
577
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
626
- use ops:: RangeInclusive :: * ;
627
-
628
- match * self {
629
- Empty { .. } => ( 0 , Some ( 0 ) ) ,
630
-
631
- NonEmpty { ref start, ref end } =>
632
- match Step :: steps_between_by_one ( start, end) {
633
- Some ( hint) => ( hint. saturating_add ( 1 ) , hint. checked_add ( 1 ) ) ,
634
- None => ( 0 , None ) ,
635
- }
578
+ match Step :: steps_between_by_one ( & self . start , & self . end ) {
579
+ Some ( hint) => ( hint. saturating_add ( 1 ) , hint. checked_add ( 1 ) ) ,
580
+ None => ( 0 , None ) ,
636
581
}
637
582
}
638
583
}
@@ -644,33 +589,20 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> where
644
589
{
645
590
#[ inline]
646
591
fn next_back ( & mut self ) -> Option < A > {
647
- use ops:: RangeInclusive :: * ;
648
-
649
- // see Iterator::next for comments
650
-
651
- let ( finishing, n) = match * self {
652
- Empty { .. } => return None ,
653
-
654
- NonEmpty { ref mut start, ref mut end } => {
655
- if start == end {
656
- ( Some ( start. replace_one ( ) ) , Some ( end. replace_one ( ) ) )
657
- } else if start < end {
658
- let mut n = end. sub_one ( ) ;
659
- mem:: swap ( & mut n, end) ;
660
-
661
- ( if n == * start { Some ( start. replace_one ( ) ) } else { None } ,
662
- Some ( n) )
663
- } else {
664
- ( Some ( end. replace_one ( ) ) , None )
665
- }
666
- }
667
- } ;
668
-
669
- if let Some ( start) = finishing {
670
- * self = Empty { at : start } ;
592
+ use cmp:: Ordering :: * ;
593
+
594
+ match self . start . partial_cmp ( & self . end ) {
595
+ Some ( Less ) => {
596
+ let n = self . end . sub_one ( ) ;
597
+ Some ( mem:: replace ( & mut self . end , n) )
598
+ } ,
599
+ Some ( Equal ) => {
600
+ let last = self . end . replace_zero ( ) ;
601
+ self . start . replace_one ( ) ;
602
+ Some ( last)
603
+ } ,
604
+ _ => None ,
671
605
}
672
-
673
- n
674
606
}
675
607
}
676
608
0 commit comments