Skip to content

Commit e301cd3

Browse files
authored
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.
2 parents 415f7e1 + e52837c commit e301cd3

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

library/core/src/iter/adapters/skip.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ where
3333
#[inline]
3434
fn next(&mut self) -> Option<I::Item> {
3535
if unlikely(self.n > 0) {
36-
self.iter.nth(crate::mem::take(&mut self.n) - 1);
36+
self.iter.nth(crate::mem::take(&mut self.n) - 1)?;
3737
}
3838
self.iter.next()
3939
}

library/core/tests/iter/adapters/skip.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use core::iter::*;
22

3+
use super::Unfuse;
4+
35
#[test]
46
fn test_iterator_skip() {
57
let xs = [0, 1, 2, 3, 5, 13, 15, 16, 17, 19, 20, 30];
@@ -190,3 +192,12 @@ fn test_skip_nth_back() {
190192
it.by_ref().skip(2).nth_back(10);
191193
assert_eq!(it.next_back(), Some(&1));
192194
}
195+
196+
#[test]
197+
fn test_skip_non_fused() {
198+
let non_fused = Unfuse::new(0..10);
199+
200+
// `Skip` would previously exhaust the iterator in this `next` call and then erroneously try to
201+
// advance it further. `Unfuse` tests that this doesn't happen by panicking in that scenario.
202+
let _ = non_fused.skip(20).next();
203+
}

0 commit comments

Comments
 (0)