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
2 of 5 tasks
scottmcm opened this issue Nov 15, 2022 · 0 comments
Open
2 of 5 tasks

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

scottmcm opened this issue Nov 15, 2022 · 0 comments
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. 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
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. 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

1 participant