Skip to content

Commit d6e2867

Browse files
committed
Add finite iterator specialisations for Cycle
1 parent 6710669 commit d6e2867

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/libcore/iter/mod.rs

+36-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@
302302
303303
#![stable(feature = "rust1", since = "1.0.0")]
304304

305-
use cmp;
305+
use cmp::{self, Ordering};
306306
use fmt;
307307
use iter_private::TrustedRandomAccess;
308308
use ops::Try;
@@ -643,6 +643,41 @@ impl<I> Iterator for Cycle<I> where I: Clone + Iterator {
643643
_ => (usize::MAX, None)
644644
}
645645
}
646+
647+
#[inline]
648+
fn all<F>(&mut self, f: F) -> bool where F: FnMut(Self::Item) -> bool { self.orig.clone().all(f) }
649+
650+
#[inline]
651+
fn max(self) -> Option<Self::Item> where Self::Item: cmp::Ord { self.orig.clone().max() }
652+
653+
#[inline]
654+
fn min(self) -> Option<Self::Item> where Self::Item: cmp::Ord {
655+
cmp::min(self.iter.min(), self.orig.clone().min())
656+
}
657+
658+
#[inline]
659+
fn max_by_key<B: Ord, F>(self, f: F) -> Option<Self::Item>
660+
where F: FnMut(&Self::Item) -> B {
661+
self.orig.clone().max_by_key(f)
662+
}
663+
664+
#[inline]
665+
fn max_by<F>(self, f: F) -> Option<Self::Item>
666+
where F: FnMut(&Self::Item, &Self::Item) -> Ordering {
667+
self.orig.clone().max_by(f)
668+
}
669+
670+
#[inline]
671+
fn min_by_key<B: Ord, F>(self, f: F) -> Option<Self::Item>
672+
where F: FnMut(&Self::Item) -> B {
673+
self.iter.chain(self.orig.clone()).min_by_key(f)
674+
}
675+
676+
#[inline]
677+
fn min_by<F>(self, f: F) -> Option<Self::Item>
678+
where F: FnMut(&Self::Item, &Self::Item) -> Ordering {
679+
self.iter.chain(self.orig.clone()).min_by(f)
680+
}
646681
}
647682

648683
#[unstable(feature = "fused", issue = "35602")]

src/libcore/tests/iter.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,20 @@ fn test_cycle() {
867867
assert_eq!(it.next(), None);
868868
}
869869

870+
#[test]
871+
fn test_cycle_iterator() {
872+
let a = 1;
873+
let b = 5;
874+
let mut it = (a..b).cycle();
875+
assert_eq!(it.all(|x| x <= b), true);
876+
assert_eq!((a..b).cycle().max(), Some(b - 1));
877+
assert_eq!((a..b).cycle().min(), Some(a));
878+
assert_eq!((a..b).cycle().max_by_key(|x| -x), Some(a));
879+
assert_eq!((a..b).cycle().max_by(|x, y| y.cmp(x)), Some(a));
880+
assert_eq!((a..b).cycle().min_by_key(|x| -x), Some(b - 1));
881+
assert_eq!((a..b).cycle().min_by(|x, y| y.cmp(x)), Some(b - 1));
882+
}
883+
870884
#[test]
871885
fn test_iterator_nth() {
872886
let v: &[_] = &[0, 1, 2, 3, 4];
@@ -1414,9 +1428,9 @@ fn test_repeat_iterator() {
14141428
assert_eq!(repeat(42).max(), Some(42));
14151429
assert_eq!(repeat(42).min(), Some(42));
14161430
assert_eq!(repeat(42).max_by_key(|_| 0), Some(42));
1417-
assert_eq!(repeat(42).max_by_key(|_| Ordering::Greater), Some(42));
1431+
assert_eq!(repeat(42).max_by(|_, _| Ordering::Equal), Some(42));
14181432
assert_eq!(repeat(42).min_by_key(|_| 0), Some(42));
1419-
assert_eq!(repeat(42).min_by_key(|_| Ordering::Greater), Some(42));
1433+
assert_eq!(repeat(42).min_by(|_, _| Ordering::Equal), Some(42));
14201434
}
14211435

14221436
#[test]

0 commit comments

Comments
 (0)