Skip to content

Commit

Permalink
Adjust TupleWindows::size_hint
Browse files Browse the repository at this point in the history
Now that `TupleWindows` is lazy, we must adjust the size hint accordingly.
  • Loading branch information
Philippe-Cholet authored and jswrenn committed Oct 6, 2023
1 parent 255ca84 commit 8db450f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
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
12 changes: 9 additions & 3 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 @@ -209,9 +211,13 @@ where
}

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

0 comments on commit 8db450f

Please sign in to comment.