libs-team
                                
                                 libs-team copied to clipboard
                                
                                    libs-team copied to clipboard
                            
                            
                            
                        Implement ExactSizeIterator for Flatten<option::IntoIter<I>>
Proposal
Motivating examples or use cases
fn iter_if<T>(cond: bool, inner: impl ExactSizeIterator<Item = T>) -> impl ExactSizeIterator<Item = T> {
    cond.then(|| inner).into_iter().flatten()
}
cannot compile because
error[E0277]: the trait bound
Flatten<std::option::IntoIter<impl ExactSizeIterator<Item = T>>>: ExactSizeIteratoris not satisfied
Solution sketch
In reality, the exact size can be inferred from the underlying iterator directly, something like this:
match self.inner.as_ref() {
    None => (0, Some(0)),
    Some(iter) => iter.size_hint(),
}
However this requires specialization on the size_hint impl of FlattenCompat.
Alternatives
Or we could just add an into_iter_flatten() method on Option, which returns IntoIterFlatten<I> that basically reimplements Flatten<option::IntoIter<I>>.
Links and related work
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())
}