-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Add iterator specialisations for Repeat and Cycle #47370
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 |
---|---|---|
|
@@ -302,7 +302,7 @@ | |
|
||
#![stable(feature = "rust1", since = "1.0.0")] | ||
|
||
use cmp; | ||
use cmp::{self, Ordering}; | ||
use fmt; | ||
use iter_private::TrustedRandomAccess; | ||
use ops::Try; | ||
|
@@ -643,6 +643,43 @@ impl<I> Iterator for Cycle<I> where I: Clone + Iterator { | |
_ => (usize::MAX, None) | ||
} | ||
} | ||
|
||
#[inline] | ||
fn all<F>(&mut self, f: F) -> bool where F: FnMut(Self::Item) -> bool { | ||
self.iter.clone().chain(self.orig.clone()).all(f) | ||
} | ||
|
||
#[inline] | ||
fn max(self) -> Option<Self::Item> where Self::Item: cmp::Ord { self.orig.clone().max() } | ||
|
||
#[inline] | ||
fn min(self) -> Option<Self::Item> where Self::Item: cmp::Ord { | ||
cmp::min(self.iter.min(), self.orig.clone().min()) | ||
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.
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. Yeah — this is necessary to preserve the consistency of the behaviour of |
||
} | ||
|
||
#[inline] | ||
fn max_by_key<B: Ord, F>(self, f: F) -> Option<Self::Item> | ||
where F: FnMut(&Self::Item) -> B { | ||
self.orig.clone().max_by_key(f) | ||
} | ||
|
||
#[inline] | ||
fn max_by<F>(self, f: F) -> Option<Self::Item> | ||
where F: FnMut(&Self::Item, &Self::Item) -> Ordering { | ||
self.orig.clone().max_by(f) | ||
} | ||
|
||
#[inline] | ||
fn min_by_key<B: Ord, F>(self, f: F) -> Option<Self::Item> | ||
where F: FnMut(&Self::Item) -> B { | ||
self.iter.chain(self.orig.clone()).min_by_key(f) | ||
} | ||
|
||
#[inline] | ||
fn min_by<F>(self, f: F) -> Option<Self::Item> | ||
where F: FnMut(&Self::Item, &Self::Item) -> Ordering { | ||
self.iter.chain(self.orig.clone()).min_by(f) | ||
} | ||
} | ||
|
||
#[unstable(feature = "fused", issue = "35602")] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
use fmt; | ||
use marker; | ||
use usize; | ||
use cmp::Ordering; | ||
|
||
use super::{FusedIterator, TrustedLen}; | ||
|
||
|
@@ -31,8 +32,41 @@ impl<A: Clone> Iterator for Repeat<A> { | |
|
||
#[inline] | ||
fn next(&mut self) -> Option<A> { Some(self.element.clone()) } | ||
|
||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { (usize::MAX, None) } | ||
|
||
#[inline] | ||
fn nth(&mut self, _: usize) -> Option<A> { self.next() } | ||
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. I like this one more than the others here, but it's an observable change if 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. Oh, I didn't consider that. It seems to me that |
||
|
||
#[inline] | ||
fn all<F>(&mut self, mut f: F) -> bool where F: FnMut(A) -> bool { f(self.element.clone()) } | ||
|
||
#[inline] | ||
fn max(mut self) -> Option<A> { self.next() } | ||
|
||
#[inline] | ||
fn min(mut self) -> Option<A> { self.next() } | ||
|
||
#[inline] | ||
fn max_by_key<B: Ord, F>(mut self, _: F) -> Option<A> where F: FnMut(&A) -> B { | ||
self.next() | ||
} | ||
|
||
#[inline] | ||
fn max_by<F>(mut self, _: F) -> Option<A> where F: FnMut(&A, &A) -> Ordering { | ||
self.next() | ||
} | ||
|
||
#[inline] | ||
fn min_by_key<B: Ord, F>(mut self, _: F) -> Option<A> where F: FnMut(&A) -> B { | ||
self.next() | ||
} | ||
|
||
#[inline] | ||
fn min_by<F>(mut self, _: F) -> Option<A> where F: FnMut(&A, &A) -> Ordering { | ||
self.next() | ||
} | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.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.
If this short-circuits with an element inside
self.orig
, this iterator should continue with the next element after the matching one inself.orig
. Currently it'll instead restart with the first element inself.orig
.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.
Ah, that's a good point. I'll fix that if the decision is to merge.