fix underflow in DoubleEndedIterator::next_back

This commit is contained in:
Alex Burka 2016-02-27 01:56:19 -05:00
parent 1ec3005e45
commit f27a3a304f
2 changed files with 14 additions and 5 deletions

View File

@ -4665,11 +4665,15 @@ impl<A: Step + One> DoubleEndedIterator for ops::RangeInclusive<A> where
NonEmpty { ref mut start, ref mut end } => {
let one = A::one();
let mut n = &*end - &one;
mem::swap(&mut n, end);
if start <= end {
let mut n = &*end - &one;
mem::swap(&mut n, end);
(if n == *start { Some(mem::replace(start, one)) } else { None },
n)
(if n == *start { Some(mem::replace(start, one)) } else { None },
Some(n))
} else {
(Some(mem::replace(end, one)), None)
}
}
};
@ -4677,7 +4681,7 @@ impl<A: Step + One> DoubleEndedIterator for ops::RangeInclusive<A> where
*self = Empty { at: start };
}
Some(n)
n
}
}

View File

@ -99,6 +99,11 @@ pub fn main() {
}
assert_eq!(long, RangeInclusive::Empty { at: 251 });
// check underflow
let mut narrow = 1...0;
assert_eq!(narrow.next_back(), None);
assert_eq!(narrow, RangeInclusive::Empty { at: 0 });
// what happens if you have a nonsense range?
let mut nonsense = 10...5;
assert_eq!(nonsense.next(), None);