10
10
11
11
use convert:: TryFrom ;
12
12
use mem;
13
- use ops:: { self , Add , Sub , Try } ;
13
+ use ops:: { self , Add , Sub } ;
14
14
use usize;
15
15
16
16
use super :: { FusedIterator , TrustedLen } ;
@@ -330,23 +330,23 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
330
330
331
331
#[ inline]
332
332
fn next ( & mut self ) -> Option < A > {
333
- if self . start <= self . end {
334
- if self . start < self . end {
335
- let n = self . start . add_one ( ) ;
336
- Some ( mem:: replace ( & mut self . start , n) )
337
- } else {
338
- let last = self . start . replace_one ( ) ;
339
- self . end . replace_zero ( ) ;
340
- Some ( last)
341
- }
342
- } else {
343
- None
333
+ self . compute_is_empty ( ) ;
334
+ if self . is_empty . unwrap_or_default ( ) {
335
+ return None ;
344
336
}
337
+ let is_iterating = self . start < self . end ;
338
+ self . is_empty = Some ( !is_iterating) ;
339
+ Some ( if is_iterating {
340
+ let n = self . start . add_one ( ) ;
341
+ mem:: replace ( & mut self . start , n)
342
+ } else {
343
+ self . start . clone ( )
344
+ } )
345
345
}
346
346
347
347
#[ inline]
348
348
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
349
- if ! ( self . start <= self . end ) {
349
+ if self . is_empty ( ) {
350
350
return ( 0 , Some ( 0 ) ) ;
351
351
}
352
352
@@ -358,25 +358,29 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
358
358
359
359
#[ inline]
360
360
fn nth ( & mut self , n : usize ) -> Option < A > {
361
+ self . compute_is_empty ( ) ;
362
+ if self . is_empty . unwrap_or_default ( ) {
363
+ return None ;
364
+ }
365
+
361
366
if let Some ( plus_n) = self . start . add_usize ( n) {
362
367
use cmp:: Ordering :: * ;
363
368
364
369
match plus_n. partial_cmp ( & self . end ) {
365
370
Some ( Less ) => {
371
+ self . is_empty = Some ( false ) ;
366
372
self . start = plus_n. add_one ( ) ;
367
373
return Some ( plus_n)
368
374
}
369
375
Some ( Equal ) => {
370
- self . start . replace_one ( ) ;
371
- self . end . replace_zero ( ) ;
376
+ self . is_empty = Some ( true ) ;
372
377
return Some ( plus_n)
373
378
}
374
379
_ => { }
375
380
}
376
381
}
377
382
378
- self . start . replace_one ( ) ;
379
- self . end . replace_zero ( ) ;
383
+ self . is_empty = Some ( true ) ;
380
384
None
381
385
}
382
386
@@ -394,68 +398,24 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
394
398
fn max ( mut self ) -> Option < A > {
395
399
self . next_back ( )
396
400
}
397
-
398
- #[ inline]
399
- fn try_fold < B , F , R > ( & mut self , init : B , mut f : F ) -> R where
400
- Self : Sized , F : FnMut ( B , Self :: Item ) -> R , R : Try < Ok =B >
401
- {
402
- let mut accum = init;
403
- if self . start <= self . end {
404
- loop {
405
- let ( x, done) =
406
- if self . start < self . end {
407
- let n = self . start . add_one ( ) ;
408
- ( mem:: replace ( & mut self . start , n) , false )
409
- } else {
410
- self . end . replace_zero ( ) ;
411
- ( self . start . replace_one ( ) , true )
412
- } ;
413
- accum = f ( accum, x) ?;
414
- if done { break }
415
- }
416
- }
417
- Try :: from_ok ( accum)
418
- }
419
401
}
420
402
421
403
#[ stable( feature = "inclusive_range" , since = "1.26.0" ) ]
422
404
impl < A : Step > DoubleEndedIterator for ops:: RangeInclusive < A > {
423
405
#[ inline]
424
406
fn next_back ( & mut self ) -> Option < A > {
425
- if self . start <= self . end {
426
- if self . start < self . end {
427
- let n = self . end . sub_one ( ) ;
428
- Some ( mem:: replace ( & mut self . end , n) )
429
- } else {
430
- let last = self . end . replace_zero ( ) ;
431
- self . start . replace_one ( ) ;
432
- Some ( last)
433
- }
434
- } else {
435
- None
407
+ self . compute_is_empty ( ) ;
408
+ if self . is_empty . unwrap_or_default ( ) {
409
+ return None ;
436
410
}
437
- }
438
-
439
- #[ inline]
440
- fn try_rfold < B , F , R > ( & mut self , init : B , mut f : F ) -> R where
441
- Self : Sized , F : FnMut ( B , Self :: Item ) -> R , R : Try < Ok =B >
442
- {
443
- let mut accum = init;
444
- if self . start <= self . end {
445
- loop {
446
- let ( x, done) =
447
- if self . start < self . end {
448
- let n = self . end . sub_one ( ) ;
449
- ( mem:: replace ( & mut self . end , n) , false )
450
- } else {
451
- self . start . replace_one ( ) ;
452
- ( self . end . replace_zero ( ) , true )
453
- } ;
454
- accum = f ( accum, x) ?;
455
- if done { break }
456
- }
457
- }
458
- Try :: from_ok ( accum)
411
+ let is_iterating = self . start < self . end ;
412
+ self . is_empty = Some ( !is_iterating) ;
413
+ Some ( if is_iterating {
414
+ let n = self . end . sub_one ( ) ;
415
+ mem:: replace ( & mut self . end , n)
416
+ } else {
417
+ self . end . clone ( )
418
+ } )
459
419
}
460
420
}
461
421
0 commit comments