Gankra / collect-rs

Miscellaneous Collections
Apache License 2.0
64 stars 21 forks source link

Something Something Streaming Iterators Something Something? #13

Closed Gankra closed 9 years ago

Gankra commented 9 years ago

cc @burntsushi

tbu- commented 9 years ago

I would be really happy about this, this would simplify writing no-alloc code quite a lot I guess.

reem commented 9 years ago

Just documenting this here for others who come to this issue:

Doing streaming iterators correctly requires HKT, we do not have HKT (yet), therefore we do not have streaming iterators (yet).

To show why this is, let's try to create the trait without HKT:

trait StreamingIterator<T> {
    fn next<'a>(&'a mut self) -> ???
}

We would like to replace the ??? with something like T<'a>, but that is a higher-kinded lifetime, hence, we need that feature to do this correctly.

A common way to encode HKT is to put the requisite types or lifetimes into the trait, like so:

trait StreamingIterator<'a, T> {
    fn next(&'a mut self) -> T;
}

then you can instantiate the trait with T = &'a OtherT. However, this is also not maximally flexible, since it requires adding spurious lifetimes in any bounds or uses of this trait, and also requires a separate bound for each instantiated lifetime (every distinct borrow of the iterator), which is also unnecessary.

Gankra commented 9 years ago

We could get a poor-man's streaming iterator via only supporting &T and &mut T. Not sure what the usecases everyone has (at least one, supporting insertion removal by returning Entries, won't work with this).

reem commented 9 years ago

You can also write one-off streaming iterators (like the cursor in proto::Dlist) meant to be used via while let Some(x) = item.next() but these do not abstract well.

reem commented 9 years ago

I think streaming via &T and &mut T is useful because it addresses cases like reading the lines out of a file using a buffer etc., but I don't know if it's a good idea to provide a broken abstraction when a better one is so easily achieved with HKT.