Skip to content

Commit b05aae2

Browse files
committed
test: Use the new for protocol
1 parent cdc266e commit b05aae2

21 files changed

+164
-45
lines changed

src/libcore/core

9.02 MB
Binary file not shown.

src/libcore/iter.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,34 @@ pub fn to_vec<T>(iter: &fn(f: &fn(T) -> bool) -> bool) -> ~[T] {
8686
#[cfg(not(stage0))]
8787
pub fn any<T>(predicate: &fn(T) -> bool,
8888
iter: &fn(f: &fn(T) -> bool) -> bool) -> bool {
89-
// If the predicate returns true, we break. If we ever broke, then we found
90-
// something
91-
!iter(|x| !predicate(x))
89+
for iter |x| {
90+
if predicate(x) {
91+
return true;
92+
}
93+
}
94+
return false;
95+
}
96+
97+
/**
98+
* Return true if `predicate` is true for all values yielded by an internal iterator.
99+
*
100+
* # Example:
101+
*
102+
* ~~~~
103+
* assert!(all(|&x: &uint| x < 6, |f| uint::range(1, 6, f)));
104+
* assert!(!all(|&x: &uint| x < 5, |f| uint::range(1, 6, f)));
105+
* ~~~~
106+
*/
107+
#[inline(always)]
108+
#[cfg(stage0)]
109+
pub fn all<T>(predicate: &fn(T) -> bool,
110+
iter: &fn(f: &fn(T) -> bool)) -> bool {
111+
for iter |x| {
112+
if !predicate(x) {
113+
return false;
114+
}
115+
}
116+
return true;
92117
}
93118

94119
/**

src/libcore/str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3404,7 +3404,7 @@ mod tests {
34043404
let lf = ~"\nMary had a little lamb\nLittle lamb\n";
34053405
let crlf = ~"\r\nMary had a little lamb\r\nLittle lamb\r\n";
34063406

3407-
fn t(s: &str, f: &fn(&str, &fn(&str) -> bool), u: &[~str]) {
3407+
fn t(s: &str, f: &fn(&str, &fn(&str) -> bool) -> bool, u: &[~str]) {
34083408
let mut v = ~[];
34093409
for f(s) |s| { v.push(s.to_owned()) }
34103410
assert!(vec::all2(v, u, |a,b| a == b));
@@ -3424,7 +3424,7 @@ mod tests {
34243424
34253425
#[test]
34263426
fn test_words () {
3427-
fn t(s: &str, f: &fn(&str, &fn(&str) -> bool), u: &[~str]) {
3427+
fn t(s: &str, f: &fn(&str, &fn(&str) -> bool) -> bool, u: &[~str]) {
34283428
let mut v = ~[];
34293429
for f(s) |s| { v.push(s.to_owned()) }
34303430
assert!(vec::all2(v, u, |a,b| a == b));

src/libcore/vec.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,13 +1744,37 @@ pub fn each_permutation<T:Copy>(v: &[T], put: &fn(ts: &[T]) -> bool) -> bool {
17441744
* ~~~
17451745
*
17461746
*/
1747+
#[cfg(stage0)]
17471748
pub fn windowed<'r, T>(n: uint, v: &'r [T], it: &fn(&'r [T]) -> bool) {
17481749
assert!(1u <= n);
17491750
if n > v.len() { return; }
17501751
for uint::range(0, v.len() - n + 1) |i| {
17511752
if !it(v.slice(i, i + n)) { return }
17521753
}
17531754
}
1755+
/**
1756+
* Iterate over all contiguous windows of length `n` of the vector `v`.
1757+
*
1758+
* # Example
1759+
*
1760+
* Print the adjacent pairs of a vector (i.e. `[1,2]`, `[2,3]`, `[3,4]`)
1761+
*
1762+
* ~~~
1763+
* for windowed(2, &[1,2,3,4]) |v| {
1764+
* io::println(fmt!("%?", v));
1765+
* }
1766+
* ~~~
1767+
*
1768+
*/
1769+
#[cfg(not(stage0))]
1770+
pub fn windowed<'r, T>(n: uint, v: &'r [T], it: &fn(&'r [T]) -> bool) -> bool {
1771+
assert!(1u <= n);
1772+
if n > v.len() { return true; }
1773+
for uint::range(0, v.len() - n + 1) |i| {
1774+
if !it(v.slice(i, i + n)) { return false; }
1775+
}
1776+
return true;
1777+
}
17541778

17551779
/**
17561780
* Work with the buffer of a vector.
@@ -4566,7 +4590,7 @@ mod tests {
45664590
}
45674591
i += 0;
45684592
false
4569-
}
4593+
};
45704594
}
45714595

45724596
#[test]
@@ -4581,7 +4605,7 @@ mod tests {
45814605
}
45824606
i += 0;
45834607
false
4584-
}
4608+
};
45854609
}
45864610

45874611
#[test]

src/libstd/dlist.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ pub impl<T> DList<T> {
393393
}
394394
395395
/// Iterate over nodes.
396+
#[cfg(stage0)]
396397
fn each_node(@mut self, f: &fn(@mut DListNode<T>) -> bool) {
397398
let mut link = self.peek_n();
398399
while link.is_some() {
@@ -401,6 +402,17 @@ pub impl<T> DList<T> {
401402
link = nobe.next_link();
402403
}
403404
}
405+
/// Iterate over nodes.
406+
#[cfg(not(stage0))]
407+
fn each_node(@mut self, f: &fn(@mut DListNode<T>) -> bool) -> bool {
408+
let mut link = self.peek_n();
409+
while link.is_some() {
410+
let nobe = link.get();
411+
if !f(nobe) { return false; }
412+
link = nobe.next_link();
413+
}
414+
return true;
415+
}
404416
405417
/// Check data structure integrity. O(n).
406418
fn assert_consistent(@mut self) {

src/libstd/fileinput.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,21 @@ impl FileInput {
256256
(line numbers and file names, see documentation for
257257
`FileInputState`). Otherwise identical to `lines_each`.
258258
*/
259+
#[cfg(stage0)]
259260
pub fn each_line_state(&self,
260261
f: &fn(&str, FileInputState) -> bool) {
261262
self.each_line(|line| f(line, copy self.fi.state));
262263
}
264+
/**
265+
Apply `f` to each line successively, along with some state
266+
(line numbers and file names, see documentation for
267+
`FileInputState`). Otherwise identical to `lines_each`.
268+
*/
269+
#[cfg(not(stage0))]
270+
pub fn each_line_state(&self,
271+
f: &fn(&str, FileInputState) -> bool) -> bool {
272+
self.each_line(|line| f(line, copy self.fi.state))
273+
}
263274

264275

265276
/**
@@ -367,10 +378,22 @@ reading from `stdin`).
367378
368379
Fails when attempting to read from a file that can't be opened.
369380
*/
381+
#[cfg(stage0)]
370382
pub fn input(f: &fn(&str) -> bool) {
371383
let mut i = FileInput::from_args();
372384
i.each_line(f);
373385
}
386+
/**
387+
Iterate directly over the command line arguments (no arguments implies
388+
reading from `stdin`).
389+
390+
Fails when attempting to read from a file that can't be opened.
391+
*/
392+
#[cfg(not(stage0))]
393+
pub fn input(f: &fn(&str) -> bool) -> bool {
394+
let mut i = FileInput::from_args();
395+
i.each_line(f)
396+
}
374397
375398
/**
376399
Iterate directly over the command line arguments (no arguments
@@ -379,32 +402,69 @@ provided at each call.
379402
380403
Fails when attempting to read from a file that can't be opened.
381404
*/
405+
#[cfg(stage0)]
382406
pub fn input_state(f: &fn(&str, FileInputState) -> bool) {
383407
let mut i = FileInput::from_args();
384408
i.each_line_state(f);
385409
}
410+
/**
411+
Iterate directly over the command line arguments (no arguments
412+
implies reading from `stdin`) with the current state of the iteration
413+
provided at each call.
414+
415+
Fails when attempting to read from a file that can't be opened.
416+
*/
417+
#[cfg(not(stage0))]
418+
pub fn input_state(f: &fn(&str, FileInputState) -> bool) -> bool {
419+
let mut i = FileInput::from_args();
420+
i.each_line_state(f)
421+
}
386422
387423
/**
388424
Iterate over a vector of files (an empty vector implies just `stdin`).
389425
390426
Fails when attempting to read from a file that can't be opened.
391427
*/
428+
#[cfg(stage0)]
392429
pub fn input_vec(files: ~[Option<Path>], f: &fn(&str) -> bool) {
393430
let mut i = FileInput::from_vec(files);
394431
i.each_line(f);
395432
}
433+
/**
434+
Iterate over a vector of files (an empty vector implies just `stdin`).
435+
436+
Fails when attempting to read from a file that can't be opened.
437+
*/
438+
#[cfg(not(stage0))]
439+
pub fn input_vec(files: ~[Option<Path>], f: &fn(&str) -> bool) -> bool {
440+
let mut i = FileInput::from_vec(files);
441+
i.each_line(f)
442+
}
396443
397444
/**
398445
Iterate over a vector of files (an empty vector implies just `stdin`)
399446
with the current state of the iteration provided at each call.
400447
401448
Fails when attempting to read from a file that can't be opened.
402449
*/
450+
#[cfg(stage0)]
403451
pub fn input_vec_state(files: ~[Option<Path>],
404452
f: &fn(&str, FileInputState) -> bool) {
405453
let mut i = FileInput::from_vec(files);
406454
i.each_line_state(f);
407455
}
456+
/**
457+
Iterate over a vector of files (an empty vector implies just `stdin`)
458+
with the current state of the iteration provided at each call.
459+
460+
Fails when attempting to read from a file that can't be opened.
461+
*/
462+
#[cfg(not(stage0))]
463+
pub fn input_vec_state(files: ~[Option<Path>],
464+
f: &fn(&str, FileInputState) -> bool) -> bool {
465+
let mut i = FileInput::from_vec(files);
466+
i.each_line_state(f)
467+
}
408468
409469
#[cfg(test)]
410470
mod test {

src/libstd/treemap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> {
674674
a = x.next();
675675
}
676676
}
677-
return a.each(|&x| f(x)) && y.advance(f);
677+
return b.each(|&x| f(x)) && y.advance(f);
678678
}
679679
}
680680

@@ -1326,7 +1326,7 @@ mod test_set {
13261326
}
13271327
13281328
fn check(a: &[int], b: &[int], expected: &[int],
1329-
f: &fn(&TreeSet<int>, &TreeSet<int>, f: &fn(&int) -> bool)) {
1329+
f: &fn(&TreeSet<int>, &TreeSet<int>, f: &fn(&int) -> bool) -> bool) {
13301330
let mut set_a = TreeSet::new();
13311331
let mut set_b = TreeSet::new();
13321332

src/test/bench/graph500-bfs.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
// option. This file may not be copied, modified, or distributed
1111
// except according to those terms.
1212

13-
#[allow(deprecated_mode)];
14-
1513
/*!
1614
1715
An implementation of the Graph500 Breadth First Search problem in Rust.
@@ -23,7 +21,7 @@ use std::arc;
2321
use std::time;
2422
use std::deque::Deque;
2523
use std::par;
26-
use core::hashmap::{HashMap, HashSet};
24+
use core::hashmap::HashSet;
2725
use core::int::abs;
2826
use core::rand::RngUtil;
2927

@@ -83,14 +81,13 @@ fn make_graph(N: uint, edges: ~[(node_id, node_id)]) -> graph {
8381
HashSet::new()
8482
};
8583

86-
do vec::each(edges) |e| {
84+
for vec::each(edges) |e| {
8785
match *e {
8886
(i, j) => {
8987
graph[i].insert(j);
9088
graph[j].insert(i);
9189
}
9290
}
93-
true
9491
}
9592

9693
do vec::map_consume(graph) |mut v| {

src/test/compile-fail/bad-for-loop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
fn main() {
1212
fn baz(_x: &fn(y: int) -> int) {}
1313
for baz |_e| { } //~ ERROR A `for` loop iterator should expect a closure that returns `bool`
14+
//~^ ERROR expected `for` closure to return `bool`
1415
}

src/test/compile-fail/borrowck-lend-flow-loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
fn borrow(_v: &int) {}
1818
fn borrow_mut(_v: &mut int) {}
1919
fn cond() -> bool { fail!() }
20-
fn for_func(_f: &fn() -> bool) { fail!() }
20+
fn for_func(_f: &fn() -> bool) -> bool { fail!() }
2121
fn produce<T>() -> T { fail!(); }
2222

2323
fn inc(v: &mut ~int) {

src/test/compile-fail/issue-2817-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
fn not_bool(f: &fn(int) -> ~str) {}
11+
fn not_bool(f: &fn(int) -> ~str) -> bool {}
1212

1313
fn main() {
1414
for uint::range(0, 100000) |_i| { //~ ERROR A for-loop body must return (), but

src/test/compile-fail/issue-2817.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ fn uuid_random() -> uint { fail!(); }
1616

1717
fn main() {
1818
do uint::range(0, 100000) |_i| { //~ ERROR Do-block body must return bool, but
19-
}
19+
};
2020
// should get a more general message if the callback
2121
// doesn't return nil
2222
do uint::range(0, 100000) |_i| { //~ ERROR mismatched types
2323
~"str"
24-
}
24+
};
2525
}

src/test/compile-fail/issue-3651-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
do 5.times {} //~ ERROR Do-block body must return bool, but returns () here. Perhaps
12+
do 5.times {}; //~ ERROR Do-block body must return bool, but returns () here. Perhaps
1313
}

src/test/compile-fail/issue-3651.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010

1111
fn main() {
1212
for task::spawn { return true; } //~ ERROR A `for` loop iterator should expect a closure that
13+
//~^ ERROR expected `for` closure to return `bool`
1314
}

src/test/compile-fail/private-method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mod kitties {
1818
}
1919

2020
pub impl cat {
21-
priv fn nap(&self) { uint::range(1u, 10000u, |_i| false)}
21+
priv fn nap(&self) { uint::range(1u, 10000u, |_i| false); }
2222
}
2323

2424
pub fn cat(in_x : uint, in_y : int) -> cat {

src/test/run-pass/assignability-trait.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,18 @@
1313
// it.
1414

1515
trait iterable<A> {
16-
fn iterate(&self, blk: &fn(x: &A) -> bool);
16+
fn iterate(&self, blk: &fn(x: &A) -> bool) -> bool;
1717
}
1818

1919
impl<'self,A> iterable<A> for &'self [A] {
20-
fn iterate(&self, f: &fn(x: &A) -> bool) {
21-
for vec::each(*self) |e| {
22-
if !f(e) { break; }
23-
}
20+
fn iterate(&self, f: &fn(x: &A) -> bool) -> bool {
21+
vec::each(*self, f)
2422
}
2523
}
2624

2725
impl<A> iterable<A> for ~[A] {
28-
fn iterate(&self, f: &fn(x: &A) -> bool) {
29-
for vec::each(*self) |e| {
30-
if !f(e) { break; }
31-
}
26+
fn iterate(&self, f: &fn(x: &A) -> bool) -> bool {
27+
vec::each(*self, f)
3228
}
3329
}
3430

0 commit comments

Comments
 (0)