diff --git a/src/adaptors/multi_product.rs b/src/adaptors/multi_product.rs index bd74e12c4..8e5a261a7 100644 --- a/src/adaptors/multi_product.rs +++ b/src/adaptors/multi_product.rs @@ -2,6 +2,8 @@ use alloc::vec::Vec; +use crate::size_hint; + #[derive(Clone)] /// An iterator adaptor that iterates over the cartesian product of /// multiple iterators of type `I`. @@ -152,6 +154,27 @@ where } } + fn size_hint(&self) -> (usize, Option) { + match &self.0 { + None => (0, Some(0)), + Some(MultiProductInner { iters, cur }) => { + if cur.is_none() { + iters + .iter() + .map(|iter| iter.iter_orig.size_hint()) + .fold((1, Some(1)), size_hint::mul) + } else if let [first, tail @ ..] = &iters[..] { + tail.iter().fold(first.iter.size_hint(), |mut sh, iter| { + sh = size_hint::mul(sh, iter.iter_orig.size_hint()); + size_hint::add(sh, iter.iter.size_hint()) + }) + } else { + (0, Some(0)) + } + } + } + } + fn last(self) -> Option { let MultiProductInner { iters, cur } = self.0?; if let Some(values) = cur {