-
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
TrustedRandomAccess specialisation for Iterator::cloned when Item: Copy. #44790
Conversation
r? @sfackler (rust_highfive has picked a reviewer for you, use r? to override) |
9870913
to
71c62cb
Compare
This verifies that TrustedRandomAccess has no side effects when the iterator item implements Copy. This also implements TrustedLen and TrustedRandomAccess for str::Bytes.
71c62cb
to
1c589b7
Compare
@@ -499,6 +499,18 @@ unsafe impl<'a, I, T: 'a> TrustedRandomAccess for Cloned<I> | |||
fn may_have_side_effect() -> bool { true } | |||
} | |||
|
|||
#[doc(hidden)] | |||
unsafe impl<'a, I, T: 'a> TrustedRandomAccess for Cloned<I> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this specialization required? It seems totally equivalent to the impl above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It returns false
for may_have_side_effect
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe may_have_side_effect could be removed and it just assumes everything has side effects. Since some .map().zip() invocations optimize ok it could be suspected that the compiler can remove dead code itself. Unfortunately a closer examination of benchmarks is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does assume that Clone
is simply *self
, but it could do weird side-effectful things. I think it's probably reasonable to state that we're allowed to assume that Clone
behaves sanely for Copy
types, but I don't think that's explicitly documented currently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears to be in fact documented: https://doc.rust-lang.org/std/clone/trait.Clone.html#how-can-i-implement-clone
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah great, I missed that.
may_have_side_effect is a crutch for .zip(): by default it calls .next() on the LHS once more than on the RHS, to discover that the LHS is at its end. The specialization aims to preserve this by default, so that nobody notices anything. (Edited) This whole crutch is actually needed, for when the input iterators are different lengths. |
@bors r+ |
📌 Commit 1c589b7 has been approved by |
TrustedRandomAccess specialisation for Iterator::cloned when Item: Copy. This should fix #44424. It also provides a potential fix for more iterators using `Iterator::cloned`.
☀️ Test successful - status-appveyor, status-travis |
@bors try- retry rollup I don't know what bors is doing after the last comment... |
This should fix #44424. It also provides a potential fix for more iterators using
Iterator::cloned
.