Blog series: generators

Honestly I am baffled by the last blog post. The whole premise sounds wrong. I think here:

But we want to be able to make this into an Iterator with an Item of io::Result<usize>

Author confuses "how we want to" and "how we are used to".

I would like to argue that most of the code which uses Iterator<Item=Result<T, E>> stops iteration after the first encountered error. For example some of my code is plagued with lines like these:

for record in iterator {
    let record = record?;
    // process record
}

This is why I've wrote the following proposal:

And to me it looks like the author wants to set in stone automatic Generator -> Iterator conversion, while the linked proposal argues that Iterator<T> -> Generator<T, ()> is a much more natural generalization. And it's especially strange for me considering the fact that @withoutboats participated in the Pre-RFC discussion.

But indeed there are cases when we want to convert generator into iterator. Why don't just add methods to Generator trait which will do the conversion? So instead of iter::try_gen(generator()) we for example will write generator().into_iterator() for Iterator which will ignore result and generator().into_try_iterator() for iterator which will convert Generator<T1, Result<T2, E>> to Iterator<Result<T1, E>>.

Why can't we just write a wrapper struct GeneratorTryWrapper<G: Generator<T1, Result<T2, E>>> which will implement Iterator<Result<T1, E>>?

10 Likes