From dbcb74e247b892a5174524bbbafbe93c51c53f65 Mon Sep 17 00:00:00 2001 From: blake2-ppc Date: Wed, 31 Jul 2013 21:07:44 +0200 Subject: [PATCH] extra: Replace `for` with `do { .. }` expr where internal iterators are used --- src/libextra/arena.rs | 5 +++-- src/libextra/bitv.rs | 45 +++++++++++++++++++++++------------------ src/libextra/getopts.rs | 7 ++++--- src/libextra/list.rs | 9 +++++---- src/libextra/treemap.rs | 17 +++++++++------- 5 files changed, 47 insertions(+), 36 deletions(-) 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/bitv.rs b/src/libextra/bitv.rs index 4d2d5635eff..c2ea2dee82c 100644 --- a/src/libextra/bitv.rs +++ b/src/libextra/bitv.rs @@ -646,9 +646,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 }, @@ -1354,18 +1355,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 +1401,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 +1425,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 +1451,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 +1480,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/getopts.rs b/src/libextra/getopts.rs index b0e6f82322b..e50693236fd 100644 --- a/src/libextra/getopts.rs +++ b/src/libextra/getopts.rs @@ -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/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(ls: @List, f: &fn(&T) -> bool) -> Option { /// Returns true if a list contains an element with the given value pub fn has(ls: @List, 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/treemap.rs b/src/libextra/treemap.rs index 50ad5c77fba..4e66870a947 100644 --- a/src/libextra/treemap.rs +++ b/src/libextra/treemap.rs @@ -1001,11 +1001,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 +1278,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 +1294,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()); }