Skip to content

Commit 2290ce1

Browse files
committed
Remove some users of io::file_reader
1 parent ff95904 commit 2290ce1

File tree

6 files changed

+68
-30
lines changed

6 files changed

+68
-30
lines changed

src/compiletest/errors.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::io;
12-
1311
pub struct ExpectedError { line: uint, kind: ~str, msg: ~str }
1412

1513
// Load any test directives embedded in the file
1614
pub fn load_errors(testfile: &Path) -> ~[ExpectedError] {
15+
use std::rt::io::Open;
16+
use std::rt::io::file::FileInfo;
17+
use std::rt::io::buffered::BufferedReader;
18+
1719
let mut error_patterns = ~[];
18-
let rdr = io::file_reader(testfile).unwrap();
20+
let mut rdr = BufferedReader::new(testfile.open_reader(Open).unwrap());
1921
let mut line_num = 1u;
20-
while !rdr.eof() {
21-
let ln = rdr.read_line();
22+
loop {
23+
let ln = match rdr.read_line() {
24+
Some(ln) => ln, None => break,
25+
};
2226
error_patterns.push_all_move(parse_expected(line_num, ln));
2327
line_num += 1u;
2428
}

src/compiletest/header.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use common::config;
1212
use common;
1313
use util;
1414

15-
use std::io;
16-
1715
pub struct TestProps {
1816
// Lines that should be expected, in order, on standard out
1917
error_patterns: ~[~str],
@@ -104,17 +102,23 @@ pub fn is_test_ignored(config: &config, testfile: &Path) -> bool {
104102
!val
105103
}
106104

107-
fn iter_header(testfile: &Path, it: &fn(~str) -> bool) -> bool {
108-
let rdr = io::file_reader(testfile).unwrap();
109-
while !rdr.eof() {
110-
let ln = rdr.read_line();
105+
fn iter_header(testfile: &Path, it: &fn(&str) -> bool) -> bool {
106+
use std::rt::io::Open;
107+
use std::rt::io::file::FileInfo;
108+
use std::rt::io::buffered::BufferedReader;
109+
110+
let mut rdr = BufferedReader::new(testfile.open_reader(Open).unwrap());
111+
loop {
112+
let ln = match rdr.read_line() {
113+
Some(ln) => ln, None => break
114+
};
111115

112116
// Assume that any directives will be found before the first
113117
// module or function. This doesn't seem to be an optimization
114118
// with a warm page cache. Maybe with a cold one.
115119
if ln.starts_with("fn") || ln.starts_with("mod") {
116120
return true;
117-
} else { if !(it(ln)) { return false; } }
121+
} else { if !(it(ln.trim())) { return false; } }
118122
}
119123
return true;
120124
}

src/libstd/rt/io/buffered.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ impl<R: Reader> BufferedReader<R> {
8989
/// Reads the next line of input, interpreted as a sequence of utf-8
9090
/// encoded unicode codepoints. If a newline is encountered, then the
9191
/// newline is contained in the returned string.
92-
pub fn read_line(&mut self) -> ~str {
93-
str::from_utf8_owned(self.read_until('\n' as u8))
92+
pub fn read_line(&mut self) -> Option<~str> {
93+
self.read_until('\n' as u8).map(str::from_utf8_owned)
9494
}
9595

9696
/// Reads a sequence of bytes leading up to a specified delimeter. Once the
9797
/// specified byte is encountered, reading ceases and the bytes up to and
9898
/// including the delimiter are returned.
99-
pub fn read_until(&mut self, byte: u8) -> ~[u8] {
99+
pub fn read_until(&mut self, byte: u8) -> Option<~[u8]> {
100100
let mut res = ~[];
101101
let mut used;
102102
loop {
@@ -120,7 +120,7 @@ impl<R: Reader> BufferedReader<R> {
120120
self.pos += used;
121121
}
122122
self.pos += used;
123-
return res;
123+
return if res.len() == 0 {None} else {Some(res)};
124124
}
125125

126126
fn fill_buffer<'a>(&'a mut self) -> &'a [u8] {

src/libstd/rt/io/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,16 @@ pub trait Reader {
462462
fn eof(&mut self) -> bool;
463463
}
464464

465+
impl Reader for ~Reader {
466+
fn read(&mut self, buf: &mut [u8]) -> Option<uint> { self.read(buf) }
467+
fn eof(&mut self) -> bool { self.eof() }
468+
}
469+
470+
impl<'self> Reader for &'self mut Reader {
471+
fn read(&mut self, buf: &mut [u8]) -> Option<uint> { self.read(buf) }
472+
fn eof(&mut self) -> bool { self.eof() }
473+
}
474+
465475
pub trait Writer {
466476
/// Write the given buffer
467477
///
@@ -474,6 +484,16 @@ pub trait Writer {
474484
fn flush(&mut self);
475485
}
476486

487+
impl Writer for ~Writer {
488+
fn write(&mut self, buf: &[u8]) { self.write(buf) }
489+
fn flush(&mut self) { self.flush() }
490+
}
491+
492+
impl<'self> Writer for &'self mut Writer {
493+
fn write(&mut self, buf: &[u8]) { self.write(buf) }
494+
fn flush(&mut self) { self.flush() }
495+
}
496+
477497
pub trait Stream: Reader + Writer { }
478498

479499
impl<T: Reader + Writer> Stream for T {}

src/test/bench/core-std.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
extern mod extra;
1616

1717
use extra::time::precise_time_s;
18-
use std::io;
1918
use std::os;
2019
use std::rand::Rng;
2120
use std::rand;
@@ -70,11 +69,15 @@ fn shift_push() {
7069
}
7170

7271
fn read_line() {
72+
use std::rt::io::{Reader, Open};
73+
use std::rt::io::file::FileInfo;
74+
use std::rt::io::buffered::BufferedReader;
75+
7376
let path = Path(env!("CFG_SRC_DIR"))
7477
.push_rel(&Path("src/test/bench/shootout-k-nucleotide.data"));
7578

7679
for _ in range(0, 3) {
77-
let reader = io::file_reader(&path).unwrap();
80+
let mut reader = BufferedReader::new(path.open_reader(Open).unwrap());
7881
while !reader.eof() {
7982
reader.read_line();
8083
}

src/test/bench/shootout-k-nucleotide-pipes.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,21 @@ fn make_sequence_processor(sz: uint,
156156
157157
// given a FASTA file on stdin, process sequence THREE
158158
fn main() {
159-
let rdr = if os::getenv("RUST_BENCH").is_some() {
160-
// FIXME: Using this compile-time env variable is a crummy way to
161-
// get to this massive data set, but include_bin! chokes on it (#2598)
162-
let path = Path(env!("CFG_SRC_DIR"))
163-
.push_rel(&Path("src/test/bench/shootout-k-nucleotide.data"));
164-
io::file_reader(&path).unwrap()
165-
} else {
166-
io::stdin()
167-
};
168-
159+
use std::rt::io::{Reader, Open};
160+
use std::rt::io::file::FileInfo;
161+
use std::rt::io::native::stdio;
162+
use std::rt::io::buffered::BufferedReader;
169163
164+
let rdr = if os::getenv("RUST_BENCH").is_some() {
165+
// FIXME: Using this compile-time env variable is a crummy way to
166+
// get to this massive data set, but include_bin! chokes on it (#2598)
167+
let path = Path(env!("CFG_SRC_DIR"))
168+
.push_rel(&Path("src/test/bench/shootout-k-nucleotide.data"));
169+
~path.open_reader(Open).unwrap() as ~Reader
170+
} else {
171+
~stdio::stdin() as ~Reader
172+
};
173+
let mut rdr = BufferedReader::new(rdr);
170174
171175
// initialize each sequence sorter
172176
let sizes = ~[1u,2,3,4,6,12,18];
@@ -193,8 +197,11 @@ fn main() {
193197
// reading the sequence of interest
194198
let mut proc_mode = false;
195199
196-
while !rdr.eof() {
197-
let line: ~str = rdr.read_line();
200+
loop {
201+
let line = match rdr.read_line() {
202+
Some(ln) => ln, None => break,
203+
};
204+
let line = line.trim().to_owned();
198205
199206
if line.len() == 0u { continue; }
200207

0 commit comments

Comments
 (0)