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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions library/core/src/iter/sources/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,32 @@ impl<A: Clone> Iterator for Repeat<A> {
fn next(&mut self) -> Option<A> {
Some(self.element.clone())
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(usize::MAX, None)
}

#[inline]
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;
Comment on lines +82 to +90
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.

Some(self.element.clone())
}

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.

}

fn count(self) -> usize {
loop {}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -84,6 +106,19 @@ impl<A: Clone> DoubleEndedIterator for Repeat<A> {
fn next_back(&mut self) -> Option<A> {
Some(self.element.clone())
}

#[inline]
fn advance_back_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_back(&mut self, n: usize) -> Option<A> {
let _ = n;
Some(self.element.clone())
}
}

#[stable(feature = "fused", since = "1.26.0")]
Expand Down