about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorBen Blum <bblum@andrew.cmu.edu>2012-07-14 00:23:02 -0400
committerBen Blum <bblum@andrew.cmu.edu>2012-07-14 00:56:07 -0400
commite993b838cadcef9945e0471689c6efae53d545a1 (patch)
tree41aa164a3e1155337570e99406dd3bf3680dd70a /src
parent1ffc0720bb166f4d7d4cf6299ba237f8c0c95527 (diff)
dlist: handle iter early break properly
Diffstat (limited to 'src')
-rw-r--r--src/libcore/dlist.rs10
-rw-r--r--src/libcore/iter-trait/dlist.rs2
2 files changed, 11 insertions, 1 deletions
diff --git a/src/libcore/dlist.rs b/src/libcore/dlist.rs
index c66f094eec0..06548e632b9 100644
--- a/src/libcore/dlist.rs
+++ b/src/libcore/dlist.rs
@@ -441,6 +441,16 @@ mod tests {
         assert iter::foldl(l, 0, |accum,elem| accum+elem) == 5050;
     }
     #[test]
+    fn test_dlist_break_early() {
+        let l = from_vec(~[1,2,3,4,5]);
+        let mut x = 0;
+        for l.each |i| {
+            x += 1;
+            if (i == 3) { break; }
+        }
+        assert x == 3;
+    }
+    #[test]
     fn test_dlist_remove_head() {
         let l = create::<int>();
         l.assert_consistent(); let one = l.push_n(1);
diff --git a/src/libcore/iter-trait/dlist.rs b/src/libcore/iter-trait/dlist.rs
index d34ea38034d..b1a822a5bad 100644
--- a/src/libcore/iter-trait/dlist.rs
+++ b/src/libcore/iter-trait/dlist.rs
@@ -18,7 +18,7 @@ fn EACH<A>(self: IMPL_T<A>, f: fn(A) -> bool) {
            !box::ptr_eq(*option::get(nobe.root), *self) {
             fail "Iteration encountered a dlist node not on this dlist."
         }
-        f(nobe.data);
+        if !f(nobe.data) { break; }
         // Check that the user didn't do a remove.
         // Note that this makes it ok for the user to remove the node and then
         // immediately put it back in a different position. I allow this.