Skip to content

Commit

Permalink
Better Product::size_hint
Browse files Browse the repository at this point in the history
`b` size hint was computed even when not needed.
  • Loading branch information
Philippe-Cholet authored and jswrenn committed Nov 13, 2023
1 parent 8d07f6b commit bf2b012
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,13 @@ where
}

fn size_hint(&self) -> (usize, Option<usize>) {
let has_cur = matches!(self.a_cur, Some(Some(_))) as usize;
// Not ExactSizeIterator because size may be larger than usize
let (b_min, b_max) = self.b.size_hint();

// Compute a * b_orig + b for both lower and upper bound
size_hint::add(
size_hint::mul(self.a.size_hint(), self.b_orig.size_hint()),
(b_min * has_cur, b_max.map(move |x| x * has_cur)),
)
let mut sh = size_hint::mul(self.a.size_hint(), self.b_orig.size_hint());
if matches!(self.a_cur, Some(Some(_))) {
sh = size_hint::add(sh, self.b.size_hint());
}
sh
}

fn fold<Acc, G>(self, mut accum: Acc, mut f: G) -> Acc
Expand Down

0 comments on commit bf2b012

Please sign in to comment.