From 2290ce14f29450560cd34c219dbc8b6a10283967 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 6 Oct 2013 16:08:56 -0700 Subject: [PATCH] Remove some users of io::file_reader --- src/compiletest/errors.rs | 14 ++++++--- src/compiletest/header.rs | 18 ++++++----- src/libstd/rt/io/buffered.rs | 8 ++--- src/libstd/rt/io/mod.rs | 20 ++++++++++++ src/test/bench/core-std.rs | 7 +++-- src/test/bench/shootout-k-nucleotide-pipes.rs | 31 ++++++++++++------- 6 files changed, 68 insertions(+), 30 deletions(-) diff --git a/src/compiletest/errors.rs b/src/compiletest/errors.rs index 13f4cbf8e212..e49a9701460b 100644 --- a/src/compiletest/errors.rs +++ b/src/compiletest/errors.rs @@ -8,17 +8,21 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::io; - pub struct ExpectedError { line: uint, kind: ~str, msg: ~str } // Load any test directives embedded in the file pub fn load_errors(testfile: &Path) -> ~[ExpectedError] { + use std::rt::io::Open; + use std::rt::io::file::FileInfo; + use std::rt::io::buffered::BufferedReader; + let mut error_patterns = ~[]; - let rdr = io::file_reader(testfile).unwrap(); + let mut rdr = BufferedReader::new(testfile.open_reader(Open).unwrap()); let mut line_num = 1u; - while !rdr.eof() { - let ln = rdr.read_line(); + loop { + let ln = match rdr.read_line() { + Some(ln) => ln, None => break, + }; error_patterns.push_all_move(parse_expected(line_num, ln)); line_num += 1u; } diff --git a/src/compiletest/header.rs b/src/compiletest/header.rs index 36780abc7eef..730df66af23e 100644 --- a/src/compiletest/header.rs +++ b/src/compiletest/header.rs @@ -12,8 +12,6 @@ use common::config; use common; use util; -use std::io; - pub struct TestProps { // Lines that should be expected, in order, on standard out error_patterns: ~[~str], @@ -104,17 +102,23 @@ pub fn is_test_ignored(config: &config, testfile: &Path) -> bool { !val } -fn iter_header(testfile: &Path, it: &fn(~str) -> bool) -> bool { - let rdr = io::file_reader(testfile).unwrap(); - while !rdr.eof() { - let ln = rdr.read_line(); +fn iter_header(testfile: &Path, it: &fn(&str) -> bool) -> bool { + use std::rt::io::Open; + use std::rt::io::file::FileInfo; + use std::rt::io::buffered::BufferedReader; + + let mut rdr = BufferedReader::new(testfile.open_reader(Open).unwrap()); + loop { + let ln = match rdr.read_line() { + Some(ln) => ln, None => break + }; // Assume that any directives will be found before the first // module or function. This doesn't seem to be an optimization // with a warm page cache. Maybe with a cold one. if ln.starts_with("fn") || ln.starts_with("mod") { return true; - } else { if !(it(ln)) { return false; } } + } else { if !(it(ln.trim())) { return false; } } } return true; } diff --git a/src/libstd/rt/io/buffered.rs b/src/libstd/rt/io/buffered.rs index 725477bec41c..9dcb35c806f7 100644 --- a/src/libstd/rt/io/buffered.rs +++ b/src/libstd/rt/io/buffered.rs @@ -89,14 +89,14 @@ impl BufferedReader { /// Reads the next line of input, interpreted as a sequence of utf-8 /// encoded unicode codepoints. If a newline is encountered, then the /// newline is contained in the returned string. - pub fn read_line(&mut self) -> ~str { - str::from_utf8_owned(self.read_until('\n' as u8)) + pub fn read_line(&mut self) -> Option<~str> { + self.read_until('\n' as u8).map(str::from_utf8_owned) } /// Reads a sequence of bytes leading up to a specified delimeter. Once the /// specified byte is encountered, reading ceases and the bytes up to and /// including the delimiter are returned. - pub fn read_until(&mut self, byte: u8) -> ~[u8] { + pub fn read_until(&mut self, byte: u8) -> Option<~[u8]> { let mut res = ~[]; let mut used; loop { @@ -120,7 +120,7 @@ impl BufferedReader { self.pos += used; } self.pos += used; - return res; + return if res.len() == 0 {None} else {Some(res)}; } fn fill_buffer<'a>(&'a mut self) -> &'a [u8] { diff --git a/src/libstd/rt/io/mod.rs b/src/libstd/rt/io/mod.rs index d56ad9ce8483..fa53f3cda259 100644 --- a/src/libstd/rt/io/mod.rs +++ b/src/libstd/rt/io/mod.rs @@ -462,6 +462,16 @@ pub trait Reader { fn eof(&mut self) -> bool; } +impl Reader for ~Reader { + fn read(&mut self, buf: &mut [u8]) -> Option { self.read(buf) } + fn eof(&mut self) -> bool { self.eof() } +} + +impl<'self> Reader for &'self mut Reader { + fn read(&mut self, buf: &mut [u8]) -> Option { self.read(buf) } + fn eof(&mut self) -> bool { self.eof() } +} + pub trait Writer { /// Write the given buffer /// @@ -474,6 +484,16 @@ pub trait Writer { fn flush(&mut self); } +impl Writer for ~Writer { + fn write(&mut self, buf: &[u8]) { self.write(buf) } + fn flush(&mut self) { self.flush() } +} + +impl<'self> Writer for &'self mut Writer { + fn write(&mut self, buf: &[u8]) { self.write(buf) } + fn flush(&mut self) { self.flush() } +} + pub trait Stream: Reader + Writer { } impl Stream for T {} diff --git a/src/test/bench/core-std.rs b/src/test/bench/core-std.rs index 7323dcf4ecb5..5222c4f59b72 100644 --- a/src/test/bench/core-std.rs +++ b/src/test/bench/core-std.rs @@ -15,7 +15,6 @@ extern mod extra; use extra::time::precise_time_s; -use std::io; use std::os; use std::rand::Rng; use std::rand; @@ -70,11 +69,15 @@ fn shift_push() { } fn read_line() { + use std::rt::io::{Reader, Open}; + use std::rt::io::file::FileInfo; + use std::rt::io::buffered::BufferedReader; + let path = Path(env!("CFG_SRC_DIR")) .push_rel(&Path("src/test/bench/shootout-k-nucleotide.data")); for _ in range(0, 3) { - let reader = io::file_reader(&path).unwrap(); + let mut reader = BufferedReader::new(path.open_reader(Open).unwrap()); while !reader.eof() { reader.read_line(); } diff --git a/src/test/bench/shootout-k-nucleotide-pipes.rs b/src/test/bench/shootout-k-nucleotide-pipes.rs index ff7e7192b9f4..445b28b693cf 100644 --- a/src/test/bench/shootout-k-nucleotide-pipes.rs +++ b/src/test/bench/shootout-k-nucleotide-pipes.rs @@ -156,17 +156,21 @@ fn make_sequence_processor(sz: uint, // given a FASTA file on stdin, process sequence THREE fn main() { + use std::rt::io::{Reader, Open}; + use std::rt::io::file::FileInfo; + use std::rt::io::native::stdio; + use std::rt::io::buffered::BufferedReader; + let rdr = if os::getenv("RUST_BENCH").is_some() { - // FIXME: Using this compile-time env variable is a crummy way to - // get to this massive data set, but include_bin! chokes on it (#2598) - let path = Path(env!("CFG_SRC_DIR")) - .push_rel(&Path("src/test/bench/shootout-k-nucleotide.data")); - io::file_reader(&path).unwrap() - } else { - io::stdin() - }; - - + // FIXME: Using this compile-time env variable is a crummy way to + // get to this massive data set, but include_bin! chokes on it (#2598) + let path = Path(env!("CFG_SRC_DIR")) + .push_rel(&Path("src/test/bench/shootout-k-nucleotide.data")); + ~path.open_reader(Open).unwrap() as ~Reader + } else { + ~stdio::stdin() as ~Reader + }; + let mut rdr = BufferedReader::new(rdr); // initialize each sequence sorter let sizes = ~[1u,2,3,4,6,12,18]; @@ -193,8 +197,11 @@ fn main() { // reading the sequence of interest let mut proc_mode = false; - while !rdr.eof() { - let line: ~str = rdr.read_line(); + loop { + let line = match rdr.read_line() { + Some(ln) => ln, None => break, + }; + let line = line.trim().to_owned(); if line.len() == 0u { continue; }