about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--RELEASES.txt14
-rw-r--r--src/libcore/iterator.rs69
-rw-r--r--src/libstd/treemap.rs6
3 files changed, 53 insertions, 36 deletions
diff --git a/RELEASES.txt b/RELEASES.txt
index 19ba7d3466d..13e4e0c2039 100644
--- a/RELEASES.txt
+++ b/RELEASES.txt
@@ -1,5 +1,17 @@
+Version 0.7 (July 2013)
+-----------------------
+
+   * ??? changes, numerous bugfixes
+
+   * Semantic changes
+      * The `self` parameter no longer implicitly means `&'self self`, and can be explicitly marked
+        with a lifetime.
+
+   * Libraries
+      * New `core::iterator` module for external iterator objects
+
 Version 0.6 (April 2013)
----------------------------
+------------------------
 
    * ~2100 changes, numerous bugfixes
 
diff --git a/src/libcore/iterator.rs b/src/libcore/iterator.rs
index e7a2f3a3928..fcb5102d4c0 100644
--- a/src/libcore/iterator.rs
+++ b/src/libcore/iterator.rs
@@ -12,20 +12,46 @@
 
 use prelude::*;
 
-pub trait Iterator<T> {
+pub trait Iterator<A> {
     /// Advance the iterator and return the next value. Return `None` when the end is reached.
-    fn next(&mut self) -> Option<T>;
+    fn next(&mut self) -> Option<A>;
 }
 
-/// A shim implementing the `for` loop iteration protocol for iterator objects
-#[inline]
-pub fn advance<T, U: Iterator<T>>(iter: &mut U, f: &fn(T) -> bool) {
-    loop {
-        match iter.next() {
-            Some(x) => {
-                if !f(x) { return }
+pub trait IteratorUtil<A> {
+    fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<Self, U>;
+    // FIXME: #5898: should be called map
+    fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, Self>;
+    fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> FilterIterator<'r, A, Self>;
+    fn advance(&mut self, f: &fn(A) -> bool);
+}
+
+impl<A, T: Iterator<A>> IteratorUtil<A> for T {
+    #[inline(always)]
+    fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<T, U> {
+        ZipIterator{a: self, b: other}
+    }
+
+    // FIXME: #5898: should be called map
+    #[inline(always)]
+    fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, T> {
+        MapIterator{iter: self, f: f}
+    }
+
+    #[inline(always)]
+    fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> FilterIterator<'r, A, T> {
+        FilterIterator{iter: self, predicate: predicate}
+    }
+
+    /// A shim implementing the `for` loop iteration protocol for iterator objects
+    #[inline]
+    fn advance(&mut self, f: &fn(A) -> bool) {
+        loop {
+            match self.next() {
+                Some(x) => {
+                    if !f(x) { return }
+                }
+                None => return
             }
-            None => return
         }
     }
 }
@@ -35,13 +61,6 @@ pub struct ZipIterator<T, U> {
     priv b: U
 }
 
-pub impl<A, B, T: Iterator<A>, U: Iterator<B>> ZipIterator<T, U> {
-    #[inline(always)]
-    fn new(a: T, b: U) -> ZipIterator<T, U> {
-        ZipIterator{a: a, b: b}
-    }
-}
-
 impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for ZipIterator<T, U> {
     #[inline]
     fn next(&mut self) -> Option<(A, B)> {
@@ -57,17 +76,10 @@ pub struct FilterIterator<'self, A, T> {
     priv predicate: &'self fn(&A) -> bool
 }
 
-pub impl<'self, A, T: Iterator<A>> FilterIterator<'self, A, T> {
-    #[inline(always)]
-    fn new(iter: T, predicate: &'self fn(&A) -> bool) -> FilterIterator<'self, A, T> {
-        FilterIterator{iter: iter, predicate: predicate}
-    }
-}
-
 impl<'self, A, T: Iterator<A>> Iterator<A> for FilterIterator<'self, A, T> {
     #[inline]
     fn next(&mut self) -> Option<A> {
-        for advance(self) |x| {
+        for self.iter.advance |x| {
             if (self.predicate)(&x) {
                 return Some(x);
             } else {
@@ -83,13 +95,6 @@ pub struct MapIterator<'self, A, B, T> {
     priv f: &'self fn(A) -> B
 }
 
-pub impl<'self, A, B, T: Iterator<A>> MapIterator<'self, A, B, T> {
-    #[inline(always)]
-    fn new(iter: T, f: &'self fn(A) -> B) -> MapIterator<'self, A, B, T> {
-        MapIterator{iter: iter, f: f}
-    }
-}
-
 impl<'self, A, B, T: Iterator<A>> Iterator<B> for MapIterator<'self, A, B, T> {
     #[inline]
     fn next(&mut self) -> Option<B> {
diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs
index bc8cbaa0825..ac887c7fdc4 100644
--- a/src/libstd/treemap.rs
+++ b/src/libstd/treemap.rs
@@ -996,7 +996,7 @@ mod test_treemap {
                         (&x5, &y5)];
         let mut i = 0;
 
-        for advance(&mut b) |x| {
+        for b.advance |x| {
             assert!(expected[i] == x);
             i += 1;
 
@@ -1005,7 +1005,7 @@ mod test_treemap {
             }
         }
 
-        for advance(&mut b) |x| {
+        for b.advance |x| {
             assert!(expected[i] == x);
             i += 1;
         }
@@ -1209,7 +1209,7 @@ mod test_set {
 
         let x = x;
         let y = y;
-        let mut z = ZipIterator::new(x.iter(), y.iter());
+        let mut z = x.iter().zip(y.iter());
 
         // FIXME: #5801: this needs a type hint to compile...
         let result: Option<(&uint, & &'static str)> = z.next();