-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Optimize &Iterator::nth #25471
Optimize &Iterator::nth #25471
Conversation
This changes the signature of nth but can't break any existing code because: 1. nth can currently only be overridden on Sized structs. 2. Extra `Self: Sized` constraints on methods in impls on Sized structs are no-ops.
r? @brson (rust_highfive has picked a reviewer for you, use r? to override) |
r? @aturon |
Feel free to completely ignore this PR till after the 1.0 dust settles. |
When treating a reference to an iterator as an iterator, proxy calls to nth to the underlying implementation instead of calling the default nth defined in the trait. This allows us to take advantage of optimized nth implementations in by-ref iterators. We can't proxy the other methods due to their sized constraints and the fact that we can't specialize.
Hm, I agree that I'm not 100% certain that the removal of |
@Stebalien could you back out the removal of |
@alexcrichton unfortunately, not without specialization. I would need error: the requirement `I : marker::Sized` appears on the impl method but not on the corresponding trait method [E0276]
fn nth(&mut self, n: usize) -> Option<I::Item> where I: Sized { (**self).nth(n) } |
Yeah the impl of |
There are actually three changes here:
by_ref
calls in the Iterator trait. I needed to remove theby_ref
call from thenth
method to make it object safe (by_ref
isn't object safe) but I ended up removing all of them because they were pointless.nth
object safe. This changesnth
's signature but I'm nearly positive that this change can't break existing code (see the commit message).nth
onby_ref
iterators to make it call though tonth
on the underlying implementation (the type behind the reference). This way, if the underlying implementation provides an optimizednth
,by_ref
iterators can take advantage of it.Part of #24214