about summary refs log tree commit diff
path: root/src/libextra
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-08-01 12:52:29 -0700
committerbors <bors@rust-lang.org>2013-08-01 12:52:29 -0700
commit82b24559e6aa0914f8a49e0a9dbfb3cf35372515 (patch)
treec2b0ff9b26400eac3f3405d78fe89dc07607c3ae /src/libextra
parent479809a267dbbcc3e2ec87da677c63430d3d229a (diff)
parent94f1a5d6f8ecd30c6f59dfeaacdd5962f58bc44c (diff)
downloadrust-82b24559e6aa0914f8a49e0a9dbfb3cf35372515.tar.gz
rust-82b24559e6aa0914f8a49e0a9dbfb3cf35372515.zip
auto merge of #8190 : thestinger/rust/for, r=thestinger
Diffstat (limited to 'src/libextra')
-rw-r--r--src/libextra/arc.rs12
-rw-r--r--src/libextra/arena.rs5
-rw-r--r--src/libextra/base64.rs6
-rw-r--r--src/libextra/bitv.rs191
-rw-r--r--src/libextra/dlist.rs2
-rw-r--r--src/libextra/flate.rs6
-rw-r--r--src/libextra/getopts.rs9
-rw-r--r--src/libextra/iter.rs41
-rw-r--r--src/libextra/json.rs2
-rw-r--r--src/libextra/list.rs9
-rw-r--r--src/libextra/ringbuf.rs2
-rw-r--r--src/libextra/serialize.rs10
-rw-r--r--src/libextra/sort.rs8
-rw-r--r--src/libextra/sync.rs24
-rw-r--r--src/libextra/task_pool.rs2
-rw-r--r--src/libextra/tempfile.rs3
-rw-r--r--src/libextra/treemap.rs98
17 files changed, 235 insertions, 195 deletions
diff --git a/src/libextra/arc.rs b/src/libextra/arc.rs
index fe13430327a..f235a3d2db3 100644
--- a/src/libextra/arc.rs
+++ b/src/libextra/arc.rs
@@ -23,7 +23,7 @@
  * let numbers=vec::from_fn(100, |ind| (ind as float)*rand::random());
  * let shared_numbers=arc::Arc::new(numbers);
  *
- *   for 10.times {
+ *   do 10.times {
  *       let (port, chan)  = stream();
  *       chan.send(shared_numbers.clone());
  *
@@ -761,7 +761,7 @@ mod tests {
 
         do task::spawn || {
             do arc2.write |num| {
-                for 10.times {
+                do 10.times {
                     let tmp = *num;
                     *num = -1;
                     task::yield();
@@ -773,7 +773,7 @@ mod tests {
 
         // Readers try to catch the writer in the act
         let mut children = ~[];
-        for 5.times {
+        do 5.times {
             let arc3 = (*arc).clone();
             let mut builder = task::task();
             builder.future_result(|r| children.push(r));
@@ -807,7 +807,7 @@ mod tests {
 
         // Reader tasks
         let mut reader_convos = ~[];
-        for 10.times {
+        do 10.times {
             let ((rp1,rc1),(rp2,rc2)) = (comm::stream(),comm::stream());
             reader_convos.push((rc1, rp2));
             let arcn = (*arc).clone();
@@ -921,7 +921,7 @@ mod tests {
             do read_mode.read |state| {
                 // if writer mistakenly got in, make sure it mutates state
                 // before we assert on it
-                for 5.times { task::yield(); }
+                do 5.times { task::yield(); }
                 // make sure writer didn't get in.
                 assert!(*state);
             }
@@ -933,6 +933,6 @@ mod tests {
         // helped to expose the race nearly 100% of the time... but adding
         // yields in the intuitively-right locations made it even less likely,
         // and I wasn't sure why :( . This is a mediocre "next best" option.
-        for 8.times { test_rw_write_cond_downgrade_read_race_helper() }
+        do 8.times { test_rw_write_cond_downgrade_read_race_helper() }
     }
 }
diff --git a/src/libextra/arena.rs b/src/libextra/arena.rs
index efe1d47563e..31acb5bd498 100644
--- a/src/libextra/arena.rs
+++ b/src/libextra/arena.rs
@@ -72,11 +72,12 @@ impl Drop for Arena {
     fn drop(&self) {
         unsafe {
             destroy_chunk(&self.head);
-            for self.chunks.each |chunk| {
+            do self.chunks.each |chunk| {
                 if !chunk.is_pod {
                     destroy_chunk(chunk);
                 }
-            }
+                true
+            };
         }
     }
 }
diff --git a/src/libextra/base64.rs b/src/libextra/base64.rs
index 5061dbf401b..5d5311d2329 100644
--- a/src/libextra/base64.rs
+++ b/src/libextra/base64.rs
@@ -358,9 +358,9 @@ mod test {
         use std::rand::{task_rng, random, RngUtil};
         use std::vec;
 
-        for 1000.times {
+        do 1000.times {
             let v: ~[u8] = do vec::build |push| {
-                for task_rng().gen_uint_range(1, 100).times {
+                do task_rng().gen_uint_range(1, 100).times {
                     push(random());
                 }
             };
@@ -389,4 +389,4 @@ mod test {
         bh.bytes = b.len() as u64;
     }
 
-}
\ No newline at end of file
+}
diff --git a/src/libextra/bitv.rs b/src/libextra/bitv.rs
index 4d2d5635eff..570186b65a6 100644
--- a/src/libextra/bitv.rs
+++ b/src/libextra/bitv.rs
@@ -12,7 +12,8 @@
 
 
 use std::cmp;
-use std::iterator::{DoubleEndedIterator, RandomAccessIterator, Invert};
+use std::iterator::RandomAccessIterator;
+use std::iterator::{Invert, Enumerate};
 use std::num;
 use std::ops;
 use std::uint;
@@ -164,7 +165,7 @@ impl BigBitv {
     }
 
     #[inline]
-    pub fn negate(&mut self) { for self.each_storage |w| { *w = !*w } }
+    pub fn negate(&mut self) { do self.each_storage |w| { *w = !*w; true }; }
 
     #[inline]
     pub fn union(&mut self, b: &BigBitv, nbits: uint) -> bool {
@@ -646,9 +647,10 @@ impl BitvSet {
     /// Creates a new bit vector set from the given bit vector
     pub fn from_bitv(bitv: Bitv) -> BitvSet {
         let mut size = 0;
-        for bitv.ones |_| {
+        do bitv.ones |_| {
             size += 1;
-        }
+            true
+        };
         let Bitv{rep, _} = bitv;
         match rep {
             Big(b) => BitvSet{ size: size, bitv: b },
@@ -672,7 +674,7 @@ impl BitvSet {
     fn other_op(&mut self, other: &BitvSet, f: &fn(uint, uint) -> uint) {
         fn nbits(mut w: uint) -> uint {
             let mut bits = 0;
-            for uint::bits.times {
+            for uint::range(0, uint::bits) |_| {
                 if w == 0 {
                     break;
                 }
@@ -715,6 +717,41 @@ impl BitvSet {
     pub fn iter<'a>(&'a self) -> BitvSetIterator<'a> {
         BitvSetIterator {set: self, next_idx: 0}
     }
+
+    pub fn difference(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
+        for self.common_iter(other).advance |(i, w1, w2)| {
+            if !iterate_bits(i, w1 & !w2, |b| f(&b)) {
+                return false;
+            }
+        }
+        /* everything we have that they don't also shows up */
+        self.outlier_iter(other).advance(|(mine, i, w)|
+            !mine || iterate_bits(i, w, |b| f(&b))
+        )
+    }
+
+    pub fn symmetric_difference(&self, other: &BitvSet,
+                            f: &fn(&uint) -> bool) -> bool {
+        for self.common_iter(other).advance |(i, w1, w2)| {
+            if !iterate_bits(i, w1 ^ w2, |b| f(&b)) {
+                return false;
+            }
+        }
+        self.outlier_iter(other).advance(|(_, i, w)| iterate_bits(i, w, |b| f(&b)))
+    }
+
+    pub fn intersection(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
+        self.common_iter(other).advance(|(i, w1, w2)| iterate_bits(i, w1 & w2, |b| f(&b)))
+    }
+
+    pub fn union(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
+        for self.common_iter(other).advance |(i, w1, w2)| {
+            if !iterate_bits(i, w1 | w2, |b| f(&b)) {
+                return false;
+            }
+        }
+        self.outlier_iter(other).advance(|(_, i, w)| iterate_bits(i, w, |b| f(&b)))
+    }
 }
 
 impl cmp::Eq for BitvSet {
@@ -722,12 +759,12 @@ impl cmp::Eq for BitvSet {
         if self.size != other.size {
             return false;
         }
-        for self.each_common(other) |_, w1, w2| {
+        for self.common_iter(other).advance |(_, w1, w2)| {
             if w1 != w2 {
                 return false;
             }
         }
-        for self.each_outlier(other) |_, _, w| {
+        for self.outlier_iter(other).advance |(_, _, w)| {
             if w != 0 {
                 return false;
             }
@@ -745,7 +782,7 @@ impl Container for BitvSet {
 
 impl Mutable for BitvSet {
     fn clear(&mut self) {
-        for self.bitv.each_storage |w| { *w = 0; }
+        do self.bitv.each_storage |w| { *w = 0; true };
         self.size = 0;
     }
 }
@@ -756,14 +793,13 @@ impl Set<uint> for BitvSet {
     }
 
     fn is_disjoint(&self, other: &BitvSet) -> bool {
-        for self.intersection(other) |_| {
-            return false;
+        do self.intersection(other) |_| {
+            false
         }
-        return true;
     }
 
     fn is_subset(&self, other: &BitvSet) -> bool {
-        for self.each_common(other) |_, w1, w2| {
+        for self.common_iter(other).advance |(_, w1, w2)| {
             if w1 & w2 != w1 {
                 return false;
             }
@@ -771,7 +807,7 @@ impl Set<uint> for BitvSet {
         /* If anything is not ours, then everything is not ours so we're
            definitely a subset in that case. Otherwise if there's any stray
            ones that 'other' doesn't have, we're not a subset. */
-        for self.each_outlier(other) |mine, _, w| {
+        for self.outlier_iter(other).advance |(mine, _, w)| {
             if !mine {
                 return true;
             } else if w != 0 {
@@ -784,41 +820,6 @@ impl Set<uint> for BitvSet {
     fn is_superset(&self, other: &BitvSet) -> bool {
         other.is_subset(self)
     }
-
-    fn difference(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
-        for self.each_common(other) |i, w1, w2| {
-            if !iterate_bits(i, w1 & !w2, |b| f(&b)) {
-                return false;
-            }
-        }
-        /* everything we have that they don't also shows up */
-        self.each_outlier(other, |mine, i, w|
-            !mine || iterate_bits(i, w, |b| f(&b))
-        )
-    }
-
-    fn symmetric_difference(&self, other: &BitvSet,
-                            f: &fn(&uint) -> bool) -> bool {
-        for self.each_common(other) |i, w1, w2| {
-            if !iterate_bits(i, w1 ^ w2, |b| f(&b)) {
-                return false;
-            }
-        }
-        self.each_outlier(other, |_, i, w| iterate_bits(i, w, |b| f(&b)))
-    }
-
-    fn intersection(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
-        self.each_common(other, |i, w1, w2| iterate_bits(i, w1 & w2, |b| f(&b)))
-    }
-
-    fn union(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
-        for self.each_common(other) |i, w1, w2| {
-            if !iterate_bits(i, w1 | w2, |b| f(&b)) {
-                return false;
-            }
-        }
-        self.each_outlier(other, |_, i, w| iterate_bits(i, w, |b| f(&b)))
-    }
 }
 
 impl MutableSet<uint> for BitvSet {
@@ -860,13 +861,14 @@ impl BitvSet {
     /// both have in common. The three yielded arguments are (bit location,
     /// w1, w2) where the bit location is the number of bits offset so far,
     /// and w1/w2 are the words coming from the two vectors self, other.
-    fn each_common(&self, other: &BitvSet,
-                   f: &fn(uint, uint, uint) -> bool) -> bool {
+    fn common_iter<'a>(&'a self, other: &'a BitvSet)
+        -> MapE<(uint,&uint),(uint,uint,uint), &'a ~[uint],Enumerate<vec::VecIterator<'a,uint>>> {
         let min = num::min(self.bitv.storage.len(),
                             other.bitv.storage.len());
-        self.bitv.storage.slice(0, min).iter().enumerate().advance(|(i, &w)| {
-            f(i * uint::bits, w, other.bitv.storage[i])
-        })
+        MapE{iter: self.bitv.storage.slice(0, min).iter().enumerate(),
+             env: &other.bitv.storage,
+             f: |(i, &w): (uint, &uint), o_store| (i * uint::bits, w, o_store[i])
+        }
     }
 
     /// Visits each word in self or other that extends beyond the other. This
@@ -876,24 +878,45 @@ impl BitvSet {
     /// The yielded arguments are a bool, the bit offset, and a word. The bool
     /// is true if the word comes from 'self', and false if it comes from
     /// 'other'.
-    fn each_outlier(&self, other: &BitvSet,
-                    f: &fn(bool, uint, uint) -> bool) -> bool {
+    fn outlier_iter<'a>(&'a self, other: &'a BitvSet)
+        -> MapE<(uint, &uint),(bool, uint, uint), uint, Enumerate<vec::VecIterator<'a, uint>>> {
         let len1 = self.bitv.storage.len();
         let len2 = other.bitv.storage.len();
         let min = num::min(len1, len2);
 
-        /* only one of these loops will execute and that's the point */
-        foreach (i, &w) in self.bitv.storage.slice(min, len1).iter().enumerate() {
-            if !f(true, (i + min) * uint::bits, w) {
-                return false;
+        if min < len1 {
+            MapE{iter: self.bitv.storage.slice(min, len1).iter().enumerate(),
+                 env: min,
+                 f: |(i, &w): (uint, &uint), min| (true, (i + min) * uint::bits, w)
             }
-        }
-        foreach (i, &w) in other.bitv.storage.slice(min, len2).iter().enumerate() {
-            if !f(false, (i + min) * uint::bits, w) {
-                return false;
+        } else {
+            MapE{iter: other.bitv.storage.slice(min, len2).iter().enumerate(),
+                 env: min,
+                 f: |(i, &w): (uint, &uint), min| (false, (i + min) * uint::bits, w)
             }
         }
-        return true;
+    }
+}
+
+/// Like iterator::Map with explicit env capture
+struct MapE<A, B, Env, I> {
+    priv env: Env,
+    priv f: &'static fn(A, Env) -> B,
+    priv iter: I,
+}
+
+impl<'self, A, B, Env: Clone, I: Iterator<A>> Iterator<B> for MapE<A, B, Env, I> {
+    #[inline]
+    fn next(&mut self) -> Option<B> {
+        match self.iter.next() {
+            Some(elt) => Some((self.f)(elt, self.env.clone())),
+            None => None
+        }
+    }
+
+    #[inline]
+    fn size_hint(&self) -> (uint, Option<uint>) {
+        self.iter.size_hint()
     }
 }
 
@@ -1354,18 +1377,18 @@ mod tests {
     fn test_small_clear() {
         let mut b = Bitv::new(14, true);
         b.clear();
-        for b.ones |i| {
-            fail!("found 1 at %?", i);
-        }
+        do b.ones |i| {
+            fail!("found 1 at %?", i)
+        };
     }
 
     #[test]
     fn test_big_clear() {
         let mut b = Bitv::new(140, true);
         b.clear();
-        for b.ones |i| {
-            fail!("found 1 at %?", i);
-        }
+        do b.ones |i| {
+            fail!("found 1 at %?", i)
+        };
     }
 
     #[test]
@@ -1400,10 +1423,11 @@ mod tests {
 
         let mut i = 0;
         let expected = [3, 5, 11, 77];
-        for a.intersection(&b) |x| {
+        do a.intersection(&b) |x| {
             assert_eq!(*x, expected[i]);
-            i += 1
-        }
+            i += 1;
+            true
+        };
         assert_eq!(i, expected.len());
     }
 
@@ -1423,10 +1447,11 @@ mod tests {
 
         let mut i = 0;
         let expected = [1, 5, 500];
-        for a.difference(&b) |x| {
+        do a.difference(&b) |x| {
             assert_eq!(*x, expected[i]);
-            i += 1
-        }
+            i += 1;
+            true
+        };
         assert_eq!(i, expected.len());
     }
 
@@ -1448,10 +1473,11 @@ mod tests {
 
         let mut i = 0;
         let expected = [1, 5, 11, 14, 220];
-        for a.symmetric_difference(&b) |x| {
+        do a.symmetric_difference(&b) |x| {
             assert_eq!(*x, expected[i]);
-            i += 1
-        }
+            i += 1;
+            true
+        };
         assert_eq!(i, expected.len());
     }
 
@@ -1476,10 +1502,11 @@ mod tests {
 
         let mut i = 0;
         let expected = [1, 3, 5, 9, 11, 13, 19, 24, 160];
-        for a.union(&b) |x| {
+        do a.union(&b) |x| {
             assert_eq!(*x, expected[i]);
-            i += 1
-        }
+            i += 1;
+            true
+        };
         assert_eq!(i, expected.len());
     }
 
diff --git a/src/libextra/dlist.rs b/src/libextra/dlist.rs
index c684ff14615..b2b39d34ce2 100644
--- a/src/libextra/dlist.rs
+++ b/src/libextra/dlist.rs
@@ -933,7 +933,7 @@ mod tests {
 
     #[test]
     fn test_fuzz() {
-        for 25.times {
+        do 25.times {
             fuzz_test(3);
             fuzz_test(16);
             fuzz_test(189);
diff --git a/src/libextra/flate.rs b/src/libextra/flate.rs
index 57edaa53eaf..d330b0ea163 100644
--- a/src/libextra/flate.rs
+++ b/src/libextra/flate.rs
@@ -90,13 +90,13 @@ mod tests {
     fn test_flate_round_trip() {
         let mut r = rand::rng();
         let mut words = ~[];
-        for 20.times {
+        do 20.times {
             let range = r.gen_uint_range(1, 10);
             words.push(r.gen_bytes(range));
         }
-        for 20.times {
+        do 20.times {
             let mut input = ~[];
-            for 2000.times {
+            do 2000.times {
                 input.push_all(r.choose(words));
             }
             debug!("de/inflate of %u bytes of random word-sequences",
diff --git a/src/libextra/getopts.rs b/src/libextra/getopts.rs
index b0e6f82322b..31a73833e27 100644
--- a/src/libextra/getopts.rs
+++ b/src/libextra/getopts.rs
@@ -662,7 +662,7 @@ pub mod groups {
             // here we just need to indent the start of the description
             let rowlen = row.len();
             if rowlen < 24 {
-                for (24 - rowlen).times {
+                do (24 - rowlen).times {
                     row.push_char(' ')
                 }
             } else {
@@ -678,9 +678,10 @@ pub mod groups {
 
             // FIXME: #5516
             let mut desc_rows = ~[];
-            for each_split_within(desc_normalized_whitespace, 54) |substr| {
+            do each_split_within(desc_normalized_whitespace, 54) |substr| {
                 desc_rows.push(substr.to_owned());
-            }
+                true
+            };
 
             // FIXME: #5516
             // wrapped description
@@ -780,7 +781,7 @@ pub mod groups {
     priv fn test_split_within() {
         fn t(s: &str, i: uint, u: &[~str]) {
             let mut v = ~[];
-            for each_split_within(s, i) |s| { v.push(s.to_owned()) }
+            do each_split_within(s, i) |s| { v.push(s.to_owned()); true };
             assert!(v.iter().zip(u.iter()).all(|(a,b)| a == b));
         }
         t("", 0, []);
diff --git a/src/libextra/iter.rs b/src/libextra/iter.rs
index 3552ff59783..720a525164a 100644
--- a/src/libextra/iter.rs
+++ b/src/libextra/iter.rs
@@ -72,12 +72,9 @@ pub trait FromIter<T> {
 #[inline]
 pub fn any<T>(predicate: &fn(T) -> bool,
               iter: &fn(f: &fn(T) -> bool) -> bool) -> bool {
-    for iter |x| {
-        if predicate(x) {
-            return true;
-        }
+    do iter |x| {
+        predicate(x)
     }
-    return false;
 }
 
 /**
@@ -111,12 +108,14 @@ pub fn all<T>(predicate: &fn(T) -> bool,
 #[inline]
 pub fn find<T>(predicate: &fn(&T) -> bool,
                iter: &fn(f: &fn(T) -> bool) -> bool) -> Option<T> {
-    for iter |x| {
+    let mut ret = None;
+    do iter |x| {
         if predicate(&x) {
-            return Some(x);
-        }
-    }
-    None
+            ret = Some(x);
+            false
+        } else { true }
+    };
+    ret
 }
 
 /**
@@ -132,7 +131,7 @@ pub fn find<T>(predicate: &fn(&T) -> bool,
 #[inline]
 pub fn max<T: Ord>(iter: &fn(f: &fn(T) -> bool) -> bool) -> Option<T> {
     let mut result = None;
-    for iter |x| {
+    do iter |x| {
         match result {
             Some(ref mut y) => {
                 if x > *y {
@@ -141,7 +140,8 @@ pub fn max<T: Ord>(iter: &fn(f: &fn(T) -> bool) -> bool) -> Option<T> {
             }
             None => result = Some(x)
         }
-    }
+        true
+    };
     result
 }
 
@@ -158,7 +158,7 @@ pub fn max<T: Ord>(iter: &fn(f: &fn(T) -> bool) -> bool) -> Option<T> {
 #[inline]
 pub fn min<T: Ord>(iter: &fn(f: &fn(T) -> bool) -> bool) -> Option<T> {
     let mut result = None;
-    for iter |x| {
+    do iter |x| {
         match result {
             Some(ref mut y) => {
                 if x < *y {
@@ -167,7 +167,8 @@ pub fn min<T: Ord>(iter: &fn(f: &fn(T) -> bool) -> bool) -> Option<T> {
             }
             None => result = Some(x)
         }
-    }
+        true
+    };
     result
 }
 
@@ -183,9 +184,10 @@ pub fn min<T: Ord>(iter: &fn(f: &fn(T) -> bool) -> bool) -> Option<T> {
 #[inline]
 pub fn fold<T, U>(start: T, iter: &fn(f: &fn(U) -> bool) -> bool, f: &fn(&mut T, U)) -> T {
     let mut result = start;
-    for iter |x| {
+    do iter |x| {
         f(&mut result, x);
-    }
+        true
+    };
     result
 }
 
@@ -206,9 +208,10 @@ pub fn fold<T, U>(start: T, iter: &fn(f: &fn(U) -> bool) -> bool, f: &fn(&mut T,
 #[inline]
 pub fn fold_ref<T, U>(start: T, iter: &fn(f: &fn(&U) -> bool) -> bool, f: &fn(&mut T, &U)) -> T {
     let mut result = start;
-    for iter |x| {
+    do iter |x| {
         f(&mut result, x);
-    }
+        true
+    };
     result
 }
 
@@ -246,7 +249,7 @@ impl<T> FromIter<T> for ~[T]{
     #[inline]
     pub fn from_iter(iter: &fn(f: &fn(T) -> bool) -> bool) -> ~[T] {
         let mut v = ~[];
-        for iter |x| { v.push(x) }
+        do iter |x| { v.push(x); true };
         v
     }
 }
diff --git a/src/libextra/json.rs b/src/libextra/json.rs
index f0d5b336e30..a9f4276e79e 100644
--- a/src/libextra/json.rs
+++ b/src/libextra/json.rs
@@ -77,7 +77,7 @@ fn escape_str(s: &str) -> ~str {
 
 fn spaces(n: uint) -> ~str {
     let mut ss = ~"";
-    for n.times {
+    do n.times {
         ss.push_str(" ");
     }
     return ss;
diff --git a/src/libextra/list.rs b/src/libextra/list.rs
index 8f7ade7228b..0e8c50ac873 100644
--- a/src/libextra/list.rs
+++ b/src/libextra/list.rs
@@ -70,10 +70,11 @@ pub fn find<T:Clone>(ls: @List<T>, f: &fn(&T) -> bool) -> Option<T> {
 
 /// Returns true if a list contains an element with the given value
 pub fn has<T:Eq>(ls: @List<T>, elt: T) -> bool {
-    for each(ls) |e| {
-        if *e == elt { return true; }
-    }
-    return false;
+    let mut found = false;
+    do each(ls) |e| {
+        if *e == elt { found = true; false } else { true }
+    };
+    return found;
 }
 
 /// Returns true if the list is empty
diff --git a/src/libextra/ringbuf.rs b/src/libextra/ringbuf.rs
index e2950293719..9cb45a2ec7a 100644
--- a/src/libextra/ringbuf.rs
+++ b/src/libextra/ringbuf.rs
@@ -509,7 +509,7 @@ mod tests {
     fn bench_grow(b: &mut test::BenchHarness) {
         let mut deq = RingBuf::new();
         do b.iter {
-            for 65.times {
+            do 65.times {
                 deq.push_front(1);
             }
         }
diff --git a/src/libextra/serialize.rs b/src/libextra/serialize.rs
index 0c8821e753e..95f3af006e8 100644
--- a/src/libextra/serialize.rs
+++ b/src/libextra/serialize.rs
@@ -784,11 +784,12 @@ impl<
     fn encode(&self, e: &mut E) {
         do e.emit_map(self.len()) |e| {
             let mut i = 0;
-            for self.each |key, val| {
+            do self.each |key, val| {
                 e.emit_map_elt_key(i, |e| key.encode(e));
                 e.emit_map_elt_val(i, |e| val.encode(e));
                 i += 1;
-            }
+                true
+            };
         }
     }
 }
@@ -814,10 +815,11 @@ impl<S: Encoder> Encodable<S> for TrieSet {
     fn encode(&self, s: &mut S) {
         do s.emit_seq(self.len()) |s| {
             let mut i = 0;
-            for self.each |e| {
+            do self.each |e| {
                 s.emit_seq_elt(i, |s| e.encode(s));
                 i += 1;
-            }
+                true
+            };
         }
     }
 }
diff --git a/src/libextra/sort.rs b/src/libextra/sort.rs
index 9cce19da524..9832ff7396b 100644
--- a/src/libextra/sort.rs
+++ b/src/libextra/sort.rs
@@ -1081,7 +1081,7 @@ mod big_tests {
             tim_sort(arr); // /sort
             isSorted(arr);
 
-            for 3.times {
+            do 3.times {
                 let i1 = rng.gen_uint_range(0, n);
                 let i2 = rng.gen_uint_range(0, n);
                 arr.swap(i1, i2);
@@ -1100,7 +1100,7 @@ mod big_tests {
             tim_sort(arr); // +sort
             isSorted(arr);
 
-            for (n/100).times {
+            do (n/100).times {
                 let idx = rng.gen_uint_range(0, n);
                 arr[idx] = rng.gen();
             }
@@ -1153,7 +1153,7 @@ mod big_tests {
             tim_sort(arr); // /sort
             isSorted(arr);
 
-            for 3.times {
+            do 3.times {
                 let i1 = rng.gen_uint_range(0, n);
                 let i2 = rng.gen_uint_range(0, n);
                 arr.swap(i1, i2);
@@ -1172,7 +1172,7 @@ mod big_tests {
             tim_sort(arr); // +sort
             isSorted(arr);
 
-            for (n/100).times {
+            do (n/100).times {
                 let idx = rng.gen_uint_range(0, n);
                 arr[idx] = @rng.gen();
             }
diff --git a/src/libextra/sync.rs b/src/libextra/sync.rs
index dc26d1e36ce..e539b067edd 100644
--- a/src/libextra/sync.rs
+++ b/src/libextra/sync.rs
@@ -106,7 +106,7 @@ impl<Q:Send> Sem<Q> {
                 }
             }
             // Uncomment if you wish to test for sem races. Not valgrind-friendly.
-            /* for 1000.times { task::yield(); } */
+            /* do 1000.times { task::yield(); } */
             // Need to wait outside the exclusive.
             if waiter_nobe.is_some() {
                 let _ = comm::recv_one(waiter_nobe.unwrap());
@@ -143,7 +143,7 @@ impl Sem<~[WaitQueue]> {
     fn new_and_signal(count: int, num_condvars: uint)
         -> Sem<~[WaitQueue]> {
         let mut queues = ~[];
-        for num_condvars.times {
+        do num_condvars.times {
             queues.push(WaitQueue::new());
         }
         Sem::new(count, queues)
@@ -826,11 +826,11 @@ mod tests {
         let s2 = ~s.clone();
         do task::spawn || {
             do s2.access {
-                for 5.times { task::yield(); }
+                do 5.times { task::yield(); }
             }
         }
         do s.access {
-            for 5.times { task::yield(); }
+            do 5.times { task::yield(); }
         }
     }
     #[test]
@@ -843,7 +843,7 @@ mod tests {
             s2.acquire();
             c.send(());
         }
-        for 5.times { task::yield(); }
+        do 5.times { task::yield(); }
         s.release();
         let _ = p.recv();
 
@@ -852,7 +852,7 @@ mod tests {
         let s = ~Semaphore::new(0);
         let s2 = ~s.clone();
         do task::spawn || {
-            for 5.times { task::yield(); }
+            do 5.times { task::yield(); }
             s2.release();
             let _ = p.recv();
         }
@@ -895,7 +895,7 @@ mod tests {
                     c.send(());
                 }
                 let _ = p.recv(); // wait for child to come alive
-                for 5.times { task::yield(); } // let the child contend
+                do 5.times { task::yield(); } // let the child contend
             }
             let _ = p.recv(); // wait for child to be done
         }
@@ -929,7 +929,7 @@ mod tests {
         }
 
         fn access_shared(sharedstate: &mut int, m: &Mutex, n: uint) {
-            for n.times {
+            do n.times {
                 do m.lock {
                     let oldval = *sharedstate;
                     task::yield();
@@ -975,7 +975,7 @@ mod tests {
         let m = ~Mutex::new();
         let mut ports = ~[];
 
-        for num_waiters.times {
+        do num_waiters.times {
             let mi = ~m.clone();
             let (port, chan) = comm::stream();
             ports.push(port);
@@ -1065,7 +1065,7 @@ mod tests {
 
         let result: result::Result<(),()> = do task::try || {
             let mut sibling_convos = ~[];
-            for 2.times {
+            do 2.times {
                 let (p,c) = comm::stream();
                 let c = Cell::new(c);
                 sibling_convos.push(p);
@@ -1212,7 +1212,7 @@ mod tests {
 
         fn access_shared(sharedstate: &mut int, x: &RWLock, mode: RWLockMode,
                          n: uint) {
-            for n.times {
+            do n.times {
                 do lock_rwlock_in_mode(x, mode) {
                     let oldval = *sharedstate;
                     task::yield();
@@ -1343,7 +1343,7 @@ mod tests {
         let x = ~RWLock::new();
         let mut ports = ~[];
 
-        for num_waiters.times {
+        do num_waiters.times {
             let xi = (*x).clone();
             let (port, chan) = comm::stream();
             ports.push(port);
diff --git a/src/libextra/task_pool.rs b/src/libextra/task_pool.rs
index 523e11e810c..ddb3c31ec05 100644
--- a/src/libextra/task_pool.rs
+++ b/src/libextra/task_pool.rs
@@ -102,7 +102,7 @@ fn test_task_pool() {
         g
     };
     let mut pool = TaskPool::new(4, Some(SingleThreaded), f);
-    for 8.times {
+    do 8.times {
         pool.execute(|i| printfln!("Hello from thread %u!", *i));
     }
 }
diff --git a/src/libextra/tempfile.rs b/src/libextra/tempfile.rs
index c5fb4b9292e..0a2f32375f8 100644
--- a/src/libextra/tempfile.rs
+++ b/src/libextra/tempfile.rs
@@ -14,12 +14,13 @@
 use std::os;
 use std::rand::RngUtil;
 use std::rand;
+use std::uint;
 
 /// Attempts to make a temporary directory inside of `tmpdir` whose name will
 /// have the suffix `suffix`. If no directory can be created, None is returned.
 pub fn mkdtemp(tmpdir: &Path, suffix: &str) -> Option<Path> {
     let mut r = rand::rng();
-    for 1000.times {
+    for uint::range(0, 1000) |_| {
         let p = tmpdir.push(r.gen_str(16) + suffix);
         if os::make_dir(&p, 0x1c0) { // 700
             return Some(p);
diff --git a/src/libextra/treemap.rs b/src/libextra/treemap.rs
index 50ad5c77fba..9493a6fb0e0 100644
--- a/src/libextra/treemap.rs
+++ b/src/libextra/treemap.rs
@@ -16,6 +16,7 @@
 use std::num;
 use std::util::{swap, replace};
 use std::iterator::{FromIterator, Extendable};
+use std::uint;
 
 // This is implemented as an AA tree, which is a simplified variation of
 // a red-black tree where red (horizontal) nodes can only be added
@@ -47,7 +48,7 @@ impl<K: Eq + TotalOrd, V: Eq> Eq for TreeMap<K, V> {
         } else {
             let mut x = self.iter();
             let mut y = other.iter();
-            for self.len().times {
+            for uint::range(0, self.len()) |_| {
                 if x.next().unwrap() != y.next().unwrap() {
                     return false
                 }
@@ -65,7 +66,7 @@ fn lt<K: Ord + TotalOrd, V: Ord>(a: &TreeMap<K, V>,
     let mut y = b.iter();
 
     let (a_len, b_len) = (a.len(), b.len());
-    for num::min(a_len, b_len).times {
+    for uint::range(0, num::min(a_len, b_len)) |_| {
         let (key_a, value_a) = x.next().unwrap();
         let (key_b, value_b) = y.next().unwrap();
         if *key_a < *key_b { return true; }
@@ -396,9 +397,40 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
         }
         true
     }
+}
+
+impl<T: TotalOrd> MutableSet<T> for TreeSet<T> {
+    /// Add a value to the set. Return true if the value was not already
+    /// present in the set.
+    #[inline]
+    fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
+
+    /// Remove a value from the set. Return true if the value was
+    /// present in the set.
+    #[inline]
+    fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
+}
+
+impl<T: TotalOrd> TreeSet<T> {
+    /// Create an empty TreeSet
+    #[inline]
+    pub fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} }
+
+    /// Get a lazy iterator over the values in the set.
+    /// Requires that it be frozen (immutable).
+    #[inline]
+    pub fn iter<'a>(&'a self) -> TreeSetIterator<'a, T> {
+        TreeSetIterator{iter: self.map.iter()}
+    }
+
+    /// Visit all values in reverse order
+    #[inline]
+    pub fn each_reverse(&self, f: &fn(&T) -> bool) -> bool {
+        self.map.each_key_reverse(f)
+    }
 
     /// Visit the values (in-order) representing the difference
-    fn difference(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
+    pub fn difference(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
         let mut x = self.iter();
         let mut y = other.iter();
 
@@ -427,7 +459,7 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
     }
 
     /// Visit the values (in-order) representing the symmetric difference
-    fn symmetric_difference(&self, other: &TreeSet<T>,
+    pub fn symmetric_difference(&self, other: &TreeSet<T>,
                             f: &fn(&T) -> bool) -> bool {
         let mut x = self.iter();
         let mut y = other.iter();
@@ -461,7 +493,7 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
     }
 
     /// Visit the values (in-order) representing the intersection
-    fn intersection(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
+    pub fn intersection(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
         let mut x = self.iter();
         let mut y = other.iter();
 
@@ -487,7 +519,7 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
     }
 
     /// Visit the values (in-order) representing the union
-    fn union(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
+    pub fn union(&self, other: &TreeSet<T>, f: &fn(&T) -> bool) -> bool {
         let mut x = self.iter();
         let mut y = other.iter();
 
@@ -519,37 +551,6 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
     }
 }
 
-impl<T: TotalOrd> MutableSet<T> for TreeSet<T> {
-    /// Add a value to the set. Return true if the value was not already
-    /// present in the set.
-    #[inline]
-    fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
-
-    /// Remove a value from the set. Return true if the value was
-    /// present in the set.
-    #[inline]
-    fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
-}
-
-impl<T: TotalOrd> TreeSet<T> {
-    /// Create an empty TreeSet
-    #[inline]
-    pub fn new() -> TreeSet<T> { TreeSet{map: TreeMap::new()} }
-
-    /// Get a lazy iterator over the values in the set.
-    /// Requires that it be frozen (immutable).
-    #[inline]
-    pub fn iter<'a>(&'a self) -> TreeSetIterator<'a, T> {
-        TreeSetIterator{iter: self.map.iter()}
-    }
-
-    /// Visit all values in reverse order
-    #[inline]
-    pub fn each_reverse(&self, f: &fn(&T) -> bool) -> bool {
-        self.map.each_key_reverse(f)
-    }
-}
-
 /// Lazy forward iterator over a set
 pub struct TreeSetIterator<'self, T> {
     priv iter: TreeMapIterator<'self, T, ()>
@@ -931,8 +932,8 @@ mod test_treemap {
 
         let mut rng = rand::IsaacRng::new_seeded(&[42]);
 
-        for 3.times {
-            for 90.times {
+        do 3.times {
+            do 90.times {
                 let k = rng.gen();
                 let v = rng.gen();
                 if !ctrl.iter().any(|x| x == &(k, v)) {
@@ -943,7 +944,7 @@ mod test_treemap {
                 }
             }
 
-            for 30.times {
+            do 30.times {
                 let r = rng.gen_uint_range(0, ctrl.len());
                 let (key, _) = ctrl.remove(r);
                 assert!(map.remove(&key));
@@ -1001,11 +1002,12 @@ mod test_treemap {
         assert!(m.insert(1, 2));
 
         let mut n = 4;
-        for m.each_reverse |k, v| {
+        do m.each_reverse |k, v| {
             assert_eq!(*k, n);
             assert_eq!(*v, n * 2);
             n -= 1;
-        }
+            true
+        };
     }
 
     #[test]
@@ -1277,10 +1279,11 @@ mod test_set {
         assert!(m.insert(1));
 
         let mut n = 4;
-        for m.each_reverse |x| {
+        do m.each_reverse |x| {
             assert_eq!(*x, n);
-            n -= 1
-        }
+            n -= 1;
+            true
+        };
     }
 
     fn check(a: &[int], b: &[int], expected: &[int],
@@ -1292,10 +1295,11 @@ mod test_set {
         foreach y in b.iter() { assert!(set_b.insert(*y)) }
 
         let mut i = 0;
-        for f(&set_a, &set_b) |x| {
+        do f(&set_a, &set_b) |x| {
             assert_eq!(*x, expected[i]);
             i += 1;
-        }
+            true
+        };
         assert_eq!(i, expected.len());
     }