diff options
| author | Brian Anderson <banderson@mozilla.com> | 2012-05-26 00:32:08 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-05-26 02:28:00 -0700 |
| commit | 432c6cbde93ca525cc33fd17873a0557dffa07c5 (patch) | |
| tree | f3b887343ed4473be3cfc7782bd3715175e316c3 /src/libcore | |
| parent | 5281db2bc273f089a872175f935c01f33dfa4326 (diff) | |
core: Make range follow the for loop protocol
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/int-template.rs | 7 | ||||
| -rw-r--r-- | src/libcore/priv.rs | 6 | ||||
| -rw-r--r-- | src/libcore/rand.rs | 2 | ||||
| -rw-r--r-- | src/libcore/uint-template.rs | 7 | ||||
| -rw-r--r-- | src/libcore/vec.rs | 2 |
5 files changed, 15 insertions, 9 deletions
diff --git a/src/libcore/int-template.rs b/src/libcore/int-template.rs index ac1cc3bbf8b..07c3cc9011e 100644 --- a/src/libcore/int-template.rs +++ b/src/libcore/int-template.rs @@ -36,9 +36,12 @@ pure fn is_nonpositive(x: T) -> bool { x <= 0 as T } pure fn is_nonnegative(x: T) -> bool { x >= 0 as T } #[doc = "Iterate over the range [`lo`..`hi`)"] -fn range(lo: T, hi: T, it: fn(T)) { +fn range(lo: T, hi: T, it: fn(T) -> bool) { let mut i = lo; - while i < hi { it(i); i += 1 as T; } + while i < hi { + if !it(i) { break } + i += 1 as T; + } } #[doc = "Computes the bitwise complement"] diff --git a/src/libcore/priv.rs b/src/libcore/priv.rs index 6fa20ce0804..d43fc27a019 100644 --- a/src/libcore/priv.rs +++ b/src/libcore/priv.rs @@ -127,12 +127,12 @@ fn test_from_global_chan2() unsafe { // Spawn a bunch of tasks that all want to compete to // create the global channel - uint::range(0u, 10u) {|i| + for uint::range(0u, 10u) {|i| task::spawn() {|| let ch = chan_from_global_ptr( globchanp, task::builder) {|po| - uint::range(0u, 10u) {|_j| + for uint::range(0u, 10u) {|_j| let ch = comm::recv(po); comm::send(ch, {i}); } @@ -147,7 +147,7 @@ fn test_from_global_chan2() unsafe { } // There should be only one winner let mut winners = 0u; - uint::range(0u, 10u) {|_i| + for uint::range(0u, 10u) {|_i| let res = comm::recv(resultpo); if res { winners += 1u }; } diff --git a/src/libcore/rand.rs b/src/libcore/rand.rs index dedcf991670..592124767ff 100644 --- a/src/libcore/rand.rs +++ b/src/libcore/rand.rs @@ -202,7 +202,7 @@ impl extensions for rng { fn weighted_vec<T:copy>(v: [weighted<T>]) -> [T] { let mut r = []; for v.each {|item| - uint::range(0u, item.weight) {|_i| + for uint::range(0u, item.weight) {|_i| r += [item.item]; } } diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs index f9bffe3cc0f..e34fb145336 100644 --- a/src/libcore/uint-template.rs +++ b/src/libcore/uint-template.rs @@ -35,9 +35,12 @@ pure fn is_nonpositive(x: T) -> bool { x <= 0 as T } pure fn is_nonnegative(x: T) -> bool { x >= 0 as T } #[doc = "Iterate over the range [`lo`..`hi`)"] -fn range(lo: T, hi: T, it: fn(T)) { +fn range(lo: T, hi: T, it: fn(T) -> bool) { let mut i = lo; - while i < hi { it(i); i += 1 as T; } + while i < hi { + if !it(i) { break } + i += 1 as T; + } } #[doc = "Computes the bitwise complement"] diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index c72cef85cd1..fdc8b1ed248 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -892,7 +892,7 @@ Both vectors must have the same length #[inline] fn iter2<U, T>(v1: [const U], v2: [const T], f: fn(U, T)) { assert len(v1) == len(v2); - uint::range(0u, len(v1)) {|i| + for uint::range(0u, len(v1)) {|i| f(v1[i], v2[i]) } } |
