-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Make Map::last
skip closure calls until last element
#146674
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
Conversation
r? @ibraheemdev rustbot has assigned @ibraheemdev. Use |
r? @jhpratt |
|
Arguably one could use the infinite fold for side effects? It would be kind of weird though. |
@asquared31415 true, I may have to take that out then... |
Adapters like |
@LIParadise I believe that assert should hold, but let me add a test. |
@LIParadise I was wrong, but I was able to fix it... |
If that's the case, I'm afraid there are quite a few types that's |
Also crates like itertools might have a word or two.
Part of me agree that |
@LIParadise I think #146410 (comment) describes it well. Alternatively you can imagine two infinite sequences with their infinite ends glued together. It amounts to the same thing. |
I'm off-rotation as I don't have the time to handle nontrivial reviews at the moment. r? libs |
|
||
#[track_caller] | ||
fn count(self) -> usize { | ||
fn fold<B, F>(self, _init: B, _f: F) -> B |
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.
I don't think this is correct since even for an infinite iterator, you would still expect f
to be called multiple times, which may have visible side-effects.
cc @the8472 |
a2b9813
to
88a99cd
Compare
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
I've removed the change from |
last
to Map
Self: Sized, | ||
{ | ||
self.iter.last().map(&mut self.f) | ||
} |
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.
I don't think we should do this; it skips calling f
on all of the elements up to the last one, which doesn't seem right to me, especially since we do iterate over those elements (i.e., we aren't specialized in some way for DoubleEndedIterator).
If we do proceed with this it'll need libs-api FCP, so nominating this PR.
last
to Map
Map::last
skip closure calls until last element
This would be an observable behavior change, and would likely cause subtle breakage. I would object to merging this as proposed. Playground link demonstrating observable behavior: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=09b0005c5034bd8363f35180c605f1e8 |
This was discussed in the @rust-lang/libs-api meeting and we all agreed that this is definitely a breaking change since it prevents side-effects of |
I found this code in the wild that would break due to this PR. It's in a repo containing "example" code though. https://github.com/eliovir/rust-examples/blob/d46017e846c8a5331512739d195ebc9b91a29f5e/find_max.rs#L72 |
I've opened #147195 for the tests for the already accepted behavior, so I think this can be closed. |
A continuation of #146410
which changes the direct. Also added tests for the new behavior.count
impl for afold
impl, to which the defaultcount
delegatesEDIT: adds a
last
toMap
.