Skip to content

Commit 432c6cb

Browse files
committed
core: Make range follow the for loop protocol
1 parent 5281db2 commit 432c6cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+102
-88
lines changed

src/compiletest/runtest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ fn check_expected_errors(expected_errors: [errors::expected_error],
277277
}
278278
}
279279

280-
uint::range(0u, vec::len(found_flags)) {|i|
280+
for uint::range(0u, vec::len(found_flags)) {|i|
281281
if !found_flags[i] {
282282
let ee = expected_errors[i];
283283
fatal_procres(#fmt["expected %s on line %u not found: %s",

src/libcore/int-template.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ pure fn is_nonpositive(x: T) -> bool { x <= 0 as T }
3636
pure fn is_nonnegative(x: T) -> bool { x >= 0 as T }
3737

3838
#[doc = "Iterate over the range [`lo`..`hi`)"]
39-
fn range(lo: T, hi: T, it: fn(T)) {
39+
fn range(lo: T, hi: T, it: fn(T) -> bool) {
4040
let mut i = lo;
41-
while i < hi { it(i); i += 1 as T; }
41+
while i < hi {
42+
if !it(i) { break }
43+
i += 1 as T;
44+
}
4245
}
4346

4447
#[doc = "Computes the bitwise complement"]

src/libcore/priv.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ fn test_from_global_chan2() unsafe {
127127

128128
// Spawn a bunch of tasks that all want to compete to
129129
// create the global channel
130-
uint::range(0u, 10u) {|i|
130+
for uint::range(0u, 10u) {|i|
131131
task::spawn() {||
132132
let ch = chan_from_global_ptr(
133133
globchanp, task::builder) {|po|
134134

135-
uint::range(0u, 10u) {|_j|
135+
for uint::range(0u, 10u) {|_j|
136136
let ch = comm::recv(po);
137137
comm::send(ch, {i});
138138
}
@@ -147,7 +147,7 @@ fn test_from_global_chan2() unsafe {
147147
}
148148
// There should be only one winner
149149
let mut winners = 0u;
150-
uint::range(0u, 10u) {|_i|
150+
for uint::range(0u, 10u) {|_i|
151151
let res = comm::recv(resultpo);
152152
if res { winners += 1u };
153153
}

src/libcore/rand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl extensions for rng {
202202
fn weighted_vec<T:copy>(v: [weighted<T>]) -> [T] {
203203
let mut r = [];
204204
for v.each {|item|
205-
uint::range(0u, item.weight) {|_i|
205+
for uint::range(0u, item.weight) {|_i|
206206
r += [item.item];
207207
}
208208
}

src/libcore/uint-template.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ pure fn is_nonpositive(x: T) -> bool { x <= 0 as T }
3535
pure fn is_nonnegative(x: T) -> bool { x >= 0 as T }
3636

3737
#[doc = "Iterate over the range [`lo`..`hi`)"]
38-
fn range(lo: T, hi: T, it: fn(T)) {
38+
fn range(lo: T, hi: T, it: fn(T) -> bool) {
3939
let mut i = lo;
40-
while i < hi { it(i); i += 1 as T; }
40+
while i < hi {
41+
if !it(i) { break }
42+
i += 1 as T;
43+
}
4144
}
4245

4346
#[doc = "Computes the bitwise complement"]

src/libcore/vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ Both vectors must have the same length
892892
#[inline]
893893
fn iter2<U, T>(v1: [const U], v2: [const T], f: fn(U, T)) {
894894
assert len(v1) == len(v2);
895-
uint::range(0u, len(v1)) {|i|
895+
for uint::range(0u, len(v1)) {|i|
896896
f(v1[i], v2[i])
897897
}
898898
}

src/librustsyntax/codemap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ fn span_to_lines(sp: span, cm: codemap::codemap) -> @file_lines {
183183
let lo = lookup_char_pos(cm, sp.lo);
184184
let hi = lookup_char_pos(cm, sp.hi);
185185
let mut lines = [];
186-
uint::range(lo.line - 1u, hi.line as uint) {|i| lines += [i]; };
186+
for uint::range(lo.line - 1u, hi.line as uint) {|i| lines += [i]; };
187187
ret @{file: lo.file, lines: lines};
188188
}
189189

src/librustsyntax/ext/qquote.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ fn finish<T: qq_helper>
193193
let qcx = gather_anti_quotes(sp.lo, node);
194194
let cx = qcx;
195195

196-
uint::range(1u, cx.gather.len()) {|i|
196+
for uint::range(1u, cx.gather.len()) {|i|
197197
assert cx.gather[i-1u].lo < cx.gather[i].lo;
198198
// ^^ check that the vector is sorted
199199
assert cx.gather[i-1u].hi <= cx.gather[i].lo;

src/libstd/bitv.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn process(v0: bitv, v1: bitv, op: fn(uint, uint) -> uint) -> bool {
4646
assert (vec::len(v0.storage) == len);
4747
assert (v0.nbits == v1.nbits);
4848
let mut changed = false;
49-
uint::range(0u, len) {|i|
49+
for uint::range(0u, len) {|i|
5050
let w0 = v0.storage[i];
5151
let w1 = v1.storage[i];
5252
let w = op(w0, w1);
@@ -90,7 +90,7 @@ fn assign(v0: bitv, v1: bitv) -> bool {
9090
fn clone(v: bitv) -> bitv {
9191
let storage = vec::to_mut(vec::from_elem(v.nbits / uint_bits + 1u, 0u));
9292
let len = vec::len(v.storage);
93-
uint::range(0u, len) {|i| storage[i] = v.storage[i]; };
93+
for uint::range(0u, len) {|i| storage[i] = v.storage[i]; };
9494
ret @{storage: storage, nbits: v.nbits};
9595
}
9696

@@ -121,17 +121,17 @@ fn equal(v0: bitv, v1: bitv) -> bool {
121121

122122
#[doc = "Set all bits to 0"]
123123
fn clear(v: bitv) {
124-
uint::range(0u, vec::len(v.storage)) {|i| v.storage[i] = 0u; };
124+
for uint::range(0u, vec::len(v.storage)) {|i| v.storage[i] = 0u; };
125125
}
126126

127127
#[doc = "Set all bits to 1"]
128128
fn set_all(v: bitv) {
129-
uint::range(0u, v.nbits) {|i| set(v, i, true); };
129+
for uint::range(0u, v.nbits) {|i| set(v, i, true); };
130130
}
131131

132132
#[doc = "Invert all bits"]
133133
fn invert(v: bitv) {
134-
uint::range(0u, vec::len(v.storage)) {|i|
134+
for uint::range(0u, vec::len(v.storage)) {|i|
135135
v.storage[i] = !v.storage[i];
136136
};
137137
}

src/libstd/rope.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ fn concat(v: [rope]) -> rope {
158158
let mut len = vec::len(v);
159159
if len == 0u { ret node::empty; }
160160
let ropes = vec::to_mut(vec::from_elem(len, v[0]));
161-
uint::range(1u, len) {|i|
161+
for uint::range(1u, len) {|i|
162162
ropes[i] = v[i];
163163
}
164164

165165
//Merge progresively
166166
while len > 1u {
167-
uint::range(0u, len/2u) {|i|
167+
for uint::range(0u, len/2u) {|i|
168168
ropes[i] = append_rope(ropes[2u*i], ropes[2u*i+1u]);
169169
}
170170
if len%2u != 0u {
@@ -1352,19 +1352,19 @@ mod tests {
13521352
fn char_at1() {
13531353
//Generate a large rope
13541354
let mut r = of_str(@ "123456789");
1355-
uint::range(0u, 10u){|_i|
1355+
for uint::range(0u, 10u){|_i|
13561356
r = append_rope(r, r);
13571357
}
13581358

13591359
//Copy it in the slowest possible way
13601360
let mut r2 = empty();
1361-
uint::range(0u, char_len(r)){|i|
1361+
for uint::range(0u, char_len(r)){|i|
13621362
r2 = append_char(r2, char_at(r, i));
13631363
}
13641364
assert eq(r, r2);
13651365

13661366
let mut r3 = empty();
1367-
uint::range(0u, char_len(r)){|i|
1367+
for uint::range(0u, char_len(r)){|i|
13681368
r3 = prepend_char(r3, char_at(r, char_len(r) - i - 1u));
13691369
}
13701370
assert eq(r, r3);
@@ -1385,7 +1385,7 @@ mod tests {
13851385
//Generate a reasonable rope
13861386
let chunk = of_str(@ "123456789");
13871387
let mut r = empty();
1388-
uint::range(0u, 10u){|_i|
1388+
for uint::range(0u, 10u){|_i|
13891389
r = append_rope(r, chunk);
13901390
}
13911391

0 commit comments

Comments
 (0)