fix underflow in DoubleEndedIterator::next_back
This commit is contained in:
parent
1ec3005e45
commit
f27a3a304f
2 changed files with 14 additions and 5 deletions
|
@ -4665,11 +4665,15 @@ impl<A: Step + One> DoubleEndedIterator for ops::RangeInclusive<A> where
|
||||||
|
|
||||||
NonEmpty { ref mut start, ref mut end } => {
|
NonEmpty { ref mut start, ref mut end } => {
|
||||||
let one = A::one();
|
let one = A::one();
|
||||||
let mut n = &*end - &one;
|
if start <= end {
|
||||||
mem::swap(&mut n, end);
|
let mut n = &*end - &one;
|
||||||
|
mem::swap(&mut n, end);
|
||||||
|
|
||||||
(if n == *start { Some(mem::replace(start, one)) } else { None },
|
(if n == *start { Some(mem::replace(start, one)) } else { None },
|
||||||
n)
|
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 };
|
*self = Empty { at: start };
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(n)
|
n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -99,6 +99,11 @@ pub fn main() {
|
||||||
}
|
}
|
||||||
assert_eq!(long, RangeInclusive::Empty { at: 251 });
|
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?
|
// what happens if you have a nonsense range?
|
||||||
let mut nonsense = 10...5;
|
let mut nonsense = 10...5;
|
||||||
assert_eq!(nonsense.next(), None);
|
assert_eq!(nonsense.next(), None);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue