Skip to content

Commit b56bf64

Browse files
Rollup merge of rust-lang#154190 - Jules-Bertholet:nonfused, r=scottmcm
Don't fuse in `MapWindows` cc rust-lang#87155 Fusing makes the iterator larger, slower, more complicated, and less useful. Users who need fusing behavior can always use `.fuse()`, but there is no way to get non-fusing behavior from the fused version. @rustbot label A-iterators
2 parents 4b52ede + 0a49163 commit b56bf64

3 files changed

Lines changed: 49 additions & 34 deletions

File tree

‎library/core/src/iter/adapters/map_windows.rs‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ pub struct MapWindows<I: Iterator, F, const N: usize> {
1414
}
1515

1616
struct MapWindowsInner<I: Iterator, const N: usize> {
17-
// We fuse the inner iterator because there shouldn't be "holes" in
18-
// the sliding window. Once the iterator returns a `None`, we make
19-
// our `MapWindows` iterator return `None` forever.
20-
iter: Option<I>,
17+
iter: I,
2118
// Since iterators are assumed lazy, i.e. it only yields an item when
2219
// `Iterator::next()` is called, and `MapWindows` is not an exception.
2320
//
@@ -26,7 +23,7 @@ struct MapWindowsInner<I: Iterator, const N: usize> {
2623
// we collect the first `N` items yielded from the inner iterator and
2724
// put it into the buffer.
2825
//
29-
// When the inner iterator has returned a `None` (i.e. fused), we take
26+
// When the inner iterator has returned a `None`, we take
3027
// away this `buffer` and leave it `None` to reclaim its resources.
3128
//
3229
// FIXME: should we shrink the size of `buffer` using niche optimization?
@@ -64,19 +61,16 @@ impl<I: Iterator, F, const N: usize> MapWindows<I, F, N> {
6461
impl<I: Iterator, const N: usize> MapWindowsInner<I, N> {
6562
#[inline]
6663
fn new(iter: I) -> Self {
67-
Self { iter: Some(iter), buffer: None }
64+
Self { iter, buffer: None }
6865
}
6966

7067
fn next_window(&mut self) -> Option<&[I::Item; N]> {
71-
let iter = self.iter.as_mut()?;
7268
match self.buffer {
7369
// It is the first time to advance. We collect
7470
// the first `N` items from `self.iter` to initialize `self.buffer`.
75-
None => self.buffer = Buffer::try_from_iter(iter),
76-
Some(ref mut buffer) => match iter.next() {
71+
None => self.buffer = Buffer::try_from_iter(&mut self.iter),
72+
Some(ref mut buffer) => match self.iter.next() {
7773
None => {
78-
// Fuse the inner iterator since it yields a `None`.
79-
self.iter.take();
8074
self.buffer.take();
8175
}
8276
// Advance the iterator. We first call `next` before changing our buffer
@@ -89,8 +83,7 @@ impl<I: Iterator, const N: usize> MapWindowsInner<I, N> {
8983
}
9084

9185
fn size_hint(&self) -> (usize, Option<usize>) {
92-
let Some(ref iter) = self.iter else { return (0, Some(0)) };
93-
let (lo, hi) = iter.size_hint();
86+
let (lo, hi) = self.iter.size_hint();
9487
if self.buffer.is_some() {
9588
// If the first `N` items are already yielded by the inner iterator,
9689
// the size hint is then equal to the that of the inner iterator's.
@@ -253,12 +246,10 @@ where
253246
}
254247
}
255248

256-
// Note that even if the inner iterator not fused, the `MapWindows` is still fused,
257-
// because we don't allow "holes" in the mapping window.
258249
#[unstable(feature = "iter_map_windows", issue = "87155")]
259250
impl<I, F, R, const N: usize> FusedIterator for MapWindows<I, F, N>
260251
where
261-
I: Iterator,
252+
I: FusedIterator,
262253
F: FnMut(&[I::Item; N]) -> R,
263254
{
264255
}

‎library/core/src/iter/traits/iterator.rs‎

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,11 +1657,6 @@ pub const trait Iterator {
16571657
/// items yielded by `self`). If 𝑘 is less than `N`, this method yields an
16581658
/// empty iterator.
16591659
///
1660-
/// The returned iterator implements [`FusedIterator`], because once `self`
1661-
/// returns `None`, even if it returns a `Some(T)` again in the next iterations,
1662-
/// we cannot put it into a contiguous array buffer, and thus the returned iterator
1663-
/// should be fused.
1664-
///
16651660
/// [`slice::windows()`]: slice::windows
16661661
/// [`FusedIterator`]: crate::iter::FusedIterator
16671662
///
@@ -1722,7 +1717,7 @@ pub const trait Iterator {
17221717
/// assert_eq!(it.next(), None);
17231718
/// ```
17241719
///
1725-
/// For non-fused iterators, they are fused after `map_windows`.
1720+
/// For non-fused iterators, the window is reset after `None` is yielded.
17261721
///
17271722
/// ```
17281723
/// #![feature(iter_map_windows)]
@@ -1739,44 +1734,47 @@ pub const trait Iterator {
17391734
/// let val = self.state;
17401735
/// self.state = self.state + 1;
17411736
///
1742-
/// // yields `0..5` first, then only even numbers since `6..`.
1743-
/// if val < 5 || val % 2 == 0 {
1744-
/// Some(val)
1745-
/// } else {
1737+
/// // Skip every 5th number
1738+
/// if (val + 1) % 5 == 0 {
17461739
/// None
1740+
/// } else {
1741+
/// Some(val)
17471742
/// }
17481743
/// }
17491744
/// }
17501745
///
17511746
///
17521747
/// let mut iter = NonFusedIterator::default();
17531748
///
1754-
/// // yields 0..5 first.
17551749
/// assert_eq!(iter.next(), Some(0));
17561750
/// assert_eq!(iter.next(), Some(1));
17571751
/// assert_eq!(iter.next(), Some(2));
17581752
/// assert_eq!(iter.next(), Some(3));
1759-
/// assert_eq!(iter.next(), Some(4));
1760-
/// // then we can see our iterator going back and forth
17611753
/// assert_eq!(iter.next(), None);
1754+
/// assert_eq!(iter.next(), Some(5));
17621755
/// assert_eq!(iter.next(), Some(6));
1763-
/// assert_eq!(iter.next(), None);
1756+
/// assert_eq!(iter.next(), Some(7));
17641757
/// assert_eq!(iter.next(), Some(8));
17651758
/// assert_eq!(iter.next(), None);
1759+
/// assert_eq!(iter.next(), Some(10));
1760+
/// assert_eq!(iter.next(), Some(11));
17661761
///
1767-
/// // however, with `.map_windows()`, it is fused.
17681762
/// let mut iter = NonFusedIterator::default()
17691763
/// .map_windows(|arr: &[_; 2]| *arr);
17701764
///
17711765
/// assert_eq!(iter.next(), Some([0, 1]));
17721766
/// assert_eq!(iter.next(), Some([1, 2]));
17731767
/// assert_eq!(iter.next(), Some([2, 3]));
1774-
/// assert_eq!(iter.next(), Some([3, 4]));
17751768
/// assert_eq!(iter.next(), None);
17761769
///
1777-
/// // it will always return `None` after the first time.
1778-
/// assert_eq!(iter.next(), None);
1770+
/// assert_eq!(iter.next(), Some([5, 6]));
1771+
/// assert_eq!(iter.next(), Some([6, 7]));
1772+
/// assert_eq!(iter.next(), Some([7, 8]));
17791773
/// assert_eq!(iter.next(), None);
1774+
///
1775+
/// assert_eq!(iter.next(), Some([10, 11]));
1776+
/// assert_eq!(iter.next(), Some([11, 12]));
1777+
/// assert_eq!(iter.next(), Some([12, 13]));
17801778
/// assert_eq!(iter.next(), None);
17811779
/// ```
17821780
#[inline]

‎library/coretests/tests/iter/adapters/map_windows.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,29 @@ fn test_size_hint() {
284284
check_size_hint::<5>((5, Some(5)), (1, Some(1)));
285285
check_size_hint::<5>((5, Some(10)), (1, Some(6)));
286286
}
287+
288+
#[test]
289+
fn test_unfused() {
290+
#[derive(Default)]
291+
struct UnfusedIter(usize);
292+
impl Iterator for UnfusedIter {
293+
type Item = usize;
294+
295+
fn next(&mut self) -> Option<usize> {
296+
let curr = self.0;
297+
self.0 += 1;
298+
if curr % 7 == 0 { None } else { Some(curr) }
299+
}
300+
}
301+
302+
let mut iter = UnfusedIter(1).map_windows(|a: &[_; 3]| *a);
303+
assert_eq!(iter.by_ref().collect::<Vec<_>>(), vec![[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]]);
304+
assert_eq!(
305+
iter.by_ref().collect::<Vec<_>>(),
306+
vec![[8, 9, 10], [9, 10, 11], [10, 11, 12], [11, 12, 13]]
307+
);
308+
assert_eq!(
309+
iter.by_ref().collect::<Vec<_>>(),
310+
vec![[15, 16, 17], [16, 17, 18], [17, 18, 19], [18, 19, 20]]
311+
);
312+
}

0 commit comments

Comments
 (0)