Skip to content

Commit f82c964

Browse files
committed
iterator: use an IteratorUtil trait
1 parent a581926 commit f82c964

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

src/libcore/iterator.rs

+37-32
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,46 @@
1212
1313
use prelude::*;
1414

15-
pub trait Iterator<T> {
15+
pub trait Iterator<A> {
1616
/// Advance the iterator and return the next value. Return `None` when the end is reached.
17-
fn next(&mut self) -> Option<T>;
17+
fn next(&mut self) -> Option<A>;
1818
}
1919

20-
/// A shim implementing the `for` loop iteration protocol for iterator objects
21-
#[inline]
22-
pub fn advance<T, U: Iterator<T>>(iter: &mut U, f: &fn(T) -> bool) {
23-
loop {
24-
match iter.next() {
25-
Some(x) => {
26-
if !f(x) { return }
20+
pub trait IteratorUtil<A> {
21+
fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<Self, U>;
22+
// FIXME: #5898: should be called map
23+
fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, Self>;
24+
fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> FilterIterator<'r, A, Self>;
25+
fn advance(&mut self, f: &fn(A) -> bool);
26+
}
27+
28+
impl<A, T: Iterator<A>> IteratorUtil<A> for T {
29+
#[inline(always)]
30+
fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<T, U> {
31+
ZipIterator{a: self, b: other}
32+
}
33+
34+
// FIXME: #5898: should be called map
35+
#[inline(always)]
36+
fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, T> {
37+
MapIterator{iter: self, f: f}
38+
}
39+
40+
#[inline(always)]
41+
fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> FilterIterator<'r, A, T> {
42+
FilterIterator{iter: self, predicate: predicate}
43+
}
44+
45+
/// A shim implementing the `for` loop iteration protocol for iterator objects
46+
#[inline]
47+
fn advance(&mut self, f: &fn(A) -> bool) {
48+
loop {
49+
match self.next() {
50+
Some(x) => {
51+
if !f(x) { return }
52+
}
53+
None => return
2754
}
28-
None => return
2955
}
3056
}
3157
}
@@ -35,13 +61,6 @@ pub struct ZipIterator<T, U> {
3561
priv b: U
3662
}
3763

38-
pub impl<A, B, T: Iterator<A>, U: Iterator<B>> ZipIterator<T, U> {
39-
#[inline(always)]
40-
fn new(a: T, b: U) -> ZipIterator<T, U> {
41-
ZipIterator{a: a, b: b}
42-
}
43-
}
44-
4564
impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for ZipIterator<T, U> {
4665
#[inline]
4766
fn next(&mut self) -> Option<(A, B)> {
@@ -57,17 +76,10 @@ pub struct FilterIterator<'self, A, T> {
5776
priv predicate: &'self fn(&A) -> bool
5877
}
5978

60-
pub impl<'self, A, T: Iterator<A>> FilterIterator<'self, A, T> {
61-
#[inline(always)]
62-
fn new(iter: T, predicate: &'self fn(&A) -> bool) -> FilterIterator<'self, A, T> {
63-
FilterIterator{iter: iter, predicate: predicate}
64-
}
65-
}
66-
6779
impl<'self, A, T: Iterator<A>> Iterator<A> for FilterIterator<'self, A, T> {
6880
#[inline]
6981
fn next(&mut self) -> Option<A> {
70-
for advance(self) |x| {
82+
for self.iter.advance |x| {
7183
if (self.predicate)(&x) {
7284
return Some(x);
7385
} else {
@@ -83,13 +95,6 @@ pub struct MapIterator<'self, A, B, T> {
8395
priv f: &'self fn(A) -> B
8496
}
8597

86-
pub impl<'self, A, B, T: Iterator<A>> MapIterator<'self, A, B, T> {
87-
#[inline(always)]
88-
fn new(iter: T, f: &'self fn(A) -> B) -> MapIterator<'self, A, B, T> {
89-
MapIterator{iter: iter, f: f}
90-
}
91-
}
92-
9398
impl<'self, A, B, T: Iterator<A>> Iterator<B> for MapIterator<'self, A, B, T> {
9499
#[inline]
95100
fn next(&mut self) -> Option<B> {

src/libstd/treemap.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ mod test_treemap {
996996
(&x5, &y5)];
997997
let mut i = 0;
998998

999-
for advance(&mut b) |x| {
999+
for b.advance |x| {
10001000
assert!(expected[i] == x);
10011001
i += 1;
10021002

@@ -1005,7 +1005,7 @@ mod test_treemap {
10051005
}
10061006
}
10071007

1008-
for advance(&mut b) |x| {
1008+
for b.advance |x| {
10091009
assert!(expected[i] == x);
10101010
i += 1;
10111011
}
@@ -1209,7 +1209,7 @@ mod test_set {
12091209

12101210
let x = x;
12111211
let y = y;
1212-
let mut z = ZipIterator::new(x.iter(), y.iter());
1212+
let mut z = x.iter().zip(y.iter());
12131213

12141214
// FIXME: #5801: this needs a type hint to compile...
12151215
let result: Option<(&uint, & &'static str)> = z.next();

0 commit comments

Comments
 (0)