mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 23:34:48 +00:00
fix underflow in DoubleEndedIterator::next_back
This commit is contained in:
parent
1ec3005e45
commit
f27a3a304f
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user