Open SOF3 opened 8 months ago
This doesn't work because they can be Option::None
which flatten into zero items.
@the8472 Isn't that what the None => (0, Some(0))
line is supposed to handle?
Oh, sorry, I was thinking about flattening an iterator that yields option::IntoIter
, not directly flattening an option iterator.
So iter_if
could also be implemented using itertools::Either
fn iter_if<T>(cond: bool, inner: impl ExactSizeIterator<Item = T>) -> impl ExactSizeIterator<Item = T> {
if cond {
return Either::Left(inner)
}
return Either::Right(iter::empty())
}
Proposal
Motivating examples or use cases
cannot compile because
Solution sketch
In reality, the exact size can be inferred from the underlying iterator directly, something like this:
However this requires specialization on the
size_hint
impl ofFlattenCompat
.Alternatives
Or we could just add an
into_iter_flatten()
method onOption
, which returnsIntoIterFlatten<I>
that basically reimplementsFlatten<option::IntoIter<I>>
.Links and related work