10
10
11
11
use convert:: TryFrom ;
12
12
use mem;
13
- use ops:: { self , Add , Sub } ;
13
+ use ops:: { self , Add , Sub , Try } ;
14
14
use usize;
15
15
16
16
use super :: { FusedIterator , TrustedLen } ;
@@ -406,6 +406,26 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
406
406
fn max ( mut self ) -> Option < A > {
407
407
self . next_back ( )
408
408
}
409
+
410
+ #[ inline]
411
+ fn try_fold < B , F , R > ( & mut self , init : B , mut f : F ) -> R where
412
+ Self : Sized , F : FnMut ( B , Self :: Item ) -> R , R : Try < Ok =B >
413
+ {
414
+ let mut accum = init;
415
+
416
+ self . compute_is_empty ( ) ;
417
+ if self . is_empty . unwrap_or_default ( ) {
418
+ return Try :: from_ok ( accum) ;
419
+ }
420
+
421
+ while self . start < self . end {
422
+ let n = self . start . add_one ( ) ;
423
+ accum = f ( accum, mem:: replace ( & mut self . start , n) ) ?;
424
+ }
425
+
426
+ self . is_empty = Some ( true ) ;
427
+ f ( accum, self . start . clone ( ) )
428
+ }
409
429
}
410
430
411
431
#[ stable( feature = "inclusive_range" , since = "1.26.0" ) ]
@@ -425,6 +445,26 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
425
445
self . end . clone ( )
426
446
} )
427
447
}
448
+
449
+ #[ inline]
450
+ fn try_rfold < B , F , R > ( & mut self , init : B , mut f : F ) -> R where
451
+ Self : Sized , F : FnMut ( B , Self :: Item ) -> R , R : Try < Ok =B >
452
+ {
453
+ let mut accum = init;
454
+
455
+ self . compute_is_empty ( ) ;
456
+ if self . is_empty . unwrap_or_default ( ) {
457
+ return Try :: from_ok ( accum) ;
458
+ }
459
+
460
+ while self . start < self . end {
461
+ let n = self . end . sub_one ( ) ;
462
+ accum = f ( accum, mem:: replace ( & mut self . end , n) ) ?;
463
+ }
464
+
465
+ self . is_empty = Some ( true ) ;
466
+ f ( accum, self . end . clone ( ) )
467
+ }
428
468
}
429
469
430
470
#[ stable( feature = "fused" , since = "1.26.0" ) ]
0 commit comments