Skip to content

Implement DoubleEnded and ExactSize for Take<Repeat> and Take<RepeatWith> #106943

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

Merged
merged 1 commit into from
Aug 17, 2024

Conversation

mina86
Copy link
Contributor

@mina86 mina86 commented Jan 16, 2023

Repeat iterator always returns the same element and behaves the same way
backwards and forwards. Take iterator can trivially implement backwards
iteration over Repeat inner iterator by simply doing forwards iteration.

DoubleEndedIterator is not currently implemented for Take<Repeat>
because Repeat doesn’t implement ExactSizeIterator which is a required
bound on DEI implementation for Take.

Similarly, since Repeat is an infinite iterator which never stops, Take
can trivially know how many elements it’s going to return. This allows
implementing ExactSizeIterator on Take<Repeat>.

While at it, observe that ExactSizeIterator can also be implemented for
Take<RepeatWhile> so add that implementation too. Since in contrast
to Repeat, RepeatWhile doesn’t guarante to always return the same value,
DoubleEndedIterator isn’t implemented.

Those changes render core::iter::repeat_n somewhat redundant.

Issue: #104434
Issue: #104729

…ith>

Repeat iterator always returns the same element and behaves the same way
backwards and forwards.  Take iterator can trivially implement backwards
iteration over Repeat inner iterator by simply doing forwards iteration.

DoubleEndedIterator is not currently implemented for Take<Repeat<T>>
because Repeat doesn’t implement ExactSizeIterator which is a required
bound on DEI implementation for Take.

Similarly, since Repeat is an infinite iterator which never stops, Take
can trivially know how many elements it’s going to return.  This allows
implementing ExactSizeIterator on Take<Repeat<T>>.

While at it, observe that ExactSizeIterator can also be implemented for
Take<RepeatWhile<F>> so add that implementation too.  Since in contrast
to Repeat, RepeatWhile doesn’t guarante to always return the same value,
DoubleEndedIterator isn’t implemented.

Those changes render core::iter::repeat_n somewhat redundant.

Issue: rust-lang#104434
Issue: rust-lang#104729
@rustbot
Copy link
Collaborator

rustbot commented Jan 16, 2023

r? @cuviper

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 16, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jan 16, 2023

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@mina86
Copy link
Contributor Author

mina86 commented Jan 16, 2023

@rustbot label +T-libs-api -T-libs

@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 16, 2023
@mina86 mina86 changed the title Implement DoubleEndedIterator and ExactSizeIterator for Take<Repaet<T>> Implement DoubleEndedIterator and ExactSizeIterator for Take<Repeat<T>> Jan 16, 2023
@mina86 mina86 force-pushed the exact_size_take_repeat branch from 45de24d to 5d1590d Compare January 16, 2023 17:42
@cuviper
Copy link
Member

cuviper commented Jan 16, 2023

r? libs-api

@rustbot rustbot assigned BurntSushi and unassigned cuviper Jan 16, 2023
@dtolnay
Copy link
Member

dtolnay commented Jan 22, 2023

Reassigning BurntSushi's reviews because based on git log -i --grep=r=burntsushi the last time they did rust-lang/rust reviews was over 3 years ago.

r? rust-lang/libs-api

@rustbot rustbot assigned joshtriplett and unassigned BurntSushi Jan 22, 2023
@pitaj
Copy link
Contributor

pitaj commented Jan 28, 2023

I believe this requires an ACP, since it introduces an insta-stable change by adding a new implementation of an existing stable trait on an existing stable type.

Please open one (if you haven't already). Then link it here and mark the PR as S-waiting-on-ACP.

@rustbot label -S-waiting-on-review +S-waiting-on-author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 28, 2023
@mina86
Copy link
Contributor Author

mina86 commented Jan 28, 2023

Sorry, I missed the requirement for ACP. So actually, rather than creating a new one, I’ve just pinged ACP for repeat_n. This approach was actually suggested there already. The ACP is: rust-lang/libs-team#120

@mina86
Copy link
Contributor Author

mina86 commented Jan 28, 2023

@rustbot label -S-waiting-on-author +S-waiting-on-ACP

@rustbot rustbot added S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 28, 2023
@workingjubilee workingjubilee added the needs-fcp This change is insta-stable, so needs a completed FCP to proceed. label Jul 31, 2023
@workingjubilee
Copy link
Member

... is it really an ExactSizeIterator if it's infinite?

@pitaj
Copy link
Contributor

pitaj commented Jul 31, 2023

... is it really an ExactSizeIterator if it's infinite?

This only adds that impl for Take<Repeat>, which has a known finite size.

@workingjubilee
Copy link
Member

...
......
........
right.

I'll uh...
r? libs-api Needs FCP for this set of trait impls that offer effectively an alternative to the repeat_n function.

@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Aug 16, 2024
@dtolnay dtolnay assigned dtolnay and unassigned m-ou-se Aug 16, 2024
@bors
Copy link
Collaborator

bors commented Aug 16, 2024

⌛ Testing commit ebf79b5 with merge 9dda6ac...

bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 16, 2024
…lnay

Implement DoubleEnded and ExactSize for Take<Repeat> and Take<RepeatWith>

Repeat iterator always returns the same element and behaves the same way
backwards and forwards.  Take iterator can trivially implement backwards
iteration over Repeat inner iterator by simply doing forwards iteration.

DoubleEndedIterator is not currently implemented for Take<Repeat<T>>
because Repeat doesn’t implement ExactSizeIterator which is a required
bound on DEI implementation for Take.

Similarly, since Repeat is an infinite iterator which never stops, Take
can trivially know how many elements it’s going to return.  This allows
implementing ExactSizeIterator on Take<Repeat<T>>.

While at it, observe that ExactSizeIterator can also be implemented for
Take<RepeatWhile<F>> so add that implementation too.  Since in contrast
to Repeat, RepeatWhile doesn’t guarante to always return the same value,
DoubleEndedIterator isn’t implemented.

Those changes render core::iter::repeat_n somewhat redundant.

Issue: rust-lang#104434
Issue: rust-lang#104729

- [ ] ACP: rust-lang/libs-team#120 (this is actually ACP for repeat_n but this is nearly the same functionality so hijacking it so both approaches can be discussed in one place)
@bors
Copy link
Collaborator

bors commented Aug 16, 2024

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 16, 2024
@rust-log-analyzer

This comment has been minimized.

@dtolnay
Copy link
Member

dtolnay commented Aug 16, 2024

Needs a rebase to fix the conflict with #120486.

@dtolnay dtolnay force-pushed the exact_size_take_repeat branch from ebf79b5 to 994e712 Compare August 16, 2024 23:58
@dtolnay
Copy link
Member

dtolnay commented Aug 16, 2024

- fn advance_back_by(&mut self, n: usize) -> Result<(), NonZeroUsize> {
+ fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 16, 2024

📌 Commit 994e712 has been approved by dtolnay

It is now in the queue for this repository.

@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 Aug 16, 2024
@bors
Copy link
Collaborator

bors commented Aug 17, 2024

⌛ Testing commit 994e712 with merge f24a6ba...

@bors
Copy link
Collaborator

bors commented Aug 17, 2024

☀️ Test successful - checks-actions
Approved by: dtolnay
Pushing f24a6ba to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 17, 2024
@bors bors merged commit f24a6ba into rust-lang:master Aug 17, 2024
7 checks passed
@rustbot rustbot added this to the 1.82.0 milestone Aug 17, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f24a6ba): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (primary -3.1%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.1% [-3.1%, -3.1%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -3.1% [-3.1%, -3.1%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 750.979s -> 749.822s (-0.15%)
Artifact size: 339.14 MiB -> 339.18 MiB (0.01%)

@mina86 mina86 deleted the exact_size_take_repeat branch August 19, 2024 07:33
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Aug 22, 2024
AnthonyMikh added a commit to AnthonyMikh/rust that referenced this pull request Oct 17, 2024
After rust/rust-lang#106943 the part about `ExactSizeIterator` is no longer valid
jhpratt added a commit to jhpratt/rust that referenced this pull request Oct 18, 2024
…s-not-that-special-anymore, r=jhpratt

Remove outdated documentation for `repeat_n`

After rust-lang#106943, which made `Take<Repeat<I>>` implement `ExactSizeIterator`, part of documentation about difference from `repeat(x).take(n)` is no longer valid.

`@rustbot` labels: +A-docs, +A-iterators
jhpratt added a commit to jhpratt/rust that referenced this pull request Oct 18, 2024
…s-not-that-special-anymore, r=jhpratt

Remove outdated documentation for `repeat_n`

After rust-lang#106943, which made `Take<Repeat<I>>` implement `ExactSizeIterator`, part of documentation about difference from `repeat(x).take(n)` is no longer valid.

``@rustbot`` labels: +A-docs, +A-iterators
jhpratt added a commit to jhpratt/rust that referenced this pull request Oct 18, 2024
…s-not-that-special-anymore, r=jhpratt

Remove outdated documentation for `repeat_n`

After rust-lang#106943, which made `Take<Repeat<I>>` implement `ExactSizeIterator`, part of documentation about difference from `repeat(x).take(n)` is no longer valid.

```@rustbot``` labels: +A-docs, +A-iterators
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Oct 18, 2024
…s-not-that-special-anymore, r=jhpratt

Remove outdated documentation for `repeat_n`

After rust-lang#106943, which made `Take<Repeat<I>>` implement `ExactSizeIterator`, part of documentation about difference from `repeat(x).take(n)` is no longer valid.

````@rustbot```` labels: +A-docs, +A-iterators
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 18, 2024
Rollup merge of rust-lang#131858 - AnthonyMikh:AnthonyMikh/repeat_n-is-not-that-special-anymore, r=jhpratt

Remove outdated documentation for `repeat_n`

After rust-lang#106943, which made `Take<Repeat<I>>` implement `ExactSizeIterator`, part of documentation about difference from `repeat(x).take(n)` is no longer valid.

````@rustbot```` labels: +A-docs, +A-iterators
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. merged-by-bors This PR was explicitly merged by bors. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.