mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
std: Replace for
with do { .. }
expr where internal iterators are used
This commit is contained in:
parent
02bdf90cf6
commit
b18bd785ec
@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -94,27 +94,29 @@ pub unsafe fn annihilate() {
|
||||
//
|
||||
// In this pass, nothing gets freed, so it does not matter whether
|
||||
// we read the next field before or after the callback.
|
||||
for each_live_alloc(true) |box, uniq| {
|
||||
do each_live_alloc(true) |box, uniq| {
|
||||
stats.n_total_boxes += 1;
|
||||
if uniq {
|
||||
stats.n_unique_boxes += 1;
|
||||
} else {
|
||||
(*box).ref_count = managed::RC_IMMORTAL;
|
||||
}
|
||||
}
|
||||
true
|
||||
};
|
||||
|
||||
// Pass 2: Drop all boxes.
|
||||
//
|
||||
// In this pass, unique-managed boxes may get freed, but not
|
||||
// managed boxes, so we must read the `next` field *after* the
|
||||
// callback, as the original value may have been freed.
|
||||
for each_live_alloc(false) |box, uniq| {
|
||||
do each_live_alloc(false) |box, uniq| {
|
||||
if !uniq {
|
||||
let tydesc = (*box).type_desc;
|
||||
let data = &(*box).data as *();
|
||||
((*tydesc).drop_glue)(data as *i8);
|
||||
}
|
||||
}
|
||||
true
|
||||
};
|
||||
|
||||
// Pass 3: Free all boxes.
|
||||
//
|
||||
@ -122,14 +124,15 @@ pub unsafe fn annihilate() {
|
||||
// unique-managed boxes, though I think that none of those are
|
||||
// left), so we must read the `next` field before, since it will
|
||||
// not be valid after.
|
||||
for each_live_alloc(true) |box, uniq| {
|
||||
do each_live_alloc(true) |box, uniq| {
|
||||
if !uniq {
|
||||
stats.n_bytes_freed +=
|
||||
(*((*box).type_desc)).size
|
||||
+ sys::size_of::<raw::Box<()>>();
|
||||
local_free(box as *i8);
|
||||
}
|
||||
}
|
||||
true
|
||||
};
|
||||
|
||||
if debug_mem() {
|
||||
// We do logging here w/o allocation.
|
||||
|
@ -130,15 +130,17 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
|
||||
hash: uint,
|
||||
k: &K)
|
||||
-> SearchResult {
|
||||
for self.bucket_sequence(hash) |i| {
|
||||
let mut ret = TableFull;
|
||||
do self.bucket_sequence(hash) |i| {
|
||||
match self.buckets[i] {
|
||||
Some(ref bkt) => if bkt.hash == hash && *k == bkt.key {
|
||||
return FoundEntry(i);
|
||||
Some(ref bkt) if bkt.hash == hash && *k == bkt.key => {
|
||||
ret = FoundEntry(i); false
|
||||
},
|
||||
None => return FoundHole(i)
|
||||
None => { ret = FoundHole(i); false }
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
TableFull
|
||||
};
|
||||
ret
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -146,17 +148,17 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
|
||||
hash: uint,
|
||||
k: &Q)
|
||||
-> SearchResult {
|
||||
for self.bucket_sequence(hash) |i| {
|
||||
let mut ret = TableFull;
|
||||
do self.bucket_sequence(hash) |i| {
|
||||
match self.buckets[i] {
|
||||
Some(ref bkt) => {
|
||||
if bkt.hash == hash && k.equiv(&bkt.key) {
|
||||
return FoundEntry(i);
|
||||
}
|
||||
Some(ref bkt) if bkt.hash == hash && k.equiv(&bkt.key) => {
|
||||
ret = FoundEntry(i); false
|
||||
},
|
||||
None => return FoundHole(i)
|
||||
None => { ret = FoundHole(i); false }
|
||||
_ => true,
|
||||
}
|
||||
}
|
||||
TableFull
|
||||
};
|
||||
ret
|
||||
}
|
||||
|
||||
/// Expand the capacity of the array to the next power of two
|
||||
@ -272,11 +274,6 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
|
||||
|
||||
value
|
||||
}
|
||||
|
||||
fn search(&self, hash: uint,
|
||||
op: &fn(x: &Option<Bucket<K, V>>) -> bool) {
|
||||
let _ = self.bucket_sequence(hash, |i| op(&self.buckets[i]));
|
||||
}
|
||||
}
|
||||
|
||||
impl<K:Hash + Eq,V> Container for HashMap<K, V> {
|
||||
|
@ -770,9 +770,10 @@ impl<T:Reader> ReaderUtil for T {
|
||||
|
||||
fn read_lines(&self) -> ~[~str] {
|
||||
do vec::build |push| {
|
||||
for self.each_line |line| {
|
||||
do self.each_line |line| {
|
||||
push(line.to_owned());
|
||||
}
|
||||
true
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -1880,16 +1881,16 @@ mod tests {
|
||||
|
||||
{
|
||||
let file = io::file_reader(&path).unwrap();
|
||||
for file.each_byte() |_| {
|
||||
fail!("must be empty");
|
||||
}
|
||||
do file.each_byte() |_| {
|
||||
fail!("must be empty")
|
||||
};
|
||||
}
|
||||
|
||||
{
|
||||
let file = io::file_reader(&path).unwrap();
|
||||
for file.each_char() |_| {
|
||||
fail!("must be empty");
|
||||
}
|
||||
do file.each_char() |_| {
|
||||
fail!("must be empty")
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -790,7 +790,7 @@ pub fn list_dir_path(p: &Path) -> ~[Path] {
|
||||
/// all its contents. Use carefully!
|
||||
pub fn remove_dir_recursive(p: &Path) -> bool {
|
||||
let mut error_happened = false;
|
||||
for walk_dir(p) |inner| {
|
||||
do walk_dir(p) |inner| {
|
||||
if !error_happened {
|
||||
if path_is_dir(inner) {
|
||||
if !remove_dir_recursive(inner) {
|
||||
@ -803,6 +803,7 @@ pub fn remove_dir_recursive(p: &Path) -> bool {
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
};
|
||||
// Directory should now be empty
|
||||
!error_happened && remove_dir(p)
|
||||
|
@ -372,8 +372,9 @@ impl Drop for Taskgroup {
|
||||
// with our own taskgroup, so long as both happen before we die.
|
||||
// We remove ourself from every ancestor we can, so no cleanup; no
|
||||
// break.
|
||||
for each_ancestor(&mut this.ancestors, |_| {}) |ancestor_group| {
|
||||
do each_ancestor(&mut this.ancestors, |_| {}) |ancestor_group| {
|
||||
leave_taskgroup(ancestor_group, &me, false);
|
||||
true
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -459,11 +459,12 @@ mod test_map {
|
||||
assert!(m.insert(1, 2));
|
||||
|
||||
let mut n = 0;
|
||||
for m.each |k, v| {
|
||||
do m.each |k, v| {
|
||||
assert_eq!(*k, n);
|
||||
assert_eq!(*v, n * 2);
|
||||
n += 1;
|
||||
}
|
||||
true
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -475,14 +476,16 @@ mod test_map {
|
||||
}
|
||||
|
||||
let mut n = uint::max_value - 10000;
|
||||
for m.each |k, v| {
|
||||
if n == uint::max_value - 5000 { break }
|
||||
assert!(n < uint::max_value - 5000);
|
||||
do m.each |k, v| {
|
||||
if n == uint::max_value - 5000 { false } else {
|
||||
assert!(n < uint::max_value - 5000);
|
||||
|
||||
assert_eq!(*k, n);
|
||||
assert_eq!(*v, n / 2);
|
||||
n += 1;
|
||||
}
|
||||
assert_eq!(*k, n);
|
||||
assert_eq!(*v, n / 2);
|
||||
n += 1;
|
||||
true
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -496,11 +499,12 @@ mod test_map {
|
||||
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]
|
||||
@ -512,14 +516,16 @@ mod test_map {
|
||||
}
|
||||
|
||||
let mut n = uint::max_value - 1;
|
||||
for m.each_reverse |k, v| {
|
||||
if n == uint::max_value - 5000 { break }
|
||||
assert!(n > uint::max_value - 5000);
|
||||
do m.each_reverse |k, v| {
|
||||
if n == uint::max_value - 5000 { false } else {
|
||||
assert!(n > uint::max_value - 5000);
|
||||
|
||||
assert_eq!(*k, n);
|
||||
assert_eq!(*v, n / 2);
|
||||
n -= 1;
|
||||
}
|
||||
assert_eq!(*k, n);
|
||||
assert_eq!(*v, n / 2);
|
||||
n -= 1;
|
||||
true
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -572,10 +578,11 @@ mod test_set {
|
||||
|
||||
let mut i = 0;
|
||||
|
||||
for trie.each |x| {
|
||||
do trie.each |x| {
|
||||
assert_eq!(expected[i], *x);
|
||||
i += 1;
|
||||
}
|
||||
true
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -2767,19 +2767,19 @@ mod tests {
|
||||
let mut results: ~[~[int]];
|
||||
|
||||
results = ~[];
|
||||
for each_permutation([]) |v| { results.push(v.to_owned()); }
|
||||
do each_permutation([]) |v| { results.push(v.to_owned()); true };
|
||||
assert_eq!(results, ~[~[]]);
|
||||
|
||||
results = ~[];
|
||||
for each_permutation([7]) |v| { results.push(v.to_owned()); }
|
||||
do each_permutation([7]) |v| { results.push(v.to_owned()); true };
|
||||
assert_eq!(results, ~[~[7]]);
|
||||
|
||||
results = ~[];
|
||||
for each_permutation([1,1]) |v| { results.push(v.to_owned()); }
|
||||
do each_permutation([1,1]) |v| { results.push(v.to_owned()); true };
|
||||
assert_eq!(results, ~[~[1,1],~[1,1]]);
|
||||
|
||||
results = ~[];
|
||||
for each_permutation([5,2,0]) |v| { results.push(v.to_owned()); }
|
||||
do each_permutation([5,2,0]) |v| { results.push(v.to_owned()); true };
|
||||
assert!(results ==
|
||||
~[~[5,2,0],~[5,0,2],~[2,5,0],~[2,0,5],~[0,5,2],~[0,2,5]]);
|
||||
}
|
||||
@ -3107,12 +3107,13 @@ mod tests {
|
||||
fn test_permute_fail() {
|
||||
let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
|
||||
let mut i = 0;
|
||||
for each_permutation(v) |_elt| {
|
||||
do each_permutation(v) |_elt| {
|
||||
if i == 2 {
|
||||
fail!()
|
||||
}
|
||||
i += 0;
|
||||
}
|
||||
true
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -3425,9 +3426,10 @@ mod tests {
|
||||
fn test_permutations0() {
|
||||
let values = [];
|
||||
let mut v : ~[~[int]] = ~[];
|
||||
for each_permutation(values) |p| {
|
||||
do each_permutation(values) |p| {
|
||||
v.push(p.to_owned());
|
||||
}
|
||||
true
|
||||
};
|
||||
assert_eq!(v, ~[~[]]);
|
||||
}
|
||||
|
||||
@ -3435,9 +3437,10 @@ mod tests {
|
||||
fn test_permutations1() {
|
||||
let values = [1];
|
||||
let mut v : ~[~[int]] = ~[];
|
||||
for each_permutation(values) |p| {
|
||||
do each_permutation(values) |p| {
|
||||
v.push(p.to_owned());
|
||||
}
|
||||
true
|
||||
};
|
||||
assert_eq!(v, ~[~[1]]);
|
||||
}
|
||||
|
||||
@ -3445,9 +3448,10 @@ mod tests {
|
||||
fn test_permutations2() {
|
||||
let values = [1,2];
|
||||
let mut v : ~[~[int]] = ~[];
|
||||
for each_permutation(values) |p| {
|
||||
do each_permutation(values) |p| {
|
||||
v.push(p.to_owned());
|
||||
}
|
||||
true
|
||||
};
|
||||
assert_eq!(v, ~[~[1,2],~[2,1]]);
|
||||
}
|
||||
|
||||
@ -3455,9 +3459,10 @@ mod tests {
|
||||
fn test_permutations3() {
|
||||
let values = [1,2,3];
|
||||
let mut v : ~[~[int]] = ~[];
|
||||
for each_permutation(values) |p| {
|
||||
do each_permutation(values) |p| {
|
||||
v.push(p.to_owned());
|
||||
}
|
||||
true
|
||||
};
|
||||
assert_eq!(v, ~[~[1,2,3],~[1,3,2],~[2,1,3],~[2,3,1],~[3,1,2],~[3,2,1]]);
|
||||
}
|
||||
|
||||
|
@ -65,13 +65,14 @@ fn square_from_char(c: char) -> square {
|
||||
fn read_board_grid<rdr:'static + io::Reader>(input: rdr) -> ~[~[square]] {
|
||||
let input = @input as @io::Reader;
|
||||
let mut grid = ~[];
|
||||
for input.each_line |line| {
|
||||
do input.each_line |line| {
|
||||
let mut row = ~[];
|
||||
foreach c in line.iter() {
|
||||
row.push(square_from_char(c))
|
||||
}
|
||||
grid.push(row)
|
||||
}
|
||||
grid.push(row);
|
||||
true
|
||||
};
|
||||
let width = grid[0].len();
|
||||
foreach row in grid.iter() { assert!(row.len() == width) }
|
||||
grid
|
||||
|
Loading…
Reference in New Issue
Block a user