Skip to content

Commit c70f805

Browse files
Tuples::size_hint: add jswrenn's comments
Co-authored-by: Jack Wrenn <[email protected]>
1 parent 6d1a35a commit c70f805

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/tuple_impl.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,19 @@ where
107107
}
108108

109109
fn size_hint(&self) -> (usize, Option<usize>) {
110-
let buf_len = T::buffer_len(&self.buf);
111-
let (mut low, mut hi) = self.iter.size_hint();
112-
low = add_then_div(low, buf_len, T::num_items()).unwrap_or(usize::MAX);
113-
hi = hi.and_then(|elt| add_then_div(elt, buf_len, T::num_items()));
114-
(low, hi)
110+
// The number of elts we've drawn from the underlying iterator, but have
111+
// not yet produced as a tuple.
112+
let buffered = T::buffer_len(&self.buf);
113+
// To that, we must add the size estimates of the underlying iterator.
114+
let (mut unbuffered_lo, mut unbuffered_hi) = self.iter.size_hint();
115+
// The total low estimate is the sum of the already-buffered elements,
116+
// plus the low estimate of remaining unbuffered elements, divided by
117+
// the tuple size.
118+
let total_lo = add_then_div(unbuffered_lo, buffered, T::num_items()).unwrap_or(usize::MAX);
119+
// And likewise for the total high estimate, but using the high estimate
120+
// of the remaining unbuffered elements.
121+
let total_hi = unbuffered_hi.and_then(|hi| add_then_div(hi, buffered, T::num_items()));
122+
(total_lo, total_hi)
115123
}
116124
}
117125

0 commit comments

Comments
 (0)