diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2013-05-03 16:33:33 -0400 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2013-05-10 19:20:20 -0400 |
| commit | b05aae2d4151a5985d58758fcd46037fb39a5fb9 (patch) | |
| tree | c12b1b3738ade87f372a3388b13c698b1929d639 /src/libstd | |
| parent | cdc266e47d8ee63a1eaf29c775f2cbc5f3a61bb4 (diff) | |
| download | rust-b05aae2d4151a5985d58758fcd46037fb39a5fb9.tar.gz rust-b05aae2d4151a5985d58758fcd46037fb39a5fb9.zip | |
test: Use the new `for` protocol
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/dlist.rs | 12 | ||||
| -rw-r--r-- | src/libstd/fileinput.rs | 60 | ||||
| -rw-r--r-- | src/libstd/treemap.rs | 4 |
3 files changed, 74 insertions, 2 deletions
diff --git a/src/libstd/dlist.rs b/src/libstd/dlist.rs index 93740f31b9b..741bd629680 100644 --- a/src/libstd/dlist.rs +++ b/src/libstd/dlist.rs @@ -393,6 +393,7 @@ pub impl<T> DList<T> { } /// Iterate over nodes. + #[cfg(stage0)] fn each_node(@mut self, f: &fn(@mut DListNode<T>) -> bool) { let mut link = self.peek_n(); while link.is_some() { @@ -401,6 +402,17 @@ pub impl<T> DList<T> { link = nobe.next_link(); } } + /// Iterate over nodes. + #[cfg(not(stage0))] + fn each_node(@mut self, f: &fn(@mut DListNode<T>) -> bool) -> bool { + let mut link = self.peek_n(); + while link.is_some() { + let nobe = link.get(); + if !f(nobe) { return false; } + link = nobe.next_link(); + } + return true; + } /// Check data structure integrity. O(n). fn assert_consistent(@mut self) { diff --git a/src/libstd/fileinput.rs b/src/libstd/fileinput.rs index 90774d19595..2c5cbc1cbf9 100644 --- a/src/libstd/fileinput.rs +++ b/src/libstd/fileinput.rs @@ -256,10 +256,21 @@ impl FileInput { (line numbers and file names, see documentation for `FileInputState`). Otherwise identical to `lines_each`. */ + #[cfg(stage0)] pub fn each_line_state(&self, f: &fn(&str, FileInputState) -> bool) { self.each_line(|line| f(line, copy self.fi.state)); } + /** + Apply `f` to each line successively, along with some state + (line numbers and file names, see documentation for + `FileInputState`). Otherwise identical to `lines_each`. + */ + #[cfg(not(stage0))] + pub fn each_line_state(&self, + f: &fn(&str, FileInputState) -> bool) -> bool { + self.each_line(|line| f(line, copy self.fi.state)) + } /** @@ -367,10 +378,22 @@ reading from `stdin`). Fails when attempting to read from a file that can't be opened. */ +#[cfg(stage0)] pub fn input(f: &fn(&str) -> bool) { let mut i = FileInput::from_args(); i.each_line(f); } +/** +Iterate directly over the command line arguments (no arguments implies +reading from `stdin`). + +Fails when attempting to read from a file that can't be opened. +*/ +#[cfg(not(stage0))] +pub fn input(f: &fn(&str) -> bool) -> bool { + let mut i = FileInput::from_args(); + i.each_line(f) +} /** Iterate directly over the command line arguments (no arguments @@ -379,20 +402,44 @@ provided at each call. Fails when attempting to read from a file that can't be opened. */ +#[cfg(stage0)] pub fn input_state(f: &fn(&str, FileInputState) -> bool) { let mut i = FileInput::from_args(); i.each_line_state(f); } +/** +Iterate directly over the command line arguments (no arguments +implies reading from `stdin`) with the current state of the iteration +provided at each call. + +Fails when attempting to read from a file that can't be opened. +*/ +#[cfg(not(stage0))] +pub fn input_state(f: &fn(&str, FileInputState) -> bool) -> bool { + let mut i = FileInput::from_args(); + i.each_line_state(f) +} /** Iterate over a vector of files (an empty vector implies just `stdin`). Fails when attempting to read from a file that can't be opened. */ +#[cfg(stage0)] pub fn input_vec(files: ~[Option<Path>], f: &fn(&str) -> bool) { let mut i = FileInput::from_vec(files); i.each_line(f); } +/** +Iterate over a vector of files (an empty vector implies just `stdin`). + +Fails when attempting to read from a file that can't be opened. +*/ +#[cfg(not(stage0))] +pub fn input_vec(files: ~[Option<Path>], f: &fn(&str) -> bool) -> bool { + let mut i = FileInput::from_vec(files); + i.each_line(f) +} /** Iterate over a vector of files (an empty vector implies just `stdin`) @@ -400,11 +447,24 @@ with the current state of the iteration provided at each call. Fails when attempting to read from a file that can't be opened. */ +#[cfg(stage0)] pub fn input_vec_state(files: ~[Option<Path>], f: &fn(&str, FileInputState) -> bool) { let mut i = FileInput::from_vec(files); i.each_line_state(f); } +/** +Iterate over a vector of files (an empty vector implies just `stdin`) +with the current state of the iteration provided at each call. + +Fails when attempting to read from a file that can't be opened. +*/ +#[cfg(not(stage0))] +pub fn input_vec_state(files: ~[Option<Path>], + f: &fn(&str, FileInputState) -> bool) -> bool { + let mut i = FileInput::from_vec(files); + i.each_line_state(f) +} #[cfg(test)] mod test { diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index d68b08dc475..252bb1a6af8 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -674,7 +674,7 @@ impl<T: TotalOrd> Set<T> for TreeSet<T> { a = x.next(); } } - return a.each(|&x| f(x)) && y.advance(f); + return b.each(|&x| f(x)) && y.advance(f); } } @@ -1326,7 +1326,7 @@ mod test_set { } fn check(a: &[int], b: &[int], expected: &[int], - f: &fn(&TreeSet<int>, &TreeSet<int>, f: &fn(&int) -> bool)) { + f: &fn(&TreeSet<int>, &TreeSet<int>, f: &fn(&int) -> bool) -> bool) { let mut set_a = TreeSet::new(); let mut set_b = TreeSet::new(); |
