GATs are getting closer. We'd like to work an iterator design that is backwards-compatible with existing iterators. Here's our perceived requirements:
- They should be implementable for all existing Iterators.
- They should support streaming if desired.
- They should support collect if they're not streaming.
Here's our idea:
trait Iterator {
type Item<'a = 'self>;
fn next<'a>(&'a mut self) -> Option<Self::Item<'a>>;
fn collect<T>(...) -> T where Self::Item<'_>: 'self {...}
}
This would require an explicit use std::v2::Iterator;
because unfortunately the way to request the collect method would be a breaking change. Other than that, changing existing types over to implement these Iterators v2 should just be a matter of importing Iterators v2 (this would require the crates to provide a semver bump however).
Additionally, for loops would have to use Iterators v2, and a blanket impl for existing Iterators should be provided. That's the easy part tho.
PS: 'self
is meant as a shorthand for "the lifetime of the type". This could also be written as
trait<'b> Iterator where Self: 'b {
type Item<'a = 'b>;
fn next<'a>(&'a mut self) -> Option<Self::Item<'a>>;
fn collect<T>(...) -> T where Self::Item<'_>: 'b, T: ... {...}
}
The main benefit here is that the use of <'a = 'self>
would allow one to omit the <>
when implementing, if so desired. (as well as when matching?) As an example vec![1, 2, 3].into_iter()
would have a type-lifetime of 'static
, which would, effectively, degenerate this trait into the existing Iterator trait.
(Uh we're not entirely sure how lifetimes work also. Would something like this even work? Probably not? The lifetimes of next may need to be tweaked. >.<)