Fix exhaustion of inclusive range try_fold and try_rfold
This commit is contained in:
parent
eb5b096886
commit
4fed67f942
2 changed files with 33 additions and 5 deletions
|
@ -389,6 +389,10 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
|
|||
{
|
||||
self.compute_is_empty();
|
||||
|
||||
if self.is_empty() {
|
||||
return Try::from_ok(init);
|
||||
}
|
||||
|
||||
let mut accum = init;
|
||||
|
||||
while self.start < self.end {
|
||||
|
@ -397,11 +401,12 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
|
|||
accum = f(accum, n)?;
|
||||
}
|
||||
|
||||
self.is_empty = Some(true);
|
||||
|
||||
if self.start == self.end {
|
||||
accum = f(accum, self.start.clone())?;
|
||||
}
|
||||
|
||||
self.is_empty = Some(true);
|
||||
Try::from_ok(accum)
|
||||
}
|
||||
|
||||
|
@ -445,6 +450,10 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
|
|||
{
|
||||
self.compute_is_empty();
|
||||
|
||||
if self.is_empty() {
|
||||
return Try::from_ok(init);
|
||||
}
|
||||
|
||||
let mut accum = init;
|
||||
|
||||
while self.start < self.end {
|
||||
|
@ -453,11 +462,12 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
|
|||
accum = f(accum, n)?;
|
||||
}
|
||||
|
||||
self.is_empty = Some(true);
|
||||
|
||||
if self.start == self.end {
|
||||
accum = f(accum, self.start.clone())?;
|
||||
}
|
||||
|
||||
self.is_empty = Some(true);
|
||||
Try::from_ok(accum)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1738,19 +1738,37 @@ fn test_range_inclusive_folds() {
|
|||
assert_eq!((1..=10).sum::<i32>(), 55);
|
||||
assert_eq!((1..=10).rev().sum::<i32>(), 55);
|
||||
|
||||
let mut it = 40..=50;
|
||||
let mut it = 44..=50;
|
||||
assert_eq!(it.try_fold(0, i8::checked_add), None);
|
||||
assert_eq!(it, 44..=50);
|
||||
assert_eq!(it, 47..=50);
|
||||
assert_eq!(it.try_fold(0, i8::checked_add), None);
|
||||
assert_eq!(it, 50..=50);
|
||||
assert_eq!(it.try_fold(0, i8::checked_add), Some(50));
|
||||
assert!(it.is_empty());
|
||||
assert_eq!(it.try_fold(0, i8::checked_add), Some(0));
|
||||
assert!(it.is_empty());
|
||||
|
||||
let mut it = 40..=47;
|
||||
assert_eq!(it.try_rfold(0, i8::checked_add), None);
|
||||
assert_eq!(it, 44..=47);
|
||||
assert_eq!(it, 40..=44);
|
||||
assert_eq!(it.try_rfold(0, i8::checked_add), None);
|
||||
assert_eq!(it, 40..=41);
|
||||
assert_eq!(it.try_rfold(0, i8::checked_add), Some(81));
|
||||
assert!(it.is_empty());
|
||||
assert_eq!(it.try_rfold(0, i8::checked_add), Some(0));
|
||||
assert!(it.is_empty());
|
||||
|
||||
let mut it = 10..=20;
|
||||
assert_eq!(it.try_fold(0, |a,b| Some(a+b)), Some(165));
|
||||
assert!(it.is_empty());
|
||||
assert_eq!(it.try_fold(0, |a,b| Some(a+b)), Some(0));
|
||||
assert!(it.is_empty());
|
||||
|
||||
let mut it = 10..=20;
|
||||
assert_eq!(it.try_rfold(0, |a,b| Some(a+b)), Some(165));
|
||||
assert!(it.is_empty());
|
||||
assert_eq!(it.try_rfold(0, |a,b| Some(a+b)), Some(0));
|
||||
assert!(it.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in a new issue