Skip to content
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

Implement more Iterator methods on core::iter::Repeat #85338

Merged
merged 1 commit into from
May 18, 2021
Merged

Implement more Iterator methods on core::iter::Repeat #85338

merged 1 commit into from
May 18, 2021

Conversation

lopopolo
Copy link
Contributor

@lopopolo lopopolo commented May 15, 2021

core::iter::Repeat always returns the same element, which means we can
do better than implementing most Iterator methods in terms of
Iterator::next.

Fixes #81292.

#81292 raises the question of whether these changes violate the contract of core::iter::Repeat, but as far as I can tell core::iter::repeat doesn't make any guarantees around how it calls Clone::clone.

`core::iter::Repeat` always returns the same element, which means we can
do better than implementing most `Iterator` methods in terms of
`Iterator::next`.

Fixes #81292.
@rust-highfive
Copy link
Collaborator

r? @dtolnay

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 15, 2021
}

fn last(self) -> Option<A> {
loop {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't self.element be the last one?
Just optimized from running infinite time to a no-op :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeat is an infinite iterator so it has no last element.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it has last element according to next_back implementation.

Copy link
Contributor

@hbina hbina Jul 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please have this behavior instead? I am using it to represent something like [[*] in tr where it expands however much is required. To make this easier I need last() to actually return something to act as a fallback in case the sequences in set1 is longer than set2.

Is there something to be gain from this being an infinite loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code does not change the behavior of repeat when calling last from Repeat as it existed prior to this PR. The default implementation it replaces calls next until None is returned, which never happens for Repeat.

@joshtriplett
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented May 17, 2021

📌 Commit 963bd3b has been approved by joshtriplett

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 17, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 18, 2021
…gh-81292, r=joshtriplett

Implement more Iterator methods on core::iter::Repeat

`core::iter::Repeat` always returns the same element, which means we can
do better than implementing most `Iterator` methods in terms of
`Iterator::next`.

Fixes rust-lang#81292.

rust-lang#81292 raises the question of whether these changes violate the contract of `core::iter::Repeat`, but as far as I can tell `core::iter::repeat` doesn't make any guarantees around how it calls `Clone::clone`.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 18, 2021
…laumeGomez

Rollup of 7 pull requests

Successful merges:

 - rust-lang#84587 (rustdoc: Make "rust code block is empty" and "could not parse code block" warnings a lint (`INVALID_RUST_CODEBLOCKS`))
 - rust-lang#85280 (Toggle-wrap items differently than top-doc.)
 - rust-lang#85338 (Implement more Iterator methods on core::iter::Repeat)
 - rust-lang#85339 (Report an error if a lang item has the wrong number of generic arguments)
 - rust-lang#85369 (Suggest borrowing if a trait implementation is found for &/&mut <type>)
 - rust-lang#85393 (Suppress spurious errors inside `async fn`)
 - rust-lang#85415 (Clean up remnants of BorrowOfPackedField)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit a181806 into rust-lang:master May 18, 2021
@rustbot rustbot added this to the 1.54.0 milestone May 18, 2021
Comment on lines +82 to +90
fn advance_by(&mut self, n: usize) -> Result<(), usize> {
// Advancing an infinite iterator of a single element is a no-op.
let _ = n;
Ok(())
}

#[inline]
fn nth(&mut self, n: usize) -> Option<A> {
let _ = n;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshtriplett @lopopolo

These two functions, as well as their counterparts in DoubleEndedIterator are incorrect. It changes behavior because there can be an arbitrary Drop impl for A, which means the side effects are removed by this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

What guarantees does core::iter::Repeat give around observing calls to clone?
10 participants