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

Tracking Issue for iter::repeat_n() (feature(iter_repeat_n)) #104434

Open
3 of 5 tasks
scottmcm opened this issue Nov 15, 2022 · 9 comments
Open
3 of 5 tasks

Tracking Issue for iter::repeat_n() (feature(iter_repeat_n)) #104434

scottmcm opened this issue Nov 15, 2022 · 9 comments
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. 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. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
Member

scottmcm commented Nov 15, 2022

Feature gate: #![feature(iter_repeat_n)]

This is a tracking issue for the iter::repeat_n function and its associated iter::RepeatN type.

This is like repeat, but count-limited so it can re-use the buffer when generating the last item.

ACP rust-lang/libs-team#120 is still open, but I'm sending a PR for it anyway as part of fixing a bug in VecDeque.

Public API

// core::iter

fn repeat_n<T>(element: T, count: usize) -> RepeatN<T>;

pub struct RepeatN<T>();

impl<T: Clone> Iterator for RepeatN<T> {}
impl<T: Clone> DoubleEndedIterator for RepeatN<T> {}
impl<T: Clone> ExactSizeIterator for RepeatN<T> {}
impl<T: Clone> FusedIterator for RepeatN<T> {}
impl<T: Clone> TrustedLen for RepeatN<T> {}

Steps / History

Unresolved Questions

  • None yet.

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

@scottmcm scottmcm added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. labels Nov 15, 2022
JohnTitor pushed a commit to JohnTitor/rust that referenced this issue Nov 20, 2022
`VecDeque::resize` should re-use the buffer in the passed-in element

Today it always copies it for *every* appended element, but one of those clones is avoidable.

This adds `iter::repeat_n` (rust-lang#104434) as the primitive needed to do this.  If this PR is acceptable, I'll also use this in `Vec` rather than its custom `ExtendElement` type & infrastructure that is harder to share between multiple different containers:

https://github.com/rust-lang/rust/blob/101e1822c3e54e63996c8aaa014d55716f3937eb/library/alloc/src/vec/mod.rs#L2479-L2492
thomcc pushed a commit to tcdi/postgrestd that referenced this issue Feb 10, 2023
`VecDeque::resize` should re-use the buffer in the passed-in element

Today it always copies it for *every* appended element, but one of those clones is avoidable.

This adds `iter::repeat_n` (rust-lang/rust#104434) as the primitive needed to do this.  If this PR is acceptable, I'll also use this in `Vec` rather than its custom `ExtendElement` type & infrastructure that is harder to share between multiple different containers:

https://github.com/rust-lang/rust/blob/101e1822c3e54e63996c8aaa014d55716f3937eb/library/alloc/src/vec/mod.rs#L2479-L2492
mina86 added a commit to mina86/rust that referenced this issue Sep 2, 2023
…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
@Amanieu
Copy link
Member

Amanieu commented Aug 6, 2024

Even if we decide to accept #106943, having a simple and ergonomic way to repeat a value N times is still useful.

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Aug 6, 2024

Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Aug 6, 2024
@joshtriplett
Copy link
Member

Is this still going to be blocked on item shadowing, to avoid breaking itertools?

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Aug 9, 2024
@rfcbot
Copy link

rfcbot commented Aug 9, 2024

🔔 This is now entering its final comment period, as per the review above. 🔔

@scottmcm
Copy link
Member Author

scottmcm commented Aug 9, 2024

@joshtriplett No shadowing concerns for this because it's a function, not a trait method.

It'll only be an issue if you use itertools::*; use std::iter::*;, but that's self-inflicted.

@joshtriplett
Copy link
Member

@scottmcm 🤦 Right, of course. Thank you.

bors added a commit to rust-lang-ci/rust that referenced this issue 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 pushed a commit to rust-lang-ci/rust that referenced this issue Aug 17, 2024
…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
bors added a commit to rust-lang-ci/rust that referenced this issue Aug 17, 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)
rezwanahmedsami pushed a commit to rezwanahmedsami/rust that referenced this issue Aug 17, 2024
…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
@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Aug 19, 2024
@rfcbot
Copy link

rfcbot commented Aug 19, 2024

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label Aug 19, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 20, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Aug 21, 2024
Rollup merge of rust-lang#129294 - scottmcm:stabilize-repeat-n, r=Noratrieb

Stabilize `iter::repeat_n`

ACP completed in rust-lang#104434 (comment)
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Aug 22, 2024
@GnomedDev
Copy link
Contributor

GnomedDev commented Sep 10, 2024

Did any discussion about using NonZero for repeat_n's argument go on? Would it be favorable to block stabilization until this mess is sorted out as from my look on the docs it hasn't reached stable yet?

@scottmcm
Copy link
Member Author

Given that repeating zero times is a completely acceptable thing to do -- just like [x; 0] works -- I don't think there's any need to use NonZero<usize> for it. That's worth considering for things like .step_by(0) where it panics for zero, but when it works just fine with zero like here, I don't see any value in blocking zero. (After all, after let mut x = repeat_n(x, 1); x.next(); you have a zero-length repeat anyway.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. 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. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants