This is a proposal to add two instances:
impl<A, B> ExactSizeIterator for Zip<A, Repeat<B>>
where
A: ExactSizeIterator,
B: Clone,
{
}
impl<A, B> ExactSizeIterator for Zip<Repeat<A>, B>
where
A: Clone,
B: ExactSizeIterator,
{
}
This would appear to be completely safe. .size_hint()
will take the minimum of usize::MAX
(from Repeat
), and the other lower bound.
I've added these instances to core
, and added this coretest:
#[test]
fn zip_repeat_bounds() {
let iter = [1, 2, 3].into_iter().zip(repeat(2));
let (lower, upper) = iter.size_hint();
assert_eq!(lower, 3);
assert_eq!(upper, Some(3));
assert_eq!(iter.len(), 3);
let iter = repeat(2).zip([1, 2, 3].into_iter());
let (lower, upper) = iter.size_hint();
assert_eq!(lower, 3);
assert_eq!(upper, Some(3));
assert_eq!(iter.len(), 3);
}
Which passes.
There is currently only one implementation of ExactSizeIterator
for Zip
:
impl<A, B> ExactSizeIterator for Zip<A, B>
where
A: ExactSizeIterator,
B: ExactSizeIterator {}
Which doesn't overlap with the proposed instances, as Repeat
does not implement ExactSizeIterator
.
If this seems sensible, I'll add a tracking issue, and PR the changes upstream.