Rollup merge of #99434 - timvermeulen:skip_next_non_fused, r=scottmcm
Fix `Skip::next` for non-fused inner iterators `iter.skip(n).next()` will currently call `nth` and `next` in succession on `iter`, without checking whether `nth` exhausts the iterator. Using `?` to propagate a `None` value returned by `nth` avoids this.
This commit is contained in:
commit
e301cd39ad
2 changed files with 12 additions and 1 deletions
|
@ -33,7 +33,7 @@ where
|
||||||
#[inline]
|
#[inline]
|
||||||
fn next(&mut self) -> Option<I::Item> {
|
fn next(&mut self) -> Option<I::Item> {
|
||||||
if unlikely(self.n > 0) {
|
if unlikely(self.n > 0) {
|
||||||
self.iter.nth(crate::mem::take(&mut self.n) - 1);
|
self.iter.nth(crate::mem::take(&mut self.n) - 1)?;
|
||||||
}
|
}
|
||||||
self.iter.next()
|
self.iter.next()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
use core::iter::*;
|
use core::iter::*;
|
||||||
|
|
||||||
|
use super::Unfuse;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_iterator_skip() {
|
fn test_iterator_skip() {
|
||||||
let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19, 20, 30];
|
let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19, 20, 30];
|
||||||
|
@ -190,3 +192,12 @@ fn test_skip_nth_back() {
|
||||||
it.by_ref().skip(2).nth_back(10);
|
it.by_ref().skip(2).nth_back(10);
|
||||||
assert_eq!(it.next_back(), Some(&1));
|
assert_eq!(it.next_back(), Some(&1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_skip_non_fused() {
|
||||||
|
let non_fused = Unfuse::new(0..10);
|
||||||
|
|
||||||
|
// `Skip` would previously exhaust the iterator in this `next` call and then erroneously try to
|
||||||
|
// advance it further. `Unfuse` tests that this doesn't happen by panicking in that scenario.
|
||||||
|
let _ = non_fused.skip(20).next();
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue