dlist: handle iter early break properly

This commit is contained in:
Ben Blum 2012-07-14 00:23:02 -04:00
parent 1ffc0720bb
commit e993b838ca
2 changed files with 11 additions and 1 deletions

View File

@ -441,6 +441,16 @@ mod tests {
assert iter::foldl(l, 0, |accum,elem| accum+elem) == 5050; assert iter::foldl(l, 0, |accum,elem| accum+elem) == 5050;
} }
#[test] #[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() { fn test_dlist_remove_head() {
let l = create::<int>(); let l = create::<int>();
l.assert_consistent(); let one = l.push_n(1); l.assert_consistent(); let one = l.push_n(1);

View File

@ -18,7 +18,7 @@ fn EACH<A>(self: IMPL_T<A>, f: fn(A) -> bool) {
!box::ptr_eq(*option::get(nobe.root), *self) { !box::ptr_eq(*option::get(nobe.root), *self) {
fail "Iteration encountered a dlist node not on this dlist." 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. // Check that the user didn't do a remove.
// Note that this makes it ok for the user to remove the node and then // 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. // immediately put it back in a different position. I allow this.