-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make iteration pattern consistent #44
Comments
The two patterns are:
I agree that we're currently inconsistent in our usage of this. We started with normal iterators, and have been moving towards fallible iterators. We want to make the API easy to use. There's two aspects to this though. It must be easy to use correctly, and hard to use incorrectly. There may be tradeoffs between these. My feeling is that fallible iterators are the better tradeoff.
The for loop comparison is between these:
and
Normal iterators are easier to use here, but I don't think the difference is that great.
I don't think that works with normal iterators (I tried to figure out how you would do it, but couldn't... eg if I change one of the benchmarks to use that instead then it doesn't compile, but please correct me if I am wrong). The code you give does work with fallible iterators though if you use the
For most of our iterators we could easily do this by setting the |
It's definitely possible to go from But either approach makes sense to me. The fallible iterator crate looks pretty nice. :-) |
FWIW, https://doc.rust-lang.org/nightly/std/iter/trait.FromIterator.html: impl<A, E, V> FromIterator<Result<A, E>> for Result<V, E> where V: FromIterator<A> I am leaning with @philipc on this one: with Concrete things to do in this issue:
|
* Change `fn next_foo(&mut self) -> ParseResult<Option<T>>` to `fn next(...) -> ...`. * Add a `FallibleIterator` implementation for that method. By still having a non-trait-impl `next` method we don't force API consumers to `use FallibleIterator;` unless they want the helper methods. * Change existing `Iterator<Item=ParseResult<T>>` implementations over to `FallibleIterator<Item=T>` implementations. Fixes gimli-rs#44.
* Change `fn next_foo(&mut self) -> ParseResult<Option<T>>` to `fn next(...) -> ...`. * Add a `FallibleIterator` implementation for that method. By still having a non-trait-impl `next` method we don't force API consumers to `use FallibleIterator;` unless they want the helper methods. * Change existing `Iterator<Item=ParseResult<T>>` implementations over to `FallibleIterator<Item=T>` implementations. Fixes gimli-rs#44.
* Change `fn next_foo(&mut self) -> ParseResult<Option<T>>` to `fn next(...) -> ...`. * Add a `FallibleIterator` implementation for that method. By still having a non-trait-impl `next` method we don't force API consumers to `use FallibleIterator;` unless they want the helper methods. * Change existing `Iterator<Item=ParseResult<T>>` implementations over to `FallibleIterator<Item=T>` implementations. Fixes gimli-rs#44.
Currently there are two patterns of iteration used across the library:
Iterator
trait withItem=ParseResult
(eg,DebugInfo::units
/UnitHeadersIter
, andDebugTypes::units
/TypeUnitHeadersIter
BlahIter
type that does not implementIterator
, and instead has anext
method that returns aParseResult<Option<Blah>>
.From https://github.com/fitzgen/gimli/pull/37#issuecomment-239650372 it seems that the preferred iteration method is the second one.
I'm mostly interested in consistency, but will say that while I get the argument in the linked comment, I still prefer using
Iterator<Item=ParseResult<Blah>>
. In addition to the more ergonomicfor _ in
syntax, it also gets you the ability to do things like:which is much more straightforward than
The potential for infinite loops on error can be fixed by having a
done
flag in the iterator struct, which gets set on the first error, or on successful completion of iteration. This is done in the tar crate, for example: https://github.com/alexcrichton/tar-rs/blob/master/src/archive.rs#L442.The text was updated successfully, but these errors were encountered: