-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
Some(self.element.clone()) | ||
} | ||
|
||
fn last(self) -> Option<A> { | ||
loop {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Repeat is an infinite iterator so it has no last element. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But it has last element according to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Is there something to be gain from this being an infinite loop? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code does not change the behavior of repeat when calling |
||
} | ||
|
||
fn count(self) -> usize { | ||
loop {} | ||
} | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
|
@@ -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")] | ||
|
There was a problem hiding this comment.
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 forA
, which means the side effects are removed by this PR.