Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tuple_windows as lazy #602

Merged
merged 4 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion src/size_hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ pub fn add_scalar(sh: SizeHint, x: usize) -> SizeHint {
}

/// Subtract `x` correctly from a `SizeHint`.
#[cfg(feature = "use_alloc")]
#[inline]
pub fn sub_scalar(sh: SizeHint, x: usize) -> SizeHint {
let (mut low, mut hi) = sh;
Expand Down
42 changes: 21 additions & 21 deletions src/tuple_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::iter::Fuse;
use std::iter::FusedIterator;
use std::marker::PhantomData;

use crate::size_hint;

// `HomogeneousTuple` is a public facade for `TupleCollect`, allowing
// tuple-related methods to be used by clients in generic contexts, while
// hiding the implementation details of `TupleCollect`.
Expand Down Expand Up @@ -172,25 +174,13 @@ where
}

/// Create a new tuple windows iterator.
pub fn tuple_windows<I, T>(mut iter: I) -> TupleWindows<I, T>
pub fn tuple_windows<I, T>(iter: I) -> TupleWindows<I, T>
where
I: Iterator<Item = T::Item>,
T: HomogeneousTuple,
T::Item: Clone,
{
use std::iter::once;

let mut last = None;
if T::num_items() != 1 {
// put in a duplicate item in front of the tuple; this simplifies
// .next() function.
if let Some(item) = iter.next() {
let iter = once(item.clone()).chain(once(item)).chain(&mut iter);
last = T::collect_from_iter_no_buf(iter);
}
}

TupleWindows { iter, last }
TupleWindows { last: None, iter }
}

impl<I, T> Iterator for TupleWindows<I, T>
Expand All @@ -205,19 +195,29 @@ where
if T::num_items() == 1 {
return T::collect_from_iter_no_buf(&mut self.iter);
}
if let Some(ref mut last) = self.last {
if let Some(new) = self.iter.next() {
if let Some(new) = self.iter.next() {
if let Some(ref mut last) = self.last {
last.left_shift_push(new);
return Some(last.clone());
Some(last.clone())
} else {
use std::iter::once;
let iter = once(new).chain(&mut self.iter);
self.last = T::collect_from_iter_no_buf(iter);
self.last.clone()
}
} else {
None
}
None
}

fn size_hint(&self) -> (usize, Option<usize>) {
// At definition, `T::num_items() - 1` are collected
// so each remaining item in `iter` will lead to an item.
self.iter.size_hint()
let mut sh = self.iter.size_hint();
// Adjust the size hint at the beginning
// OR when `num_items == 1` (but it does not change the size hint).
if self.last.is_none() {
sh = size_hint::sub_scalar(sh, T::num_items() - 1);
}
sh
}
}

Expand Down