about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-06-11 16:19:42 -0700
committerbors <bors@rust-lang.org>2013-06-11 16:19:42 -0700
commit3f900dc7d1517bbc821989d68f3392d4aae96f93 (patch)
tree1f8c006deabe41787f5111c47c823c0d2fb12301 /src/libstd
parent1175e94de3b6d0f6b35fd8de3599b29267f1adab (diff)
parentbbe3d4a9dc7eae6b53871d0a59d56372c2c03479 (diff)
downloadrust-3f900dc7d1517bbc821989d68f3392d4aae96f93.tar.gz
rust-3f900dc7d1517bbc821989d68f3392d4aae96f93.zip
auto merge of #7055 : thestinger/rust/iterator, r=catamorphism
This was a lot more painful than just changing `x.each` to `x.iter().advance` . I ran into my old friend #5898 and had to add underscores to some method names as a temporary workaround.

The borrow checker also had other ideas because rvalues aren't handled very well yet so temporary variables had to be added. However, storing the temporary in a variable led to dynamic `@mut` failures, so those had to be wrapped in blocks except where the scope ends immediately.

Anyway, the ugliness will be fixed as the compiler issues are fixed and this change will amount to `for x.each |x|` becoming `for x.iter |x|` and making all the iterator adaptors available.

I dropped the run-pass tests for `old_iter` because there's not much point in fixing a module that's on the way out in the next week or so.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/hashmap.rs3
-rw-r--r--src/libstd/iterator.rs42
-rw-r--r--src/libstd/option.rs86
-rw-r--r--src/libstd/os.rs2
-rw-r--r--src/libstd/run.rs12
-rw-r--r--src/libstd/task/spawn.rs7
-rw-r--r--src/libstd/unstable/extfmt.rs12
7 files changed, 82 insertions, 82 deletions
diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs
index b1400d1bc76..7a9435d2ecd 100644
--- a/src/libstd/hashmap.rs
+++ b/src/libstd/hashmap.rs
@@ -20,6 +20,7 @@ use cmp::{Eq, Equiv};
 use hash::Hash;
 use old_iter::BaseIter;
 use old_iter;
+use iterator::{Iterator, IteratorUtil};
 use option::{None, Option, Some};
 use rand::RngUtil;
 use rand;
@@ -316,7 +317,7 @@ impl<K:Hash + Eq,V> Map<K, V> for HashMap<K, V> {
     /// Visit all key-value pairs
     fn each<'a>(&'a self, blk: &fn(&K, &'a V) -> bool) -> bool {
         for self.buckets.each |bucket| {
-            for bucket.each |pair| {
+            for bucket.iter().advance |pair| {
                 if !blk(&pair.key, &pair.value) {
                     return false;
                 }
diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs
index 8803844fdd0..bed74eba604 100644
--- a/src/libstd/iterator.rs
+++ b/src/libstd/iterator.rs
@@ -49,12 +49,12 @@ pub trait IteratorUtil<A> {
     ///
     /// let a = [0];
     /// let b = [1];
-    /// let mut it = a.iter().chain(b.iter());
+    /// let mut it = a.iter().chain_(b.iter());
     /// assert_eq!(it.next().get(), &0);
     /// assert_eq!(it.next().get(), &1);
     /// assert!(it.next().is_none());
     /// ~~~
-    fn chain<U: Iterator<A>>(self, other: U) -> ChainIterator<A, Self, U>;
+    fn chain_<U: Iterator<A>>(self, other: U) -> ChainIterator<A, Self, U>;
 
     /// Creates an iterator which iterates over both this and the specified
     /// iterators simultaneously, yielding the two elements as pairs. When
@@ -191,6 +191,7 @@ pub trait IteratorUtil<A> {
     /// ~~~
     fn skip(self, n: uint) -> SkipIterator<A, Self>;
 
+    // FIXME: #5898: should be called take
     /// Creates an iterator which yields the first `n` elements of this
     /// iterator, and then it will always return None.
     ///
@@ -200,13 +201,13 @@ pub trait IteratorUtil<A> {
     /// use std::iterator::*;
     ///
     /// let a = [1, 2, 3, 4, 5];
-    /// let mut it = a.iter().take(3);
+    /// let mut it = a.iter().take_(3);
     /// assert_eq!(it.next().get(), &1);
     /// assert_eq!(it.next().get(), &2);
     /// assert_eq!(it.next().get(), &3);
     /// assert!(it.next().is_none());
     /// ~~~
-    fn take(self, n: uint) -> TakeIterator<A, Self>;
+    fn take_(self, n: uint) -> TakeIterator<A, Self>;
 
     /// Creates a new iterator which behaves in a similar fashion to foldl.
     /// There is a state which is passed between each iteration and can be
@@ -337,10 +338,10 @@ pub trait IteratorUtil<A> {
     ///
     /// let a = [1, 2, 3, 4, 5];
     /// let mut it = a.iter();
-    /// assert!(it.any(|&x| *x == 3));
-    /// assert!(!it.any(|&x| *x == 3));
+    /// assert!(it.any_(|&x| *x == 3));
+    /// assert!(!it.any_(|&x| *x == 3));
     /// ~~~
-    fn any(&mut self, f: &fn(A) -> bool) -> bool;
+    fn any_(&mut self, f: &fn(A) -> bool) -> bool;
 }
 
 /// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also
@@ -349,7 +350,7 @@ pub trait IteratorUtil<A> {
 /// In the future these will be default methods instead of a utility trait.
 impl<A, T: Iterator<A>> IteratorUtil<A> for T {
     #[inline(always)]
-    fn chain<U: Iterator<A>>(self, other: U) -> ChainIterator<A, T, U> {
+    fn chain_<U: Iterator<A>>(self, other: U) -> ChainIterator<A, T, U> {
         ChainIterator{a: self, b: other, flag: false}
     }
 
@@ -394,8 +395,9 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
         SkipIterator{iter: self, n: n}
     }
 
+    // FIXME: #5898: should be called take
     #[inline(always)]
-    fn take(self, n: uint) -> TakeIterator<A, T> {
+    fn take_(self, n: uint) -> TakeIterator<A, T> {
         TakeIterator{iter: self, n: n}
     }
 
@@ -467,7 +469,7 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
     }
 
     #[inline(always)]
-    fn any(&mut self, f: &fn(A) -> bool) -> bool {
+    fn any_(&mut self, f: &fn(A) -> bool) -> bool {
         for self.advance |x| { if f(x) { return true; } }
         return false;
     }
@@ -878,7 +880,7 @@ mod tests {
 
     #[test]
     fn test_counter_from_iter() {
-        let mut it = Counter::new(0, 5).take(10);
+        let mut it = Counter::new(0, 5).take_(10);
         let xs: ~[int] = iter::FromIter::from_iter::<int, ~[int]>(|f| it.advance(f));
         assert_eq!(xs, ~[0, 5, 10, 15, 20, 25, 30, 35, 40, 45]);
     }
@@ -888,7 +890,7 @@ mod tests {
         let xs = [0u, 1, 2, 3, 4, 5];
         let ys = [30u, 40, 50, 60];
         let expected = [0, 1, 2, 3, 4, 5, 30, 40, 50, 60];
-        let mut it = xs.iter().chain(ys.iter());
+        let mut it = xs.iter().chain_(ys.iter());
         let mut i = 0;
         for it.advance |&x| {
             assert_eq!(x, expected[i]);
@@ -896,8 +898,8 @@ mod tests {
         }
         assert_eq!(i, expected.len());
 
-        let ys = Counter::new(30u, 10).take(4);
-        let mut it = xs.iter().transform(|&x| x).chain(ys);
+        let ys = Counter::new(30u, 10).take_(4);
+        let mut it = xs.iter().transform(|&x| x).chain_(ys);
         let mut i = 0;
         for it.advance |x| {
             assert_eq!(x, expected[i]);
@@ -908,7 +910,7 @@ mod tests {
 
     #[test]
     fn test_filter_map() {
-        let mut it = Counter::new(0u, 1u).take(10)
+        let mut it = Counter::new(0u, 1u).take_(10)
             .filter_map(|x| if x.is_even() { Some(x*x) } else { None });
         assert_eq!(it.collect::<~[uint]>(), ~[0*0, 2*2, 4*4, 6*6, 8*8]);
     }
@@ -965,7 +967,7 @@ mod tests {
     fn test_iterator_take() {
         let xs = [0u, 1, 2, 3, 5, 13, 15, 16, 17, 19];
         let ys = [0u, 1, 2, 3, 5];
-        let mut it = xs.iter().take(5);
+        let mut it = xs.iter().take_(5);
         let mut i = 0;
         for it.advance |&x| {
             assert_eq!(x, ys[i]);
@@ -1088,9 +1090,9 @@ mod tests {
     #[test]
     fn test_any() {
         let v = ~&[1, 2, 3, 4, 5];
-        assert!(v.iter().any(|&x| x < 10));
-        assert!(v.iter().any(|&x| x.is_even()));
-        assert!(!v.iter().any(|&x| x > 100));
-        assert!(!v.slice(0, 0).iter().any(|_| fail!()));
+        assert!(v.iter().any_(|&x| x < 10));
+        assert!(v.iter().any_(|&x| x.is_even()));
+        assert!(!v.iter().any_(|&x| x > 100));
+        assert!(!v.slice(0, 0).iter().any_(|_| fail!()));
     }
 }
diff --git a/src/libstd/option.rs b/src/libstd/option.rs
index 2386a779235..80f4fb7643c 100644
--- a/src/libstd/option.rs
+++ b/src/libstd/option.rs
@@ -46,12 +46,12 @@ use ops::Add;
 use kinds::Copy;
 use util;
 use num::Zero;
-use old_iter::{BaseIter, MutableIter, ExtendedIter};
-use old_iter;
+use iterator::Iterator;
 use str::StrSlice;
 use clone::DeepClone;
 
 #[cfg(test)] use str;
+#[cfg(test)] use iterator::IteratorUtil;
 
 /// The option type
 #[deriving(Clone, DeepClone, Eq)]
@@ -100,53 +100,27 @@ impl<T: Copy + Add<T,T>> Add<Option<T>, Option<T>> for Option<T> {
     }
 }
 
-impl<T> BaseIter<T> for Option<T> {
-    /// Performs an operation on the contained value by reference
-    #[inline(always)]
-    fn each<'a>(&'a self, f: &fn(x: &'a T) -> bool) -> bool {
-        match *self { None => true, Some(ref t) => { f(t) } }
-    }
-
-    #[inline(always)]
-    fn size_hint(&self) -> Option<uint> {
-        if self.is_some() { Some(1) } else { Some(0) }
-    }
-}
-
-impl<T> MutableIter<T> for Option<T> {
-    #[inline(always)]
-    fn each_mut<'a>(&'a mut self, f: &fn(&'a mut T) -> bool) -> bool {
-        match *self { None => true, Some(ref mut t) => { f(t) } }
+impl<T> Option<T> {
+    /// Return an iterator over the possibly contained value
+    #[inline]
+    pub fn iter<'r>(&'r self) -> OptionIterator<'r, T> {
+        match *self {
+            Some(ref x) => OptionIterator{opt: Some(x)},
+            None => OptionIterator{opt: None}
+        }
     }
-}
 
-impl<A> ExtendedIter<A> for Option<A> {
-    pub fn eachi(&self, blk: &fn(uint, v: &A) -> bool) -> bool {
-        old_iter::eachi(self, blk)
-    }
-    pub fn all(&self, blk: &fn(&A) -> bool) -> bool {
-        old_iter::all(self, blk)
-    }
-    pub fn any(&self, blk: &fn(&A) -> bool) -> bool {
-        old_iter::any(self, blk)
-    }
-    pub fn foldl<B>(&self, b0: B, blk: &fn(&B, &A) -> B) -> B {
-        old_iter::foldl(self, b0, blk)
-    }
-    pub fn position(&self, f: &fn(&A) -> bool) -> Option<uint> {
-        old_iter::position(self, f)
-    }
-    fn map_to_vec<B>(&self, op: &fn(&A) -> B) -> ~[B] {
-        old_iter::map_to_vec(self, op)
-    }
-    fn flat_map_to_vec<B,IB:BaseIter<B>>(&self, op: &fn(&A) -> IB)
-        -> ~[B] {
-        old_iter::flat_map_to_vec(self, op)
+    /// Return a mutable iterator over the possibly contained value
+    #[inline]
+    pub fn mut_iter<'r>(&'r mut self) -> OptionMutIterator<'r, T> {
+        match *self {
+            Some(ref mut x) => OptionMutIterator{opt: Some(x)},
+            None => OptionMutIterator{opt: None}
+        }
     }
-}
 
-impl<T> Option<T> {
     /// Returns true if the option equals `none`
+    #[inline]
     pub fn is_none(&const self) -> bool {
         match *self { None => true, Some(_) => false }
     }
@@ -376,6 +350,28 @@ impl<T:Copy + Zero> Option<T> {
     }
 }
 
+/// Immutable iterator over an `Option<A>`
+pub struct OptionIterator<'self, A> {
+    priv opt: Option<&'self A>
+}
+
+impl<'self, A> Iterator<&'self A> for OptionIterator<'self, A> {
+    fn next(&mut self) -> Option<&'self A> {
+        util::replace(&mut self.opt, None)
+    }
+}
+
+/// Mutable iterator over an `Option<A>`
+pub struct OptionMutIterator<'self, A> {
+    priv opt: Option<&'self mut A>
+}
+
+impl<'self, A> Iterator<&'self mut A> for OptionMutIterator<'self, A> {
+    fn next(&mut self) -> Option<&'self mut A> {
+        util::replace(&mut self.opt, None)
+    }
+}
+
 #[test]
 fn test_unwrap_ptr() {
     unsafe {
@@ -429,7 +425,7 @@ fn test_option_dance() {
     let x = Some(());
     let mut y = Some(5);
     let mut y2 = 0;
-    for x.each |_x| {
+    for x.iter().advance |_x| {
         y2 = y.swap_unwrap();
     }
     assert_eq!(y2, 5);
diff --git a/src/libstd/os.rs b/src/libstd/os.rs
index ab8965a6796..66993fb9099 100644
--- a/src/libstd/os.rs
+++ b/src/libstd/os.rs
@@ -1574,7 +1574,7 @@ mod tests {
         setenv("HOME", "");
         assert!(os::homedir().is_none());
 
-        for oldhome.each |s| { setenv("HOME", *s) }
+        for oldhome.iter().advance |s| { setenv("HOME", *s) }
     }
 
     #[test]
diff --git a/src/libstd/run.rs b/src/libstd/run.rs
index 85015c9bc5e..6b2fecc5001 100644
--- a/src/libstd/run.rs
+++ b/src/libstd/run.rs
@@ -176,9 +176,9 @@ impl Process {
                                    in_fd, out_fd, err_fd);
 
         unsafe {
-            for in_pipe.each  |pipe| { libc::close(pipe.in); }
-            for out_pipe.each |pipe| { libc::close(pipe.out); }
-            for err_pipe.each |pipe| { libc::close(pipe.out); }
+            for in_pipe.iter().advance  |pipe| { libc::close(pipe.in); }
+            for out_pipe.iter().advance |pipe| { libc::close(pipe.out); }
+            for err_pipe.iter().advance |pipe| { libc::close(pipe.out); }
         }
 
         Process {
@@ -323,7 +323,7 @@ impl Process {
      * If the child has already been finished then the exit code is returned.
      */
     pub fn finish(&mut self) -> int {
-        for self.exit_code.each |&code| {
+        for self.exit_code.iter().advance |&code| {
             return code;
         }
         self.close_input();
@@ -523,7 +523,7 @@ fn spawn_process_os(prog: &str, args: &[~str],
         CloseHandle(si.hStdOutput);
         CloseHandle(si.hStdError);
 
-        for create_err.each |msg| {
+        for create_err.iter().advance |msg| {
             fail!("failure in CreateProcess: %s", *msg);
         }
 
@@ -589,7 +589,7 @@ pub fn make_command_line(prog: &str, args: &[~str]) -> ~str {
     return cmd;
 
     fn append_arg(cmd: &mut ~str, arg: &str) {
-        let quote = arg.iter().any(|c| c == ' ' || c == '\t');
+        let quote = arg.iter().any_(|c| c == ' ' || c == '\t');
         if quote {
             cmd.push_char('"');
         }
diff --git a/src/libstd/task/spawn.rs b/src/libstd/task/spawn.rs
index bc409e06633..780e9d6d923 100644
--- a/src/libstd/task/spawn.rs
+++ b/src/libstd/task/spawn.rs
@@ -92,6 +92,7 @@ use uint;
 use util;
 use unstable::sync::{Exclusive, exclusive};
 use rt::local::Local;
+use iterator::{Iterator, IteratorUtil};
 
 #[cfg(test)] use task::default_task_opts;
 #[cfg(test)] use comm;
@@ -276,7 +277,7 @@ fn each_ancestor(list:        &mut AncestorList,
                  * Step 3: Maybe unwind; compute return info for our caller.
                  *##########################################################*/
                 if need_unwind && !nobe_is_dead {
-                    for bail_opt.each |bail_blk| {
+                    for bail_opt.iter().advance |bail_blk| {
                         do with_parent_tg(&mut nobe.parent_group) |tg_opt| {
                             (*bail_blk)(tg_opt)
                         }
@@ -328,7 +329,7 @@ impl Drop for TCB {
 
             // If we are failing, the whole taskgroup needs to die.
             if rt::rust_task_is_unwinding(self.me) {
-                for this.notifier.each_mut |x| {
+                for this.notifier.mut_iter().advance |x| {
                     x.failed = true;
                 }
                 // Take everybody down with us.
@@ -357,7 +358,7 @@ fn TCB(me: *rust_task,
        ancestors: AncestorList,
        is_main: bool,
        mut notifier: Option<AutoNotify>) -> TCB {
-    for notifier.each_mut |x| {
+    for notifier.mut_iter().advance |x| {
         x.failed = false;
     }
 
diff --git a/src/libstd/unstable/extfmt.rs b/src/libstd/unstable/extfmt.rs
index 9b74119b08c..8a6f58d7992 100644
--- a/src/libstd/unstable/extfmt.rs
+++ b/src/libstd/unstable/extfmt.rs
@@ -76,8 +76,8 @@ debug!("hello, %s!", "world");
 
 */
 
-use cmp::Eq;
 use prelude::*;
+use iterator::IteratorUtil;
 
 /*
  * We have a 'ct' (compile-time) module that parses format strings into a
@@ -607,7 +607,7 @@ pub mod rt {
         let headsize = match head { Some(_) => 1, _ => 0 };
         let uwidth : uint = match cv.width {
             CountImplied => {
-                for head.each |&c| {
+                for head.iter().advance |&c| {
                     buf.push_char(c);
                 }
                 return buf.push_str(s);
@@ -616,7 +616,7 @@ pub mod rt {
         };
         let strlen = str::char_len(s) + headsize;
         if uwidth <= strlen {
-            for head.each |&c| {
+            for head.iter().advance |&c| {
                 buf.push_char(c);
             }
             return buf.push_str(s);
@@ -624,7 +624,7 @@ pub mod rt {
         let mut padchar = ' ';
         let diff = uwidth - strlen;
         if have_flag(cv.flags, flag_left_justify) {
-            for head.each |&c| {
+            for head.iter().advance |&c| {
                 buf.push_char(c);
             }
             buf.push_str(s);
@@ -658,7 +658,7 @@ pub mod rt {
         // instead.
 
         if signed && zero_padding {
-            for head.each |&head| {
+            for head.iter().advance |&head| {
                 if head == '+' || head == '-' || head == ' ' {
                     buf.push_char(head);
                     buf.push_str(padstr);
@@ -668,7 +668,7 @@ pub mod rt {
             }
         }
         buf.push_str(padstr);
-        for head.each |&c| {
+        for head.iter().advance |&c| {
             buf.push_char(c);
         }
         buf.push_str(s);