1
Fork 0

test: Remove all remaining non-procedure uses of do.

This commit is contained in:
Patrick Walton 2013-11-21 19:20:48 -08:00
parent f571e46ddb
commit 38efa17bb8
34 changed files with 385 additions and 392 deletions

View file

@ -2703,22 +2703,16 @@ A `loop` expression is only permitted in the body of a loop.
do_expr : "do" expr [ '|' ident_list '|' ] ? '{' block '}' ; do_expr : "do" expr [ '|' ident_list '|' ] ? '{' block '}' ;
~~~~ ~~~~
A _do expression_ provides a more-familiar block-syntax for a [lambda expression](#lambda-expressions), A _do expression_ provides a more-familiar block syntax
including a special translation of [return expressions](#return-expressions) inside the supplied block. for invoking a function and passing it a newly-created a procedure.
Any occurrence of a [return expression](#return-expressions) The optional `ident_list` and `block` provided in a `do` expression are parsed
inside this `block` expression is rewritten as though they constitute a procedure expression;
as a reference to an (anonymous) flag set in the caller's environment,
which is checked on return from the `expr` and, if set,
causes a corresponding return from the caller.
In this way, the meaning of `return` statements in language built-in control blocks is preserved,
if they are rewritten using lambda functions and `do` expressions as abstractions.
The optional `ident_list` and `block` provided in a `do` expression are parsed as though they constitute a lambda expression;
if the `ident_list` is missing, an empty `ident_list` is implied. if the `ident_list` is missing, an empty `ident_list` is implied.
The lambda expression is then provided as a _trailing argument_ The procedure expression is then provided as a _trailing argument_
to the outermost [call](#call-expressions) or [method call](#method-call-expressions) expression to the outermost [call](#call-expressions) or
[method call](#method-call-expressions) expression
in the `expr` following `do`. in the `expr` following `do`.
If the `expr` is a [path expression](#path-expressions), it is parsed as though it is a call expression. If the `expr` is a [path expression](#path-expressions), it is parsed as though it is a call expression.
If the `expr` is a [field expression](#field-expressions), it is parsed as though it is a method call expression. If the `expr` is a [field expression](#field-expressions), it is parsed as though it is a method call expression.
@ -2726,10 +2720,10 @@ If the `expr` is a [field expression](#field-expressions), it is parsed as thoug
In this example, both calls to `f` are equivalent: In this example, both calls to `f` are equivalent:
~~~~ ~~~~
# fn f(f: |int|) { } # fn f(f: proc(int)) { }
# fn g(i: int) { } # fn g(i: int) { }
f(|j| g(j)); f(proc(j) { g(j) });
do f |j| { do f |j| {
g(j); g(j);
@ -2739,10 +2733,10 @@ do f |j| {
In this example, both calls to the (binary) function `k` are equivalent: In this example, both calls to the (binary) function `k` are equivalent:
~~~~ ~~~~
# fn k(x:int, f: |int|) { } # fn k(x:int, f: proc(int)) { }
# fn l(i: int) { } # fn l(i: int) { }
k(3, |j| l(j)); k(3, proc(j) { l(j) });
do k(3) |j| { do k(3) |j| {
l(j); l(j);

View file

@ -457,7 +457,7 @@ condition! {
fn main() { fn main() {
// Trap the condition: // Trap the condition:
do malformed_line::cond.trap(|_| (-1,-1)).inside { malformed_line::cond.trap(|_| (-1,-1)).inside(|| {
// The protected logic. // The protected logic.
let pairs = read_int_pairs(); let pairs = read_int_pairs();
@ -465,7 +465,7 @@ fn main() {
println!("{:4.4d}, {:4.4d}", a, b); println!("{:4.4d}, {:4.4d}", a, b);
} }
} })
} }
fn read_int_pairs() -> ~[(int,int)] { fn read_int_pairs() -> ~[(int,int)] {
@ -535,7 +535,7 @@ condition! {
fn main() { fn main() {
// Trap the condition and return `None` // Trap the condition and return `None`
do malformed_line::cond.trap(|_| None).inside { malformed_line::cond.trap(|_| None).inside(|| {
// The protected logic. // The protected logic.
let pairs = read_int_pairs(); let pairs = read_int_pairs();
@ -543,7 +543,7 @@ fn main() {
println!("{:4.4d}, {:4.4d}", a, b); println!("{:4.4d}, {:4.4d}", a, b);
} }
} })
} }
fn read_int_pairs() -> ~[(int,int)] { fn read_int_pairs() -> ~[(int,int)] {
@ -631,7 +631,7 @@ condition! {
fn main() { fn main() {
// Trap the condition and return `UsePreviousLine` // Trap the condition and return `UsePreviousLine`
do malformed_line::cond.trap(|_| UsePreviousLine).inside { malformed_line::cond.trap(|_| UsePreviousLine).inside(|| {
// The protected logic. // The protected logic.
let pairs = read_int_pairs(); let pairs = read_int_pairs();
@ -639,7 +639,7 @@ fn main() {
println!("{:4.4d}, {:4.4d}", a, b); println!("{:4.4d}, {:4.4d}", a, b);
} }
} })
} }
fn read_int_pairs() -> ~[(int,int)] { fn read_int_pairs() -> ~[(int,int)] {
@ -758,10 +758,10 @@ condition! {
fn main() { fn main() {
// Trap the `malformed_int` condition and return -1 // Trap the `malformed_int` condition and return -1
do malformed_int::cond.trap(|_| -1).inside { malformed_int::cond.trap(|_| -1).inside(|| {
// Trap the `malformed_line` condition and return `UsePreviousLine` // Trap the `malformed_line` condition and return `UsePreviousLine`
do malformed_line::cond.trap(|_| UsePreviousLine).inside { malformed_line::cond.trap(|_| UsePreviousLine).inside(|| {
// The protected logic. // The protected logic.
let pairs = read_int_pairs(); let pairs = read_int_pairs();
@ -769,8 +769,8 @@ fn main() {
println!("{:4.4d}, {:4.4d}", a, b); println!("{:4.4d}, {:4.4d}", a, b);
} }
} })
} })
} }
// Parse an int; if parsing fails, call the condition handler and // Parse an int; if parsing fails, call the condition handler and

View file

@ -253,13 +253,13 @@ might look like the example below.
# use std::vec; # use std::vec;
// Create a vector of ports, one for each child task // Create a vector of ports, one for each child task
let ports = do vec::from_fn(3) |init_val| { let ports = vec::from_fn(3, |init_val| {
let (port, chan) = stream(); let (port, chan) = stream();
do spawn { do spawn {
chan.send(some_expensive_computation(init_val)); chan.send(some_expensive_computation(init_val));
} }
port port
}; });
// Wait on each port, accumulating the results // Wait on each port, accumulating the results
let result = ports.iter().fold(0, |accum, port| accum + port.recv() ); let result = ports.iter().fold(0, |accum, port| accum + port.recv() );

View file

@ -1439,19 +1439,14 @@ call_twice(function);
## Do syntax ## Do syntax
The `do` expression provides a way to treat higher-order functions The `do` expression makes it easier to call functions that take procedures
(functions that take closures as arguments) as control structures. as arguments.
Consider this function that iterates over a vector of Consider this function that takes a procedure:
integers, passing in a pointer to each integer in the vector:
~~~~ ~~~~
fn each(v: &[int], op: |v: &int|) { fn call_it(op: proc(v: int)) {
let mut n = 0; op(10)
while n < v.len() {
op(&v[n]);
n += 1;
}
} }
~~~~ ~~~~
@ -1460,26 +1455,24 @@ argument, we can write it in a way that has a pleasant, block-like
structure. structure.
~~~~ ~~~~
# fn each(v: &[int], op: |v: &int|) { } # fn call_it(op: proc(v: int)) { }
# fn do_some_work(i: &int) { } call_it(proc(n) {
each([1, 2, 3], |n| { println(n.to_str());
do_some_work(n);
}); });
~~~~ ~~~~
This is such a useful pattern that Rust has a special form of function This is such a useful pattern that Rust has a special form of function
call that can be written more like a built-in control structure: call for these functions.
~~~~ ~~~~
# fn each(v: &[int], op: |v: &int|) { } # fn call_it(op: proc(v: int)) { }
# fn do_some_work(i: &int) { } do call_it() |n| {
do each([1, 2, 3]) |n| { println(n.to_str());
do_some_work(n);
} }
~~~~ ~~~~
The call is prefixed with the keyword `do` and, instead of writing the The call is prefixed with the keyword `do` and, instead of writing the
final closure inside the argument list, it appears outside of the final procedure inside the argument list, it appears outside of the
parentheses, where it looks more like a typical block of parentheses, where it looks more like a typical block of
code. code.

View file

@ -297,14 +297,14 @@ fn test_arena_destructors_fail() {
for i in range(0u, 10) { for i in range(0u, 10) {
// Arena allocate something with drop glue to make sure it // Arena allocate something with drop glue to make sure it
// doesn't leak. // doesn't leak.
do arena.alloc { @i }; arena.alloc(|| { @i });
// Allocate something with funny size and alignment, to keep // Allocate something with funny size and alignment, to keep
// things interesting. // things interesting.
do arena.alloc { [0u8, 1u8, 2u8] }; arena.alloc(|| { [0u8, 1u8, 2u8] });
} }
// Now, fail while allocating // Now, fail while allocating
do arena.alloc::<@int> { arena.alloc::<@int>(|| {
// Now fail. // Now fail.
fail!(); fail!();
}; });
} }

View file

@ -317,20 +317,20 @@ mod test {
use std::rand::{task_rng, random, Rng}; use std::rand::{task_rng, random, Rng};
use std::vec; use std::vec;
do 1000.times { 1000.times(|| {
let times = task_rng().gen_range(1u, 100); let times = task_rng().gen_range(1u, 100);
let v = vec::from_fn(times, |_| random::<u8>()); let v = vec::from_fn(times, |_| random::<u8>());
assert_eq!(v.to_base64(STANDARD).from_base64().unwrap(), v); assert_eq!(v.to_base64(STANDARD).from_base64().unwrap(), v);
} })
} }
#[bench] #[bench]
pub fn bench_to_base64(bh: & mut BenchHarness) { pub fn bench_to_base64(bh: & mut BenchHarness) {
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \ let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
"; ";
do bh.iter { bh.iter(|| {
s.as_bytes().to_base64(STANDARD); s.as_bytes().to_base64(STANDARD);
} });
bh.bytes = s.len() as u64; bh.bytes = s.len() as u64;
} }
@ -339,9 +339,9 @@ mod test {
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \ let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
"; ";
let b = s.as_bytes().to_base64(STANDARD); let b = s.as_bytes().to_base64(STANDARD);
do bh.iter { bh.iter(|| {
b.from_base64(); b.from_base64();
} });
bh.bytes = b.len() as u64; bh.bytes = b.len() as u64;
} }

View file

@ -1362,18 +1362,18 @@ mod tests {
fn test_small_clear() { fn test_small_clear() {
let mut b = Bitv::new(14, true); let mut b = Bitv::new(14, true);
b.clear(); b.clear();
do b.ones |i| { b.ones(|i| {
fail!("found 1 at {:?}", i) fail!("found 1 at {:?}", i)
}; });
} }
#[test] #[test]
fn test_big_clear() { fn test_big_clear() {
let mut b = Bitv::new(140, true); let mut b = Bitv::new(140, true);
b.clear(); b.clear();
do b.ones |i| { b.ones(|i| {
fail!("found 1 at {:?}", i) fail!("found 1 at {:?}", i)
}; });
} }
#[test] #[test]
@ -1408,11 +1408,11 @@ mod tests {
let mut i = 0; let mut i = 0;
let expected = [3, 5, 11, 77]; let expected = [3, 5, 11, 77];
do a.intersection(&b) |x| { a.intersection(&b, |x| {
assert_eq!(*x, expected[i]); assert_eq!(*x, expected[i]);
i += 1; i += 1;
true true
}; });
assert_eq!(i, expected.len()); assert_eq!(i, expected.len());
} }
@ -1432,11 +1432,11 @@ mod tests {
let mut i = 0; let mut i = 0;
let expected = [1, 5, 500]; let expected = [1, 5, 500];
do a.difference(&b) |x| { a.difference(&b, |x| {
assert_eq!(*x, expected[i]); assert_eq!(*x, expected[i]);
i += 1; i += 1;
true true
}; });
assert_eq!(i, expected.len()); assert_eq!(i, expected.len());
} }
@ -1458,11 +1458,11 @@ mod tests {
let mut i = 0; let mut i = 0;
let expected = [1, 5, 11, 14, 220]; let expected = [1, 5, 11, 14, 220];
do a.symmetric_difference(&b) |x| { a.symmetric_difference(&b, |x| {
assert_eq!(*x, expected[i]); assert_eq!(*x, expected[i]);
i += 1; i += 1;
true true
}; });
assert_eq!(i, expected.len()); assert_eq!(i, expected.len());
} }
@ -1487,11 +1487,11 @@ mod tests {
let mut i = 0; let mut i = 0;
let expected = [1, 3, 5, 9, 11, 13, 19, 24, 160]; let expected = [1, 3, 5, 9, 11, 13, 19, 24, 160];
do a.union(&b) |x| { a.union(&b, |x| {
assert_eq!(*x, expected[i]); assert_eq!(*x, expected[i]);
i += 1; i += 1;
true true
}; });
assert_eq!(i, expected.len()); assert_eq!(i, expected.len());
} }
@ -1538,27 +1538,27 @@ mod tests {
fn bench_uint_small(b: &mut BenchHarness) { fn bench_uint_small(b: &mut BenchHarness) {
let mut r = rng(); let mut r = rng();
let mut bitv = 0 as uint; let mut bitv = 0 as uint;
do b.iter { b.iter(|| {
bitv |= (1 << ((r.next_u32() as uint) % uint::bits)); bitv |= (1 << ((r.next_u32() as uint) % uint::bits));
} })
} }
#[bench] #[bench]
fn bench_small_bitv_small(b: &mut BenchHarness) { fn bench_small_bitv_small(b: &mut BenchHarness) {
let mut r = rng(); let mut r = rng();
let mut bitv = SmallBitv::new(uint::bits); let mut bitv = SmallBitv::new(uint::bits);
do b.iter { b.iter(|| {
bitv.set((r.next_u32() as uint) % uint::bits, true); bitv.set((r.next_u32() as uint) % uint::bits, true);
} })
} }
#[bench] #[bench]
fn bench_big_bitv_small(b: &mut BenchHarness) { fn bench_big_bitv_small(b: &mut BenchHarness) {
let mut r = rng(); let mut r = rng();
let mut bitv = BigBitv::new(~[0]); let mut bitv = BigBitv::new(~[0]);
do b.iter { b.iter(|| {
bitv.set((r.next_u32() as uint) % uint::bits, true); bitv.set((r.next_u32() as uint) % uint::bits, true);
} })
} }
#[bench] #[bench]
@ -1567,87 +1567,87 @@ mod tests {
let mut storage = ~[]; let mut storage = ~[];
storage.grow(BENCH_BITS / uint::bits, &0u); storage.grow(BENCH_BITS / uint::bits, &0u);
let mut bitv = BigBitv::new(storage); let mut bitv = BigBitv::new(storage);
do b.iter { b.iter(|| {
bitv.set((r.next_u32() as uint) % BENCH_BITS, true); bitv.set((r.next_u32() as uint) % BENCH_BITS, true);
} })
} }
#[bench] #[bench]
fn bench_bitv_big(b: &mut BenchHarness) { fn bench_bitv_big(b: &mut BenchHarness) {
let mut r = rng(); let mut r = rng();
let mut bitv = Bitv::new(BENCH_BITS, false); let mut bitv = Bitv::new(BENCH_BITS, false);
do b.iter { b.iter(|| {
bitv.set((r.next_u32() as uint) % BENCH_BITS, true); bitv.set((r.next_u32() as uint) % BENCH_BITS, true);
} })
} }
#[bench] #[bench]
fn bench_bitv_small(b: &mut BenchHarness) { fn bench_bitv_small(b: &mut BenchHarness) {
let mut r = rng(); let mut r = rng();
let mut bitv = Bitv::new(uint::bits, false); let mut bitv = Bitv::new(uint::bits, false);
do b.iter { b.iter(|| {
bitv.set((r.next_u32() as uint) % uint::bits, true); bitv.set((r.next_u32() as uint) % uint::bits, true);
} })
} }
#[bench] #[bench]
fn bench_bitv_set_small(b: &mut BenchHarness) { fn bench_bitv_set_small(b: &mut BenchHarness) {
let mut r = rng(); let mut r = rng();
let mut bitv = BitvSet::new(); let mut bitv = BitvSet::new();
do b.iter { b.iter(|| {
bitv.insert((r.next_u32() as uint) % uint::bits); bitv.insert((r.next_u32() as uint) % uint::bits);
} })
} }
#[bench] #[bench]
fn bench_bitv_set_big(b: &mut BenchHarness) { fn bench_bitv_set_big(b: &mut BenchHarness) {
let mut r = rng(); let mut r = rng();
let mut bitv = BitvSet::new(); let mut bitv = BitvSet::new();
do b.iter { b.iter(|| {
bitv.insert((r.next_u32() as uint) % BENCH_BITS); bitv.insert((r.next_u32() as uint) % BENCH_BITS);
} })
} }
#[bench] #[bench]
fn bench_bitv_big_union(b: &mut BenchHarness) { fn bench_bitv_big_union(b: &mut BenchHarness) {
let mut b1 = Bitv::new(BENCH_BITS, false); let mut b1 = Bitv::new(BENCH_BITS, false);
let b2 = Bitv::new(BENCH_BITS, false); let b2 = Bitv::new(BENCH_BITS, false);
do b.iter { b.iter(|| {
b1.union(&b2); b1.union(&b2);
} })
} }
#[bench] #[bench]
fn bench_btv_small_iter(b: &mut BenchHarness) { fn bench_btv_small_iter(b: &mut BenchHarness) {
let bitv = Bitv::new(uint::bits, false); let bitv = Bitv::new(uint::bits, false);
do b.iter { b.iter(|| {
let mut _sum = 0; let mut _sum = 0;
for pres in bitv.iter() { for pres in bitv.iter() {
_sum += pres as uint; _sum += pres as uint;
} }
} })
} }
#[bench] #[bench]
fn bench_bitv_big_iter(b: &mut BenchHarness) { fn bench_bitv_big_iter(b: &mut BenchHarness) {
let bitv = Bitv::new(BENCH_BITS, false); let bitv = Bitv::new(BENCH_BITS, false);
do b.iter { b.iter(|| {
let mut _sum = 0; let mut _sum = 0;
for pres in bitv.iter() { for pres in bitv.iter() {
_sum += pres as uint; _sum += pres as uint;
} }
} })
} }
#[bench] #[bench]
fn bench_bitvset_iter(b: &mut BenchHarness) { fn bench_bitvset_iter(b: &mut BenchHarness) {
let bitv = BitvSet::from_bitv(from_fn(BENCH_BITS, let bitv = BitvSet::from_bitv(from_fn(BENCH_BITS,
|idx| {idx % 3 == 0})); |idx| {idx % 3 == 0}));
do b.iter { b.iter(|| {
let mut _sum = 0; let mut _sum = 0;
for idx in bitv.iter() { for idx in bitv.iter() {
_sum += idx; _sum += idx;
} }
} })
} }
} }

View file

@ -167,9 +167,9 @@ mod test {
do run_in_uv_task { do run_in_uv_task {
let (port, chan) = rendezvous(); let (port, chan) = rendezvous();
do spawn { do spawn {
do 1000000.times { chan.send(()) } 1000000.times(|| { chan.send(()) })
} }
do 1000000.times { port.recv() } 1000000.times(|| { port.recv() })
} }
} }

View file

@ -58,11 +58,11 @@ pub mod bench {
} }
// measure // measure
do bh.iter { bh.iter(|| {
let k = rng.gen::<uint>() % n; let k = rng.gen::<uint>() % n;
map.insert(k, 1); map.insert(k, 1);
map.remove(&k); map.remove(&k);
} })
} }
pub fn insert_seq_n<M:MutableMap<uint,uint>>(n: uint, pub fn insert_seq_n<M:MutableMap<uint,uint>>(n: uint,
@ -76,11 +76,11 @@ pub mod bench {
// measure // measure
let mut i = 1; let mut i = 1;
do bh.iter { bh.iter(|| {
map.insert(i, 1); map.insert(i, 1);
map.remove(&i); map.remove(&i);
i = (i + 2) % n; i = (i + 2) % n;
} })
} }
pub fn find_rand_n<M:MutableMap<uint,uint>>(n: uint, pub fn find_rand_n<M:MutableMap<uint,uint>>(n: uint,
@ -98,10 +98,10 @@ pub mod bench {
// measure // measure
let mut i = 0; let mut i = 0;
do bh.iter { bh.iter(|| {
map.find(&(keys[i])); map.find(&(keys[i]));
i = (i + 1) % n; i = (i + 1) % n;
} })
} }
pub fn find_seq_n<M:MutableMap<uint,uint>>(n: uint, pub fn find_seq_n<M:MutableMap<uint,uint>>(n: uint,
@ -114,9 +114,9 @@ pub mod bench {
// measure // measure
let mut i = 0; let mut i = 0;
do bh.iter { bh.iter(|| {
map.find(&i); map.find(&i);
i = (i + 1) % n; i = (i + 1) % n;
} })
} }
} }

View file

@ -1031,11 +1031,11 @@ mod tests {
#[test] #[test]
fn test_fuzz() { fn test_fuzz() {
do 25.times { 25.times(|| {
fuzz_test(3); fuzz_test(3);
fuzz_test(16); fuzz_test(16);
fuzz_test(189); fuzz_test(189);
} })
} }
#[cfg(test)] #[cfg(test)]
@ -1078,43 +1078,43 @@ mod tests {
#[bench] #[bench]
fn bench_collect_into(b: &mut test::BenchHarness) { fn bench_collect_into(b: &mut test::BenchHarness) {
let v = &[0, ..64]; let v = &[0, ..64];
do b.iter { b.iter(|| {
let _: DList<int> = v.iter().map(|x| *x).collect(); let _: DList<int> = v.iter().map(|x| *x).collect();
} })
} }
#[bench] #[bench]
fn bench_push_front(b: &mut test::BenchHarness) { fn bench_push_front(b: &mut test::BenchHarness) {
let mut m: DList<int> = DList::new(); let mut m: DList<int> = DList::new();
do b.iter { b.iter(|| {
m.push_front(0); m.push_front(0);
} })
} }
#[bench] #[bench]
fn bench_push_back(b: &mut test::BenchHarness) { fn bench_push_back(b: &mut test::BenchHarness) {
let mut m: DList<int> = DList::new(); let mut m: DList<int> = DList::new();
do b.iter { b.iter(|| {
m.push_back(0); m.push_back(0);
} })
} }
#[bench] #[bench]
fn bench_push_back_pop_back(b: &mut test::BenchHarness) { fn bench_push_back_pop_back(b: &mut test::BenchHarness) {
let mut m: DList<int> = DList::new(); let mut m: DList<int> = DList::new();
do b.iter { b.iter(|| {
m.push_back(0); m.push_back(0);
m.pop_back(); m.pop_back();
} })
} }
#[bench] #[bench]
fn bench_push_front_pop_front(b: &mut test::BenchHarness) { fn bench_push_front_pop_front(b: &mut test::BenchHarness) {
let mut m: DList<int> = DList::new(); let mut m: DList<int> = DList::new();
do b.iter { b.iter(|| {
m.push_front(0); m.push_front(0);
m.pop_front(); m.pop_front();
} })
} }
#[bench] #[bench]
@ -1122,9 +1122,9 @@ mod tests {
let mut m: DList<int> = DList::new(); let mut m: DList<int> = DList::new();
m.push_front(0); m.push_front(0);
m.push_front(1); m.push_front(1);
do b.iter { b.iter(|| {
m.rotate_forward(); m.rotate_forward();
} })
} }
#[bench] #[bench]
@ -1132,41 +1132,41 @@ mod tests {
let mut m: DList<int> = DList::new(); let mut m: DList<int> = DList::new();
m.push_front(0); m.push_front(0);
m.push_front(1); m.push_front(1);
do b.iter { b.iter(|| {
m.rotate_backward(); m.rotate_backward();
} })
} }
#[bench] #[bench]
fn bench_iter(b: &mut test::BenchHarness) { fn bench_iter(b: &mut test::BenchHarness) {
let v = &[0, ..128]; let v = &[0, ..128];
let m: DList<int> = v.iter().map(|&x|x).collect(); let m: DList<int> = v.iter().map(|&x|x).collect();
do b.iter { b.iter(|| {
assert!(m.iter().len() == 128); assert!(m.iter().len() == 128);
} })
} }
#[bench] #[bench]
fn bench_iter_mut(b: &mut test::BenchHarness) { fn bench_iter_mut(b: &mut test::BenchHarness) {
let v = &[0, ..128]; let v = &[0, ..128];
let mut m: DList<int> = v.iter().map(|&x|x).collect(); let mut m: DList<int> = v.iter().map(|&x|x).collect();
do b.iter { b.iter(|| {
assert!(m.mut_iter().len() == 128); assert!(m.mut_iter().len() == 128);
} })
} }
#[bench] #[bench]
fn bench_iter_rev(b: &mut test::BenchHarness) { fn bench_iter_rev(b: &mut test::BenchHarness) {
let v = &[0, ..128]; let v = &[0, ..128];
let m: DList<int> = v.iter().map(|&x|x).collect(); let m: DList<int> = v.iter().map(|&x|x).collect();
do b.iter { b.iter(|| {
assert!(m.rev_iter().len() == 128); assert!(m.rev_iter().len() == 128);
} })
} }
#[bench] #[bench]
fn bench_iter_mut_rev(b: &mut test::BenchHarness) { fn bench_iter_mut_rev(b: &mut test::BenchHarness) {
let v = &[0, ..128]; let v = &[0, ..128];
let mut m: DList<int> = v.iter().map(|&x|x).collect(); let mut m: DList<int> = v.iter().map(|&x|x).collect();
do b.iter { b.iter(|| {
assert!(m.mut_rev_iter().len() == 128); assert!(m.mut_rev_iter().len() == 128);
} })
} }
} }

View file

@ -108,15 +108,15 @@ mod tests {
fn test_flate_round_trip() { fn test_flate_round_trip() {
let mut r = rand::rng(); let mut r = rand::rng();
let mut words = ~[]; let mut words = ~[];
do 20.times { 20.times(|| {
let range = r.gen_range(1u, 10); let range = r.gen_range(1u, 10);
words.push(r.gen_vec::<u8>(range)); words.push(r.gen_vec::<u8>(range));
} });
do 20.times { 20.times(|| {
let mut input = ~[]; let mut input = ~[];
do 2000.times { 2000.times(|| {
input.push_all(r.choose(words)); input.push_all(r.choose(words));
} });
debug!("de/inflate of {} bytes of random word-sequences", debug!("de/inflate of {} bytes of random word-sequences",
input.len()); input.len());
let cmp = deflate_bytes(input); let cmp = deflate_bytes(input);
@ -125,7 +125,7 @@ mod tests {
input.len(), cmp.len(), input.len(), cmp.len(),
100.0 * ((cmp.len() as f64) / (input.len() as f64))); 100.0 * ((cmp.len() as f64) / (input.len() as f64)));
assert_eq!(input, out); assert_eq!(input, out);
} });
} }
#[test] #[test]

View file

@ -174,9 +174,9 @@ mod tests {
pub fn bench_to_hex(bh: & mut BenchHarness) { pub fn bench_to_hex(bh: & mut BenchHarness) {
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \ let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
"; ";
do bh.iter { bh.iter(|| {
s.as_bytes().to_hex(); s.as_bytes().to_hex();
} });
bh.bytes = s.len() as u64; bh.bytes = s.len() as u64;
} }
@ -185,9 +185,9 @@ mod tests {
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \ let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
"; ";
let b = s.as_bytes().to_hex(); let b = s.as_bytes().to_hex();
do bh.iter { bh.iter(|| {
b.from_hex(); b.from_hex();
} });
bh.bytes = b.len() as u64; bh.bytes = b.len() as u64;
} }
} }

View file

@ -1521,33 +1521,33 @@ mod tests {
fn test_write_enum() { fn test_write_enum() {
let animal = Dog; let animal = Dog;
assert_eq!( assert_eq!(
do with_str_writer |wr| { with_str_writer(|wr| {
let mut encoder = Encoder(wr); let mut encoder = Encoder(wr);
animal.encode(&mut encoder); animal.encode(&mut encoder);
}, }),
~"\"Dog\"" ~"\"Dog\""
); );
assert_eq!( assert_eq!(
do with_str_writer |wr| { with_str_writer(|wr| {
let mut encoder = PrettyEncoder(wr); let mut encoder = PrettyEncoder(wr);
animal.encode(&mut encoder); animal.encode(&mut encoder);
}, }),
~"\"Dog\"" ~"\"Dog\""
); );
let animal = Frog(~"Henry", 349); let animal = Frog(~"Henry", 349);
assert_eq!( assert_eq!(
do with_str_writer |wr| { with_str_writer(|wr| {
let mut encoder = Encoder(wr); let mut encoder = Encoder(wr);
animal.encode(&mut encoder); animal.encode(&mut encoder);
}, }),
~"{\"variant\":\"Frog\",\"fields\":[\"Henry\",349]}" ~"{\"variant\":\"Frog\",\"fields\":[\"Henry\",349]}"
); );
assert_eq!( assert_eq!(
do with_str_writer |wr| { with_str_writer(|wr| {
let mut encoder = PrettyEncoder(wr); let mut encoder = PrettyEncoder(wr);
animal.encode(&mut encoder); animal.encode(&mut encoder);
}, }),
~"\ ~"\
[\n \ [\n \
\"Frog\",\n \ \"Frog\",\n \
@ -1560,33 +1560,33 @@ mod tests {
#[test] #[test]
fn test_write_some() { fn test_write_some() {
let value = Some(~"jodhpurs"); let value = Some(~"jodhpurs");
let s = do with_str_writer |wr| { let s = with_str_writer(|wr| {
let mut encoder = Encoder(wr); let mut encoder = Encoder(wr);
value.encode(&mut encoder); value.encode(&mut encoder);
}; });
assert_eq!(s, ~"\"jodhpurs\""); assert_eq!(s, ~"\"jodhpurs\"");
let value = Some(~"jodhpurs"); let value = Some(~"jodhpurs");
let s = do with_str_writer |wr| { let s = with_str_writer(|wr| {
let mut encoder = PrettyEncoder(wr); let mut encoder = PrettyEncoder(wr);
value.encode(&mut encoder); value.encode(&mut encoder);
}; });
assert_eq!(s, ~"\"jodhpurs\""); assert_eq!(s, ~"\"jodhpurs\"");
} }
#[test] #[test]
fn test_write_none() { fn test_write_none() {
let value: Option<~str> = None; let value: Option<~str> = None;
let s = do with_str_writer |wr| { let s = with_str_writer(|wr| {
let mut encoder = Encoder(wr); let mut encoder = Encoder(wr);
value.encode(&mut encoder); value.encode(&mut encoder);
}; });
assert_eq!(s, ~"null"); assert_eq!(s, ~"null");
let s = do with_str_writer |wr| { let s = with_str_writer(|wr| {
let mut encoder = Encoder(wr); let mut encoder = Encoder(wr);
value.encode(&mut encoder); value.encode(&mut encoder);
}; });
assert_eq!(s, ~"null"); assert_eq!(s, ~"null");
} }

View file

@ -2615,19 +2615,27 @@ mod bench {
#[bench] #[bench]
fn factorial_100(bh: &mut BenchHarness) { fn factorial_100(bh: &mut BenchHarness) {
bh.iter(|| factorial(100)); bh.iter(|| {
factorial(100);
});
} }
#[bench] #[bench]
fn fib_100(bh: &mut BenchHarness) { fn fib_100(bh: &mut BenchHarness) {
bh.iter(|| fib(100)); bh.iter(|| {
fib(100);
});
} }
#[bench] #[bench]
fn to_str(bh: &mut BenchHarness) { fn to_str(bh: &mut BenchHarness) {
let fac = factorial(100); let fac = factorial(100);
let fib = fib(100); let fib = fib(100);
bh.iter(|| fac.to_str()); bh.iter(|| {
bh.iter(|| fib.to_str()); fac.to_str();
});
bh.iter(|| {
fib.to_str();
});
} }
} }

View file

@ -499,35 +499,35 @@ mod tests {
#[bench] #[bench]
fn bench_new(b: &mut test::BenchHarness) { fn bench_new(b: &mut test::BenchHarness) {
do b.iter { b.iter(|| {
let _: RingBuf<u64> = RingBuf::new(); let _: RingBuf<u64> = RingBuf::new();
} })
} }
#[bench] #[bench]
fn bench_push_back(b: &mut test::BenchHarness) { fn bench_push_back(b: &mut test::BenchHarness) {
let mut deq = RingBuf::new(); let mut deq = RingBuf::new();
do b.iter { b.iter(|| {
deq.push_back(0); deq.push_back(0);
} })
} }
#[bench] #[bench]
fn bench_push_front(b: &mut test::BenchHarness) { fn bench_push_front(b: &mut test::BenchHarness) {
let mut deq = RingBuf::new(); let mut deq = RingBuf::new();
do b.iter { b.iter(|| {
deq.push_front(0); deq.push_front(0);
} })
} }
#[bench] #[bench]
fn bench_grow(b: &mut test::BenchHarness) { fn bench_grow(b: &mut test::BenchHarness) {
let mut deq = RingBuf::new(); let mut deq = RingBuf::new();
do b.iter { b.iter(|| {
do 65.times { 65.times(|| {
deq.push_front(1); deq.push_front(1);
} })
} })
} }
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]

View file

@ -828,7 +828,7 @@ mod test_qsort {
let expected = ~[1, 2, 3]; let expected = ~[1, 2, 3];
do quick_sort(names) |x, y| { *x < *y }; quick_sort(names, |x, y| *x < *y);
let immut_names = names; let immut_names = names;
@ -968,9 +968,9 @@ mod test_tim_sort {
#[cfg(unix)] #[cfg(unix)]
fn crash_test() { fn crash_test() {
let mut rng = rand::rng(); let mut rng = rand::rng();
let mut arr = do vec::from_fn(1000) |_i| { let mut arr = vec::from_fn(1000, |_i| {
CVal { val: rng.gen() } CVal { val: rng.gen() }
}; });
tim_sort(arr); tim_sort(arr);
fail!("Guarantee the fail"); fail!("Guarantee the fail");
@ -991,9 +991,9 @@ mod test_tim_sort {
#[test] #[test]
fn test_bad_Ord_impl() { fn test_bad_Ord_impl() {
let mut rng = rand::rng(); let mut rng = rand::rng();
let mut arr = do vec::from_fn(500) |_i| { let mut arr = vec::from_fn(500, |_i| {
DVal { val: rng.gen() } DVal { val: rng.gen() }
}; });
tim_sort(arr); tim_sort(arr);
} }
@ -1024,14 +1024,14 @@ mod big_tests {
fn multiplyVec<T:Clone>(arr: &[T], num: uint) -> ~[T] { fn multiplyVec<T:Clone>(arr: &[T], num: uint) -> ~[T] {
let size = arr.len(); let size = arr.len();
let res = do vec::from_fn(num) |i| { let res = vec::from_fn(num, |i| {
arr[i % size].clone() arr[i % size].clone()
}; });
res res
} }
fn makeRange(n: uint) -> ~[uint] { fn makeRange(n: uint) -> ~[uint] {
let one = do vec::from_fn(n) |i| { i }; let one = vec::from_fn(n, |i| i);
let mut two = one.clone(); let mut two = one.clone();
two.reverse(); two.reverse();
vec::append(two, one) vec::append(two, one)
@ -1050,9 +1050,9 @@ mod big_tests {
for i in range(lo, hi) { for i in range(lo, hi) {
let n = 1 << i; let n = 1 << i;
let mut arr: ~[f64] = do vec::from_fn(n) |_i| { let mut arr: ~[f64] = vec::from_fn(n, |_i| {
rng.gen() rng.gen()
}; });
tim_sort(arr); // *sort tim_sort(arr); // *sort
isSorted(arr); isSorted(arr);
@ -1064,11 +1064,11 @@ mod big_tests {
tim_sort(arr); // /sort tim_sort(arr); // /sort
isSorted(arr); isSorted(arr);
do 3.times { 3.times(|| {
let i1 = rng.gen_range(0u, n); let i1 = rng.gen_range(0u, n);
let i2 = rng.gen_range(0u, n); let i2 = rng.gen_range(0u, n);
arr.swap(i1, i2); arr.swap(i1, i2);
} });
tim_sort(arr); // 3sort tim_sort(arr); // 3sort
isSorted(arr); isSorted(arr);
@ -1083,10 +1083,10 @@ mod big_tests {
tim_sort(arr); // +sort tim_sort(arr); // +sort
isSorted(arr); isSorted(arr);
do (n/100).times { (n/100).times(|| {
let idx = rng.gen_range(0u, n); let idx = rng.gen_range(0u, n);
arr[idx] = rng.gen(); arr[idx] = rng.gen();
} });
tim_sort(arr); tim_sort(arr);
isSorted(arr); isSorted(arr);
@ -1121,9 +1121,9 @@ mod big_tests {
for i in range(lo, hi) { for i in range(lo, hi) {
let n = 1 << i; let n = 1 << i;
let arr: ~[@f64] = do vec::from_fn(n) |_i| { let arr: ~[@f64] = vec::from_fn(n, |_i| {
@rng.gen() @rng.gen()
}; });
let mut arr = arr; let mut arr = arr;
tim_sort(arr); // *sort tim_sort(arr); // *sort
@ -1136,11 +1136,11 @@ mod big_tests {
tim_sort(arr); // /sort tim_sort(arr); // /sort
isSorted(arr); isSorted(arr);
do 3.times { 3.times(|| {
let i1 = rng.gen_range(0u, n); let i1 = rng.gen_range(0u, n);
let i2 = rng.gen_range(0u, n); let i2 = rng.gen_range(0u, n);
arr.swap(i1, i2); arr.swap(i1, i2);
} });
tim_sort(arr); // 3sort tim_sort(arr); // 3sort
isSorted(arr); isSorted(arr);
@ -1155,10 +1155,10 @@ mod big_tests {
tim_sort(arr); // +sort tim_sort(arr); // +sort
isSorted(arr); isSorted(arr);
do (n/100).times { (n/100).times(|| {
let idx = rng.gen_range(0u, n); let idx = rng.gen_range(0u, n);
arr[idx] = @rng.gen(); arr[idx] = @rng.gen();
} });
tim_sort(arr); tim_sort(arr);
isSorted(arr); isSorted(arr);

View file

@ -571,15 +571,15 @@ impl RWLock {
* # Example * # Example
* *
* ```rust * ```rust
* do lock.write_downgrade |mut write_token| { * lock.write_downgrade(|mut write_token| {
* do write_token.write_cond |condvar| { * write_token.write_cond(|condvar| {
* ... exclusive access ... * ... exclusive access ...
* } * });
* let read_token = lock.downgrade(write_token); * let read_token = lock.downgrade(write_token);
* do read_token.read { * read_token.read(|| {
* ... shared access ... * ... shared access ...
* } * })
* } * })
* ``` * ```
*/ */
pub fn write_downgrade<U>(&self, blk: |v: RWLockWriteMode| -> U) -> U { pub fn write_downgrade<U>(&self, blk: |v: RWLockWriteMode| -> U) -> U {
@ -698,20 +698,20 @@ mod tests {
#[test] #[test]
fn test_sem_basic() { fn test_sem_basic() {
let s = Semaphore::new(1); let s = Semaphore::new(1);
do s.access { } s.access(|| { })
} }
#[test] #[test]
fn test_sem_as_mutex() { fn test_sem_as_mutex() {
let s = Semaphore::new(1); let s = Semaphore::new(1);
let s2 = s.clone(); let s2 = s.clone();
do task::spawn { do task::spawn {
do s2.access { s2.access(|| {
do 5.times { task::deschedule(); } 5.times(|| { task::deschedule(); })
} })
}
do s.access {
do 5.times { task::deschedule(); }
} }
s.access(|| {
5.times(|| { task::deschedule(); })
})
} }
#[test] #[test]
fn test_sem_as_cvar() { fn test_sem_as_cvar() {
@ -723,7 +723,7 @@ mod tests {
s2.acquire(); s2.acquire();
c.send(()); c.send(());
} }
do 5.times { task::deschedule(); } 5.times(|| { task::deschedule(); });
s.release(); s.release();
let _ = p.recv(); let _ = p.recv();
@ -732,7 +732,7 @@ mod tests {
let s = Semaphore::new(0); let s = Semaphore::new(0);
let s2 = s.clone(); let s2 = s.clone();
do task::spawn { do task::spawn {
do 5.times { task::deschedule(); } 5.times(|| { task::deschedule(); });
s2.release(); s2.release();
let _ = p.recv(); let _ = p.recv();
} }
@ -748,15 +748,15 @@ mod tests {
let (p1,c1) = comm::stream(); let (p1,c1) = comm::stream();
let (p2,c2) = comm::stream(); let (p2,c2) = comm::stream();
do task::spawn { do task::spawn {
do s2.access { s2.access(|| {
let _ = p2.recv(); let _ = p2.recv();
c1.send(()); c1.send(());
})
} }
} s.access(|| {
do s.access {
c2.send(()); c2.send(());
let _ = p1.recv(); let _ = p1.recv();
} })
} }
#[test] #[test]
fn test_sem_runtime_friendly_blocking() { fn test_sem_runtime_friendly_blocking() {
@ -767,16 +767,16 @@ mod tests {
let s2 = s.clone(); let s2 = s.clone();
let (p, c) = comm::stream(); let (p, c) = comm::stream();
let child_data = Cell::new((s2, c)); let child_data = Cell::new((s2, c));
do s.access { s.access(|| {
let (s2, c) = child_data.take(); let (s2, c) = child_data.take();
do task::spawn { do task::spawn {
c.send(()); c.send(());
do s2.access { } s2.access(|| { });
c.send(()); c.send(());
} }
let _ = p.recv(); // wait for child to come alive let _ = p.recv(); // wait for child to come alive
do 5.times { task::deschedule(); } // let the child contend 5.times(|| { task::deschedule(); }); // let the child contend
} });
let _ = p.recv(); // wait for child to be done let _ = p.recv(); // wait for child to be done
} }
} }
@ -809,13 +809,13 @@ mod tests {
} }
fn access_shared(sharedstate: &mut int, m: &Mutex, n: uint) { fn access_shared(sharedstate: &mut int, m: &Mutex, n: uint) {
do n.times { n.times(|| {
do m.lock { m.lock(|| {
let oldval = *sharedstate; let oldval = *sharedstate;
task::deschedule(); task::deschedule();
*sharedstate = oldval + 1; *sharedstate = oldval + 1;
} })
} })
} }
} }
#[test] #[test]
@ -823,31 +823,31 @@ mod tests {
let m = Mutex::new(); let m = Mutex::new();
// Child wakes up parent // Child wakes up parent
do m.lock_cond |cond| { m.lock_cond(|cond| {
let m2 = m.clone(); let m2 = m.clone();
do task::spawn { do task::spawn {
do m2.lock_cond |cond| { m2.lock_cond(|cond| {
let woken = cond.signal(); let woken = cond.signal();
assert!(woken); assert!(woken);
} })
} }
cond.wait(); cond.wait();
} });
// Parent wakes up child // Parent wakes up child
let (port,chan) = comm::stream(); let (port,chan) = comm::stream();
let m3 = m.clone(); let m3 = m.clone();
do task::spawn { do task::spawn {
do m3.lock_cond |cond| { m3.lock_cond(|cond| {
chan.send(()); chan.send(());
cond.wait(); cond.wait();
chan.send(()); chan.send(());
} })
} }
let _ = port.recv(); // Wait until child gets in the mutex let _ = port.recv(); // Wait until child gets in the mutex
do m.lock_cond |cond| { m.lock_cond(|cond| {
let woken = cond.signal(); let woken = cond.signal();
assert!(woken); assert!(woken);
} });
let _ = port.recv(); // Wait until child wakes up let _ = port.recv(); // Wait until child wakes up
} }
#[cfg(test)] #[cfg(test)]
@ -855,25 +855,25 @@ mod tests {
let m = Mutex::new(); let m = Mutex::new();
let mut ports = ~[]; let mut ports = ~[];
do num_waiters.times { num_waiters.times(|| {
let mi = m.clone(); let mi = m.clone();
let (port, chan) = comm::stream(); let (port, chan) = comm::stream();
ports.push(port); ports.push(port);
do task::spawn { do task::spawn {
do mi.lock_cond |cond| { mi.lock_cond(|cond| {
chan.send(()); chan.send(());
cond.wait(); cond.wait();
chan.send(()); chan.send(());
})
} }
} });
}
// wait until all children get in the mutex // wait until all children get in the mutex
for port in ports.iter() { let _ = port.recv(); } for port in ports.iter() { let _ = port.recv(); }
do m.lock_cond |cond| { m.lock_cond(|cond| {
let num_woken = cond.broadcast(); let num_woken = cond.broadcast();
assert_eq!(num_woken, num_waiters); assert_eq!(num_woken, num_waiters);
} });
// wait until all children wake up // wait until all children wake up
for port in ports.iter() { let _ = port.recv(); } for port in ports.iter() { let _ = port.recv(); }
} }
@ -890,11 +890,11 @@ mod tests {
let m = Mutex::new(); let m = Mutex::new();
let m2 = m.clone(); let m2 = m.clone();
do task::try { do task::try {
do m.lock_cond |_x| { } m.lock_cond(|_x| { })
}; };
do m2.lock_cond |cond| { m2.lock_cond(|cond| {
assert!(!cond.signal()); assert!(!cond.signal());
} })
} }
#[test] #[test]
fn test_mutex_killed_simple() { fn test_mutex_killed_simple() {
@ -903,13 +903,13 @@ mod tests {
let m2 = m.clone(); let m2 = m.clone();
let result: result::Result<(), ~Any> = do task::try { let result: result::Result<(), ~Any> = do task::try {
do m2.lock { m2.lock(|| {
fail!(); fail!();
} })
}; };
assert!(result.is_err()); assert!(result.is_err());
// child task must have finished by the time try returns // child task must have finished by the time try returns
do m.lock { } m.lock(|| { })
} }
#[ignore(reason = "linked failure")] #[ignore(reason = "linked failure")]
#[test] #[test]
@ -926,17 +926,17 @@ mod tests {
task::deschedule(); task::deschedule();
fail!(); fail!();
} }
do m2.lock_cond |cond| { m2.lock_cond(|cond| {
c.send(()); // tell sibling go ahead c.send(()); // tell sibling go ahead
cond.wait(); // block forever cond.wait(); // block forever
} })
}; };
assert!(result.is_err()); assert!(result.is_err());
// child task must have finished by the time try returns // child task must have finished by the time try returns
do m.lock_cond |cond| { m.lock_cond(|cond| {
let woken = cond.signal(); let woken = cond.signal();
assert!(!woken); assert!(!woken);
} })
} }
#[ignore(reason = "linked failure")] #[ignore(reason = "linked failure")]
#[test] #[test]
@ -949,30 +949,30 @@ mod tests {
let result: result::Result<(), ~Any> = do task::try { let result: result::Result<(), ~Any> = do task::try {
let mut sibling_convos = ~[]; let mut sibling_convos = ~[];
do 2.times { 2.times(|| {
let (p, c) = comm::stream(); let (p, c) = comm::stream();
let c = Cell::new(c); let c = Cell::new(c);
sibling_convos.push(p); sibling_convos.push(p);
let mi = m2.clone(); let mi = m2.clone();
// spawn sibling task // spawn sibling task
do task::spawn { // linked do task::spawn { // linked
do mi.lock_cond |cond| { mi.lock_cond(|cond| {
let c = c.take(); let c = c.take();
c.send(()); // tell sibling to go ahead c.send(()); // tell sibling to go ahead
do (|| { (|| {
cond.wait(); // block forever cond.wait(); // block forever
}).finally { }).finally(|| {
error!("task unwinding and sending"); error!("task unwinding and sending");
c.send(()); c.send(());
error!("task unwinding and done sending"); error!("task unwinding and done sending");
})
})
} }
} });
}
}
for p in sibling_convos.iter() { for p in sibling_convos.iter() {
let _ = p.recv(); // wait for sibling to get in the mutex let _ = p.recv(); // wait for sibling to get in the mutex
} }
do m2.lock { } m2.lock(|| { });
c.send(sibling_convos); // let parent wait on all children c.send(sibling_convos); // let parent wait on all children
fail!(); fail!();
}; };
@ -980,24 +980,24 @@ mod tests {
// child task must have finished by the time try returns // child task must have finished by the time try returns
let r = p.recv(); let r = p.recv();
for p in r.iter() { p.recv(); } // wait on all its siblings for p in r.iter() { p.recv(); } // wait on all its siblings
do m.lock_cond |cond| { m.lock_cond(|cond| {
let woken = cond.broadcast(); let woken = cond.broadcast();
assert_eq!(woken, 0); assert_eq!(woken, 0);
} })
} }
#[test] #[test]
fn test_mutex_cond_signal_on_0() { fn test_mutex_cond_signal_on_0() {
// Tests that signal_on(0) is equivalent to signal(). // Tests that signal_on(0) is equivalent to signal().
let m = Mutex::new(); let m = Mutex::new();
do m.lock_cond |cond| { m.lock_cond(|cond| {
let m2 = m.clone(); let m2 = m.clone();
do task::spawn { do task::spawn {
do m2.lock_cond |cond| { m2.lock_cond(|cond| {
cond.signal_on(0); cond.signal_on(0);
} })
} }
cond.wait(); cond.wait();
} })
} }
#[test] #[test]
fn test_mutex_different_conds() { fn test_mutex_different_conds() {
@ -1006,17 +1006,17 @@ mod tests {
let m2 = m.clone(); let m2 = m.clone();
let (p, c) = comm::stream(); let (p, c) = comm::stream();
do task::spawn { do task::spawn {
do m2.lock_cond |cond| { m2.lock_cond(|cond| {
c.send(()); c.send(());
cond.wait_on(1); cond.wait_on(1);
} })
} }
let _ = p.recv(); let _ = p.recv();
do m.lock_cond |cond| { m.lock_cond(|cond| {
if !cond.signal_on(0) { if !cond.signal_on(0) {
fail!(); // success; punt sibling awake. fail!(); // success; punt sibling awake.
} }
} })
}; };
assert!(result.is_err()); assert!(result.is_err());
} }
@ -1024,17 +1024,17 @@ mod tests {
fn test_mutex_no_condvars() { fn test_mutex_no_condvars() {
let result = do task::try { let result = do task::try {
let m = Mutex::new_with_condvars(0); let m = Mutex::new_with_condvars(0);
do m.lock_cond |cond| { cond.wait(); } m.lock_cond(|cond| { cond.wait(); })
}; };
assert!(result.is_err()); assert!(result.is_err());
let result = do task::try { let result = do task::try {
let m = Mutex::new_with_condvars(0); let m = Mutex::new_with_condvars(0);
do m.lock_cond |cond| { cond.signal(); } m.lock_cond(|cond| { cond.signal(); })
}; };
assert!(result.is_err()); assert!(result.is_err());
let result = do task::try { let result = do task::try {
let m = Mutex::new_with_condvars(0); let m = Mutex::new_with_condvars(0);
do m.lock_cond |cond| { cond.broadcast(); } m.lock_cond(|cond| { cond.broadcast(); })
}; };
assert!(result.is_err()); assert!(result.is_err());
} }
@ -1049,14 +1049,14 @@ mod tests {
Read => x.read(blk), Read => x.read(blk),
Write => x.write(blk), Write => x.write(blk),
Downgrade => Downgrade =>
do x.write_downgrade |mode| { x.write_downgrade(|mode| {
do mode.write { blk() }; mode.write(|| { blk() });
}, }),
DowngradeRead => DowngradeRead =>
do x.write_downgrade |mode| { x.write_downgrade(|mode| {
let mode = x.downgrade(mode); let mode = x.downgrade(mode);
do mode.read { blk() }; mode.read(|| { blk() });
}, }),
} }
} }
#[cfg(test)] #[cfg(test)]
@ -1086,13 +1086,13 @@ mod tests {
fn access_shared(sharedstate: &mut int, x: &RWLock, mode: RWLockMode, fn access_shared(sharedstate: &mut int, x: &RWLock, mode: RWLockMode,
n: uint) { n: uint) {
do n.times { n.times(|| {
do lock_rwlock_in_mode(x, mode) { lock_rwlock_in_mode(x, mode, || {
let oldval = *sharedstate; let oldval = *sharedstate;
task::deschedule(); task::deschedule();
*sharedstate = oldval + 1; *sharedstate = oldval + 1;
} })
} })
} }
} }
#[test] #[test]
@ -1122,24 +1122,24 @@ mod tests {
if !make_mode2_go_first { if !make_mode2_go_first {
let _ = p2.recv(); // parent sends to us once it locks, or ... let _ = p2.recv(); // parent sends to us once it locks, or ...
} }
do lock_rwlock_in_mode(&x2, mode2) { lock_rwlock_in_mode(&x2, mode2, || {
if make_mode2_go_first { if make_mode2_go_first {
c1.send(()); // ... we send to it once we lock c1.send(()); // ... we send to it once we lock
} }
let _ = p2.recv(); let _ = p2.recv();
c1.send(()); c1.send(());
} })
} }
if make_mode2_go_first { if make_mode2_go_first {
let _ = p1.recv(); // child sends to us once it locks, or ... let _ = p1.recv(); // child sends to us once it locks, or ...
} }
do lock_rwlock_in_mode(x, mode1) { lock_rwlock_in_mode(x, mode1, || {
if !make_mode2_go_first { if !make_mode2_go_first {
c2.send(()); // ... we send to it once we lock c2.send(()); // ... we send to it once we lock
} }
c2.send(()); c2.send(());
let _ = p1.recv(); let _ = p1.recv();
} })
} }
#[test] #[test]
fn test_rwlock_readers_and_readers() { fn test_rwlock_readers_and_readers() {
@ -1154,16 +1154,16 @@ mod tests {
fn test_rwlock_downgrade_unlock() { fn test_rwlock_downgrade_unlock() {
// Tests that downgrade can unlock the lock in both modes // Tests that downgrade can unlock the lock in both modes
let x = RWLock::new(); let x = RWLock::new();
do lock_rwlock_in_mode(&x, Downgrade) { } lock_rwlock_in_mode(&x, Downgrade, || { });
test_rwlock_handshake(&x, Read, Read, false); test_rwlock_handshake(&x, Read, Read, false);
let y = RWLock::new(); let y = RWLock::new();
do lock_rwlock_in_mode(&y, DowngradeRead) { } lock_rwlock_in_mode(&y, DowngradeRead, || { });
test_rwlock_exclusion(&y, Write, Write); test_rwlock_exclusion(&y, Write, Write);
} }
#[test] #[test]
fn test_rwlock_read_recursive() { fn test_rwlock_read_recursive() {
let x = RWLock::new(); let x = RWLock::new();
do x.read { do x.read { } } x.read(|| { x.read(|| { }) })
} }
#[test] #[test]
fn test_rwlock_cond_wait() { fn test_rwlock_cond_wait() {
@ -1171,34 +1171,34 @@ mod tests {
let x = RWLock::new(); let x = RWLock::new();
// Child wakes up parent // Child wakes up parent
do x.write_cond |cond| { x.write_cond(|cond| {
let x2 = x.clone(); let x2 = x.clone();
do task::spawn { do task::spawn {
do x2.write_cond |cond| { x2.write_cond(|cond| {
let woken = cond.signal(); let woken = cond.signal();
assert!(woken); assert!(woken);
} })
} }
cond.wait(); cond.wait();
} });
// Parent wakes up child // Parent wakes up child
let (port, chan) = comm::stream(); let (port, chan) = comm::stream();
let x3 = x.clone(); let x3 = x.clone();
do task::spawn { do task::spawn {
do x3.write_cond |cond| { x3.write_cond(|cond| {
chan.send(()); chan.send(());
cond.wait(); cond.wait();
chan.send(()); chan.send(());
} })
} }
let _ = port.recv(); // Wait until child gets in the rwlock let _ = port.recv(); // Wait until child gets in the rwlock
do x.read { } // Must be able to get in as a reader in the meantime x.read(|| { }); // Must be able to get in as a reader in the meantime
do x.write_cond |cond| { // Or as another writer x.write_cond(|cond| { // Or as another writer
let woken = cond.signal(); let woken = cond.signal();
assert!(woken); assert!(woken);
} });
let _ = port.recv(); // Wait until child wakes up let _ = port.recv(); // Wait until child wakes up
do x.read { } // Just for good measure x.read(|| { }); // Just for good measure
} }
#[cfg(test)] #[cfg(test)]
fn test_rwlock_cond_broadcast_helper(num_waiters: uint, fn test_rwlock_cond_broadcast_helper(num_waiters: uint,
@ -1207,35 +1207,35 @@ mod tests {
// Much like the mutex broadcast test. Downgrade-enabled. // Much like the mutex broadcast test. Downgrade-enabled.
fn lock_cond(x: &RWLock, downgrade: bool, blk: |c: &Condvar|) { fn lock_cond(x: &RWLock, downgrade: bool, blk: |c: &Condvar|) {
if downgrade { if downgrade {
do x.write_downgrade |mode| { x.write_downgrade(|mode| {
do mode.write_cond |c| { blk(c) } mode.write_cond(|c| { blk(c) });
} });
} else { } else {
do x.write_cond |c| { blk(c) } x.write_cond(|c| { blk(c) });
} }
} }
let x = RWLock::new(); let x = RWLock::new();
let mut ports = ~[]; let mut ports = ~[];
do num_waiters.times { num_waiters.times(|| {
let xi = x.clone(); let xi = x.clone();
let (port, chan) = comm::stream(); let (port, chan) = comm::stream();
ports.push(port); ports.push(port);
do task::spawn { do task::spawn {
do lock_cond(&xi, dg1) |cond| { lock_cond(&xi, dg1, |cond| {
chan.send(()); chan.send(());
cond.wait(); cond.wait();
chan.send(()); chan.send(());
})
} }
} });
}
// wait until all children get in the mutex // wait until all children get in the mutex
for port in ports.iter() { let _ = port.recv(); } for port in ports.iter() { let _ = port.recv(); }
do lock_cond(&x, dg2) |cond| { lock_cond(&x, dg2, |cond| {
let num_woken = cond.broadcast(); let num_woken = cond.broadcast();
assert_eq!(num_woken, num_waiters); assert_eq!(num_woken, num_waiters);
} });
// wait until all children wake up // wait until all children wake up
for port in ports.iter() { let _ = port.recv(); } for port in ports.iter() { let _ = port.recv(); }
} }
@ -1257,13 +1257,13 @@ mod tests {
let x2 = x.clone(); let x2 = x.clone();
let result: result::Result<(), ~Any> = do task::try || { let result: result::Result<(), ~Any> = do task::try || {
do lock_rwlock_in_mode(&x2, mode1) { lock_rwlock_in_mode(&x2, mode1, || {
fail!(); fail!();
} })
}; };
assert!(result.is_err()); assert!(result.is_err());
// child task must have finished by the time try returns // child task must have finished by the time try returns
do lock_rwlock_in_mode(&x, mode2) { } lock_rwlock_in_mode(&x, mode2, || { })
} }
#[test] #[test]
fn test_rwlock_reader_killed_writer() { fn test_rwlock_reader_killed_writer() {
@ -1301,12 +1301,12 @@ mod tests {
// Tests that you can't downgrade with a different rwlock's token. // Tests that you can't downgrade with a different rwlock's token.
let x = RWLock::new(); let x = RWLock::new();
let y = RWLock::new(); let y = RWLock::new();
do x.write_downgrade |xwrite| { x.write_downgrade(|xwrite| {
let mut xopt = Some(xwrite); let mut xopt = Some(xwrite);
do y.write_downgrade |_ywrite| { y.write_downgrade(|_ywrite| {
y.downgrade(xopt.take_unwrap()); y.downgrade(xopt.take_unwrap());
error!("oops, y.downgrade(x) should have failed!"); error!("oops, y.downgrade(x) should have failed!");
} })
} })
} }
} }

View file

@ -102,7 +102,7 @@ fn test_task_pool() {
g g
}; };
let mut pool = TaskPool::new(4, Some(SingleThreaded), f); let mut pool = TaskPool::new(4, Some(SingleThreaded), f);
do 8.times { 8.times(|| {
pool.execute(|i| println!("Hello from thread {}!", *i)); pool.execute(|i| println!("Hello from thread {}!", *i));
} })
} }

View file

@ -1027,8 +1027,8 @@ mod test_treemap {
let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(&[42]); let mut rng: rand::IsaacRng = rand::SeedableRng::from_seed(&[42]);
do 3.times { 3.times(|| {
do 90.times { 90.times(|| {
let k = rng.gen(); let k = rng.gen();
let v = rng.gen(); let v = rng.gen();
if !ctrl.iter().any(|x| x == &(k, v)) { if !ctrl.iter().any(|x| x == &(k, v)) {
@ -1037,16 +1037,16 @@ mod test_treemap {
check_structure(&map); check_structure(&map);
check_equal(ctrl, &map); check_equal(ctrl, &map);
} }
} });
do 30.times { 30.times(|| {
let r = rng.gen_range(0, ctrl.len()); let r = rng.gen_range(0, ctrl.len());
let (key, _) = ctrl.remove(r); let (key, _) = ctrl.remove(r);
assert!(map.remove(&key)); assert!(map.remove(&key));
check_structure(&map); check_structure(&map);
check_equal(ctrl, &map); check_equal(ctrl, &map);
} });
} })
} }
#[test] #[test]
@ -1414,11 +1414,11 @@ mod test_set {
for y in b.iter() { assert!(set_b.insert(*y)) } for y in b.iter() { assert!(set_b.insert(*y)) }
let mut i = 0; let mut i = 0;
do f(&set_a, &set_b) |x| { f(&set_a, &set_b, |x| {
assert_eq!(*x, expected[i]); assert_eq!(*x, expected[i]);
i += 1; i += 1;
true true
}; });
assert_eq!(i, expected.len()); assert_eq!(i, expected.len());
} }

View file

@ -811,24 +811,24 @@ mod bench {
#[bench] #[bench]
pub fn create_uuids(bh: &mut BenchHarness) { pub fn create_uuids(bh: &mut BenchHarness) {
do bh.iter { bh.iter(|| {
Uuid::new_v4(); Uuid::new_v4();
} })
} }
#[bench] #[bench]
pub fn uuid_to_str(bh: &mut BenchHarness) { pub fn uuid_to_str(bh: &mut BenchHarness) {
let u = Uuid::new_v4(); let u = Uuid::new_v4();
do bh.iter { bh.iter(|| {
u.to_str(); u.to_str();
} })
} }
#[bench] #[bench]
pub fn parse_str(bh: &mut BenchHarness) { pub fn parse_str(bh: &mut BenchHarness) {
let s = "urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4"; let s = "urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4";
do bh.iter { bh.iter(|| {
Uuid::parse_string(s); Uuid::parse_string(s);
} })
} }
} }

View file

@ -384,7 +384,7 @@ fn executable_exists(repo: &Path, short_name: &str) -> bool {
fn test_executable_exists(repo: &Path, short_name: &str) -> bool { fn test_executable_exists(repo: &Path, short_name: &str) -> bool {
debug!("test_executable_exists: repo = {}, short_name = {}", repo.display(), short_name); debug!("test_executable_exists: repo = {}, short_name = {}", repo.display(), short_name);
let exec = built_test_in_workspace(&PkgId::new(short_name), repo); let exec = built_test_in_workspace(&PkgId::new(short_name), repo);
exec.map_default(false, |exec| exec.exists() && is_rwx(&exec)); exec.map_default(false, |exec| exec.exists() && is_rwx(&exec))
} }
fn remove_executable_file(p: &PkgId, workspace: &Path) { fn remove_executable_file(p: &PkgId, workspace: &Path) {

View file

@ -392,11 +392,11 @@ fn local_loop() -> &'static mut Loop {
unsafe { unsafe {
cast::transmute(Local::borrow(|sched: &mut Scheduler| { cast::transmute(Local::borrow(|sched: &mut Scheduler| {
let mut io = None; let mut io = None;
do sched.event_loop.io |i| { sched.event_loop.io(|i| {
let (_vtable, uvio): (uint, &'static mut uvio::UvIoFactory) = let (_vtable, uvio): (uint, &'static mut uvio::UvIoFactory) =
cast::transmute(i); cast::transmute(i);
io = Some(uvio); io = Some(uvio);
} });
io.unwrap() io.unwrap()
}).uv_loop()) }).uv_loop())
} }

View file

@ -48,9 +48,9 @@ fn with_error_checking_parse<T>(s: @str, f: |&mut Parser| -> T) -> T {
// parse a string, return a crate. // parse a string, return a crate.
pub fn string_to_crate (source_str : @str) -> ast::Crate { pub fn string_to_crate (source_str : @str) -> ast::Crate {
do with_error_checking_parse(source_str) |p| { with_error_checking_parse(source_str, |p| {
p.parse_crate_mod() p.parse_crate_mod()
} })
} }
// parse a string, return a crate and the ParseSess // parse a string, return a crate and the ParseSess
@ -61,23 +61,23 @@ pub fn string_to_crate_and_sess (source_str : @str) -> (ast::Crate,@mut ParseSes
// parse a string, return an expr // parse a string, return an expr
pub fn string_to_expr (source_str : @str) -> @ast::Expr { pub fn string_to_expr (source_str : @str) -> @ast::Expr {
do with_error_checking_parse(source_str) |p| { with_error_checking_parse(source_str, |p| {
p.parse_expr() p.parse_expr()
} })
} }
// parse a string, return an item // parse a string, return an item
pub fn string_to_item (source_str : @str) -> Option<@ast::item> { pub fn string_to_item (source_str : @str) -> Option<@ast::item> {
do with_error_checking_parse(source_str) |p| { with_error_checking_parse(source_str, |p| {
p.parse_item(~[]) p.parse_item(~[])
} })
} }
// parse a string, return a stmt // parse a string, return a stmt
pub fn string_to_stmt(source_str : @str) -> @ast::Stmt { pub fn string_to_stmt(source_str : @str) -> @ast::Stmt {
do with_error_checking_parse(source_str) |p| { with_error_checking_parse(source_str, |p| {
p.parse_stmt(~[]) p.parse_stmt(~[])
} })
} }
// parse a string, return a pat. Uses "irrefutable"... which doesn't // parse a string, return a pat. Uses "irrefutable"... which doesn't

View file

@ -29,66 +29,65 @@ fn timed(label: &str, f: ||) {
fn ascending<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint) { fn ascending<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint) {
println(" Ascending integers:"); println(" Ascending integers:");
do timed("insert") { timed("insert", || {
for i in range(0u, n_keys) { for i in range(0u, n_keys) {
map.insert(i, i + 1); map.insert(i, i + 1);
} }
} });
do timed("search") { timed("search", || {
for i in range(0u, n_keys) { for i in range(0u, n_keys) {
assert_eq!(map.find(&i).unwrap(), &(i + 1)); assert_eq!(map.find(&i).unwrap(), &(i + 1));
} }
} });
do timed("remove") { timed("remove", || {
for i in range(0, n_keys) { for i in range(0, n_keys) {
assert!(map.remove(&i)); assert!(map.remove(&i));
} }
} });
} }
fn descending<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint) { fn descending<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint) {
println(" Descending integers:"); println(" Descending integers:");
do timed("insert") { timed("insert", || {
for i in range(0, n_keys).invert() { for i in range(0, n_keys).invert() {
map.insert(i, i + 1); map.insert(i, i + 1);
} }
} });
do timed("search") { timed("search", || {
for i in range(0, n_keys).invert() { for i in range(0, n_keys).invert() {
assert_eq!(map.find(&i).unwrap(), &(i + 1)); assert_eq!(map.find(&i).unwrap(), &(i + 1));
} }
} });
do timed("remove") { timed("remove", || {
for i in range(0, n_keys) { for i in range(0, n_keys) {
assert!(map.remove(&i)); assert!(map.remove(&i));
} }
} });
} }
fn vector<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint, dist: &[uint]) { fn vector<M: MutableMap<uint, uint>>(map: &mut M, n_keys: uint, dist: &[uint]) {
timed("insert", || {
do timed("insert") {
for i in range(0u, n_keys) { for i in range(0u, n_keys) {
map.insert(dist[i], i + 1); map.insert(dist[i], i + 1);
} }
} });
do timed("search") { timed("search", || {
for i in range(0u, n_keys) { for i in range(0u, n_keys) {
assert_eq!(map.find(&dist[i]).unwrap(), &(i + 1)); assert_eq!(map.find(&dist[i]).unwrap(), &(i + 1));
} }
} });
do timed("remove") { timed("remove", || {
for i in range(0u, n_keys) { for i in range(0u, n_keys) {
assert!(map.remove(&dist[i])); assert!(map.remove(&dist[i]));
} }
} });
} }
fn main() { fn main() {

View file

@ -43,7 +43,7 @@ impl Results {
rand_cap: uint, rand_cap: uint,
f: || -> T) { { f: || -> T) { {
let mut set = f(); let mut set = f();
do timed(&mut self.sequential_ints) { timed(&mut self.sequential_ints, || {
for i in range(0u, num_keys) { for i in range(0u, num_keys) {
set.insert(i); set.insert(i);
} }
@ -51,16 +51,16 @@ impl Results {
for i in range(0u, num_keys) { for i in range(0u, num_keys) {
assert!(set.contains(&i)); assert!(set.contains(&i));
} }
} })
} }
{ {
let mut set = f(); let mut set = f();
do timed(&mut self.random_ints) { timed(&mut self.random_ints, || {
for _ in range(0, num_keys) { for _ in range(0, num_keys) {
set.insert(rng.gen::<uint>() % rand_cap); set.insert(rng.gen::<uint>() % rand_cap);
} }
} })
} }
{ {
@ -69,11 +69,11 @@ impl Results {
set.insert(i); set.insert(i);
} }
do timed(&mut self.delete_ints) { timed(&mut self.delete_ints, || {
for i in range(0u, num_keys) { for i in range(0u, num_keys) {
assert!(set.remove(&i)); assert!(set.remove(&i));
} }
} })
} }
} }
@ -85,7 +85,7 @@ impl Results {
f: || -> T) { f: || -> T) {
{ {
let mut set = f(); let mut set = f();
do timed(&mut self.sequential_strings) { timed(&mut self.sequential_strings, || {
for i in range(0u, num_keys) { for i in range(0u, num_keys) {
set.insert(i.to_str()); set.insert(i.to_str());
} }
@ -93,17 +93,17 @@ impl Results {
for i in range(0u, num_keys) { for i in range(0u, num_keys) {
assert!(set.contains(&i.to_str())); assert!(set.contains(&i.to_str()));
} }
} })
} }
{ {
let mut set = f(); let mut set = f();
do timed(&mut self.random_strings) { timed(&mut self.random_strings, || {
for _ in range(0, num_keys) { for _ in range(0, num_keys) {
let s = rng.gen::<uint>().to_str(); let s = rng.gen::<uint>().to_str();
set.insert(s); set.insert(s);
} }
} })
} }
{ {
@ -111,11 +111,11 @@ impl Results {
for i in range(0u, num_keys) { for i in range(0u, num_keys) {
set.insert(i.to_str()); set.insert(i.to_str());
} }
do timed(&mut self.delete_strings) { timed(&mut self.delete_strings, || {
for i in range(0u, num_keys) { for i in range(0u, num_keys) {
assert!(set.remove(&i.to_str())); assert!(set.remove(&i.to_str()));
} }
} })
} }
} }
} }

View file

@ -29,20 +29,20 @@ type pipe = arc::MutexArc<~[uint]>;
fn send(p: &pipe, msg: uint) { fn send(p: &pipe, msg: uint) {
unsafe { unsafe {
do p.access_cond |state, cond| { p.access_cond(|state, cond| {
state.push(msg); state.push(msg);
cond.signal(); cond.signal();
} })
} }
} }
fn recv(p: &pipe) -> uint { fn recv(p: &pipe) -> uint {
unsafe { unsafe {
do p.access_cond |state, cond| { p.access_cond(|state, cond| {
while state.is_empty() { while state.is_empty() {
cond.wait(); cond.wait();
} }
state.pop() state.pop()
} })
} }
} }

View file

@ -28,18 +28,18 @@ use std::uint;
type pipe = arc::RWArc<~[uint]>; type pipe = arc::RWArc<~[uint]>;
fn send(p: &pipe, msg: uint) { fn send(p: &pipe, msg: uint) {
do p.write_cond |state, cond| { p.write_cond(|state, cond| {
state.push(msg); state.push(msg);
cond.signal(); cond.signal();
} })
} }
fn recv(p: &pipe) -> uint { fn recv(p: &pipe) -> uint {
do p.write_cond |state, cond| { p.write_cond(|state, cond| {
while state.is_empty() { while state.is_empty() {
cond.wait(); cond.wait();
} }
state.pop() state.pop()
} })
} }
fn init() -> (pipe,pipe) { fn init() -> (pipe,pipe) {

View file

@ -37,26 +37,25 @@ fn ping_pong_bench(n: uint, m: uint) {
do spawntask_later() || { do spawntask_later() || {
let chan = ca.take(); let chan = ca.take();
let port = pb.take(); let port = pb.take();
do n.times { n.times(|| {
chan.send(()); chan.send(());
port.recv(); port.recv();
} })
} }
do spawntask_later() || { do spawntask_later() || {
let chan = cb.take(); let chan = cb.take();
let port = pa.take(); let port = pa.take();
do n.times { n.times(|| {
port.recv(); port.recv();
chan.send(()); chan.send(());
} })
} }
} }
do m.times { m.times(|| {
run_pair(n) run_pair(n)
} })
} }

View file

@ -26,8 +26,8 @@ fn main() {
100000 100000
}; };
do n.times { n.times(|| {
do spawn || {}; do spawn || {};
} })
} }

View file

@ -28,11 +28,11 @@ fn item_check(t: &Tree) -> int {
fn bottom_up_tree<'r>(arena: &'r Arena, item: int, depth: int) -> &'r Tree<'r> { fn bottom_up_tree<'r>(arena: &'r Arena, item: int, depth: int) -> &'r Tree<'r> {
if depth > 0 { if depth > 0 {
do arena.alloc { arena.alloc(|| {
Node(bottom_up_tree(arena, 2 * item - 1, depth - 1), Node(bottom_up_tree(arena, 2 * item - 1, depth - 1),
bottom_up_tree(arena, 2 * item, depth - 1), bottom_up_tree(arena, 2 * item, depth - 1),
item) item)
} })
} else {arena.alloc(|| Nil)} } else {arena.alloc(|| Nil)}
} }

View file

@ -169,7 +169,7 @@ fn main() {
let sizes = ~[1u,2,3,4,6,12,18]; let sizes = ~[1u,2,3,4,6,12,18];
let mut streams = vec::from_fn(sizes.len(), |_| Some(stream::<~str>())); let mut streams = vec::from_fn(sizes.len(), |_| Some(stream::<~str>()));
let mut from_child = ~[]; let mut from_child = ~[];
let to_child = do sizes.iter().zip(streams.mut_iter()).map |(sz, stream_ref)| { let to_child = sizes.iter().zip(streams.mut_iter()).map(|(sz, stream_ref)| {
let sz = *sz; let sz = *sz;
let stream = util::replace(stream_ref, None); let stream = util::replace(stream_ref, None);
let (from_child_, to_parent_) = stream.unwrap(); let (from_child_, to_parent_) = stream.unwrap();
@ -183,7 +183,7 @@ fn main() {
} }
to_child to_child
}.collect::<~[Chan<~[u8]>]>(); }).collect::<~[Chan<~[u8]>]>();
// latch stores true after we've started // latch stores true after we've started

View file

@ -18,7 +18,7 @@ fn iterate<'a, T>(x: T, f: 'a |&T| -> T) -> Iterate<'a, T> {
Iterate {f: f, next: x} Iterate {f: f, next: x}
} }
struct Iterate<'self, T> { struct Iterate<'self, T> {
priv f: &'self |&T| -> T, priv f: 'self |&T| -> T,
priv next: T priv next: T
} }
impl<'self, T> Iterator<T> for Iterate<'self, T> { impl<'self, T> Iterator<T> for Iterate<'self, T> {

View file

@ -51,9 +51,9 @@ impl Sudoku {
} }
pub fn from_vec(vec: &[[u8, ..9], ..9]) -> Sudoku { pub fn from_vec(vec: &[[u8, ..9], ..9]) -> Sudoku {
let g = do vec::from_fn(9u) |i| { let g = vec::from_fn(9u, |i| {
do vec::from_fn(9u) |j| { vec[i][j] } vec::from_fn(9u, |j| { vec[i][j] })
}; });
return Sudoku::new(g) return Sudoku::new(g)
} }

View file

@ -19,13 +19,13 @@ use std::vec;
fn calc(children: uint, parent_wait_chan: &Chan<Chan<Chan<int>>>) { fn calc(children: uint, parent_wait_chan: &Chan<Chan<Chan<int>>>) {
let wait_ports: ~[Port<Chan<Chan<int>>>] = do vec::from_fn(children) |_| { let wait_ports: ~[Port<Chan<Chan<int>>>] = vec::from_fn(children, |_| {
let (wait_port, wait_chan) = stream::<Chan<Chan<int>>>(); let (wait_port, wait_chan) = stream::<Chan<Chan<int>>>();
do task::spawn { do task::spawn {
calc(children / 2, &wait_chan); calc(children / 2, &wait_chan);
} }
wait_port wait_port
}; });
let child_start_chans: ~[Chan<Chan<int>>] = let child_start_chans: ~[Chan<Chan<int>>] =
wait_ports.move_iter().map(|port| port.recv()).collect(); wait_ports.move_iter().map(|port| port.recv()).collect();
@ -35,11 +35,11 @@ fn calc(children: uint, parent_wait_chan: &Chan<Chan<Chan<int>>>) {
let parent_result_chan: Chan<int> = start_port.recv(); let parent_result_chan: Chan<int> = start_port.recv();
let child_sum_ports: ~[Port<int>] = let child_sum_ports: ~[Port<int>] =
do child_start_chans.move_iter().map |child_start_chan| { child_start_chans.move_iter().map(|child_start_chan| {
let (child_sum_port, child_sum_chan) = stream::<int>(); let (child_sum_port, child_sum_chan) = stream::<int>();
child_start_chan.send(child_sum_chan); child_start_chan.send(child_sum_chan);
child_sum_port child_sum_port
}.collect(); }).collect();
let sum = child_sum_ports.move_iter().fold(0, |sum, sum_port| sum + sum_port.recv() ); let sum = child_sum_ports.move_iter().fold(0, |sum, sum_port| sum + sum_port.recv() );