Skip to content

Commit 6710669

Browse files
committed
Add finite iterator specialisations for Repeat
1 parent 73ac5d6 commit 6710669

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/libcore/iter/sources.rs

+34
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use fmt;
1212
use marker;
1313
use usize;
14+
use cmp::Ordering;
1415

1516
use super::{FusedIterator, TrustedLen};
1617

@@ -31,8 +32,41 @@ impl<A: Clone> Iterator for Repeat<A> {
3132

3233
#[inline]
3334
fn next(&mut self) -> Option<A> { Some(self.element.clone()) }
35+
3436
#[inline]
3537
fn size_hint(&self) -> (usize, Option<usize>) { (usize::MAX, None) }
38+
39+
#[inline]
40+
fn nth(&mut self, _: usize) -> Option<A> { self.next() }
41+
42+
#[inline]
43+
fn all<F>(&mut self, f: F) -> bool where F: FnMut(A) -> bool { self.any(f) }
44+
45+
#[inline]
46+
fn max(mut self) -> Option<A> { self.next() }
47+
48+
#[inline]
49+
fn min(mut self) -> Option<A> { self.next() }
50+
51+
#[inline]
52+
fn max_by_key<B: Ord, F>(mut self, _: F) -> Option<A> where F: FnMut(&A) -> B {
53+
self.next()
54+
}
55+
56+
#[inline]
57+
fn max_by<F>(mut self, _: F) -> Option<A> where F: FnMut(&A, &A) -> Ordering {
58+
self.next()
59+
}
60+
61+
#[inline]
62+
fn min_by_key<B: Ord, F>(mut self, _: F) -> Option<A> where F: FnMut(&A) -> B {
63+
self.next()
64+
}
65+
66+
#[inline]
67+
fn min_by<F>(mut self, _: F) -> Option<A> where F: FnMut(&A, &A) -> Ordering {
68+
self.next()
69+
}
3670
}
3771

3872
#[stable(feature = "rust1", since = "1.0.0")]

src/libcore/tests/iter.rs

+14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use core::iter::*;
1212
use core::{i8, i16, isize};
1313
use core::usize;
14+
use std::cmp::Ordering;
1415

1516
#[test]
1617
fn test_lt() {
@@ -1405,6 +1406,19 @@ fn test_repeat() {
14051406
assert_eq!(it.next(), Some(42));
14061407
}
14071408

1409+
#[test]
1410+
fn test_repeat_iterator() {
1411+
let mut it = repeat(42);
1412+
assert_eq!(it.nth(usize::MAX), Some(42));
1413+
assert_eq!(it.all(|x| x == 42), true);
1414+
assert_eq!(repeat(42).max(), Some(42));
1415+
assert_eq!(repeat(42).min(), Some(42));
1416+
assert_eq!(repeat(42).max_by_key(|_| 0), Some(42));
1417+
assert_eq!(repeat(42).max_by_key(|_| Ordering::Greater), Some(42));
1418+
assert_eq!(repeat(42).min_by_key(|_| 0), Some(42));
1419+
assert_eq!(repeat(42).min_by_key(|_| Ordering::Greater), Some(42));
1420+
}
1421+
14081422
#[test]
14091423
fn test_fuse() {
14101424
let mut it = 0..3;

0 commit comments

Comments
 (0)