-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Add custom nth_back to Skip #60454
Add custom nth_back to Skip #60454
Conversation
r? @cramertj (rust_highfive has picked a reviewer for you, use r? to override) |
r? @scottmcm |
Looks correct to me, though I don't think we want |
a516851
to
1edecac
Compare
Didn't notice that, I changed it |
Look good, thanks! @bors r+ rollup |
📌 Commit 1edecacf2021a3381f447c949e6dc3018f4b0be4 has been approved by |
⌛ Testing commit 1edecacf2021a3381f447c949e6dc3018f4b0be4 with merge 8e9bb42c1edf098a01f7a18737f23ad257978854... |
if n < self.len() { | ||
self.iter.nth_back(n) | ||
} else { | ||
None |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
@bors r- retry |
Isn't this still a concern with the current implementation? |
b7fe202
to
edd4879
Compare
@timvermeulen thanks :) |
fn nth_back(&mut self, n: usize) -> Option<I::Item> { | ||
if n < self.len() { | ||
self.iter.nth_back(n) | ||
} else { |
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.
In case n >= self.len() && self.len() != 0
, additionally you need to do self.iter.nth_back(self.len() - 1);
(or whatever that takes self.len()
-items from the back) since the underlying iterator could have side effects.
let mut v = vec![];
let mut it = [1, 2, 3, 4, 5]
.iter()
.cloned()
.inspect(|x| v.push(*x))
.skip(1);
assert_eq!(it.len(), 4);
assert_eq!(it.nth_back(4), None);
assert_eq!(v, vec![5, 4, 3, 2]);
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 let _ =
and not just the call to nth_back
?
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 was just for expressing intent to throw away the return value of nth_back
, and I don't meant it is actually needed.
@@ -2280,6 +2280,32 @@ fn test_skip_try_folds() { | |||
assert_eq!(iter.next_back(), Some(24)); | |||
} | |||
|
|||
#[test] | |||
fn test_skip_nth_back() { |
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 the comment above was addressed; if disagree please add a test that demonstrates that the underlying iterator is still advanced.
(You could do that with inspect, or something like let mut it = ...; it.by_ref().skip(10).nth_back(100); assert_eq!(it.next_back(), ...);
)
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.
Ok, I didn't understand you meant that.
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.
You're now calling self.len()
three times, maybe it's better to store it in a variable first.
8e83410
to
2966f58
Compare
Ping from triage, @acrrd what's the status of this? Are there still open review comments to be addressed, or is this waiting on someone to approve? |
@jonas-schievink I think all comments are addressed, waiting for approval. |
Sorry for the delay re-reviewing, this looks great now! @bors r+ rollup |
📌 Commit e80a375 has been approved by |
Add custom nth_back to Skip Implementation of nth_back for Skip. Part of rust-lang#54054
Implementation of nth_back for Skip.
Part of #54054