Skip to content

Commit

Permalink
De-~[] Reader and Writer
Browse files Browse the repository at this point in the history
There's a little more allocation here and there now since
from_utf8_owned can't be used with Vec.
  • Loading branch information
sfackler committed Apr 6, 2014
1 parent 94a055c commit d0e60b7
Show file tree
Hide file tree
Showing 27 changed files with 117 additions and 108 deletions.
4 changes: 2 additions & 2 deletions src/compiletest/procsrv.rs
Expand Up @@ -84,8 +84,8 @@ pub fn run(lib_path: &str,

Some(Result {
status: status,
out: str::from_utf8_owned(output).unwrap(),
err: str::from_utf8_owned(error).unwrap()
out: str::from_utf8(output.as_slice()).unwrap().to_owned(),
err: str::from_utf8(error.as_slice()).unwrap().to_owned()
})
},
Err(..) => None
Expand Down
6 changes: 3 additions & 3 deletions src/compiletest/runtest.rs
Expand Up @@ -153,7 +153,7 @@ fn run_pretty_test(config: &config, props: &TestProps, testfile: &Path) {
match props.pp_exact { Some(_) => 1, None => 2 };

let src = File::open(testfile).read_to_end().unwrap();
let src = str::from_utf8_owned(src).unwrap();
let src = str::from_utf8(src.as_slice()).unwrap().to_owned();
let mut srcs = vec!(src);

let mut round = 0;
Expand All @@ -177,7 +177,7 @@ fn run_pretty_test(config: &config, props: &TestProps, testfile: &Path) {
Some(ref file) => {
let filepath = testfile.dir_path().join(file);
let s = File::open(&filepath).read_to_end().unwrap();
str::from_utf8_owned(s).unwrap()
str::from_utf8(s.as_slice()).unwrap().to_owned()
}
None => { (*srcs.get(srcs.len() - 2u)).clone() }
};
Expand Down Expand Up @@ -1163,7 +1163,7 @@ fn disassemble_extract(config: &config, _props: &TestProps,

fn count_extracted_lines(p: &Path) -> uint {
let x = File::open(&p.with_extension("ll")).read_to_end().unwrap();
let x = str::from_utf8_owned(x).unwrap();
let x = str::from_utf8(x.as_slice()).unwrap();
x.lines().len()
}

Expand Down
8 changes: 5 additions & 3 deletions src/librustc/back/archive.rs
Expand Up @@ -59,8 +59,10 @@ fn run_ar(sess: &Session, args: &str, cwd: Option<&Path>,
if !o.status.success() {
sess.err(format!("{} {} failed with: {}", ar, args.connect(" "),
o.status));
sess.note(format!("stdout ---\n{}", str::from_utf8(o.output).unwrap()));
sess.note(format!("stderr ---\n{}", str::from_utf8(o.error).unwrap()));
sess.note(format!("stdout ---\n{}",
str::from_utf8(o.output.as_slice()).unwrap()));
sess.note(format!("stderr ---\n{}",
str::from_utf8(o.error.as_slice()).unwrap()));
sess.abort_if_errors();
}
o
Expand Down Expand Up @@ -129,7 +131,7 @@ impl<'a> Archive<'a> {
/// Lists all files in an archive
pub fn files(&self) -> Vec<~str> {
let output = run_ar(self.sess, "t", None, [&self.dst]);
let output = str::from_utf8(output.output).unwrap();
let output = str::from_utf8(output.output.as_slice()).unwrap();
// use lines_any because windows delimits output with `\r\n` instead of
// just `\n`
output.lines_any().map(|s| s.to_owned()).collect()
Expand Down
11 changes: 8 additions & 3 deletions src/librustc/back/link.rs
Expand Up @@ -337,7 +337,9 @@ pub mod write {
if !prog.status.success() {
sess.err(format!("linking with `{}` failed: {}", cc, prog.status));
sess.note(format!("{} arguments: '{}'", cc, args.connect("' '")));
sess.note(str::from_utf8_owned(prog.error + prog.output).unwrap());
let mut note = prog.error.clone();
note.push_all(prog.output.as_slice());
sess.note(str::from_utf8(note.as_slice()).unwrap().to_owned());
sess.abort_if_errors();
}
},
Expand Down Expand Up @@ -929,7 +931,8 @@ fn link_rlib<'a>(sess: &'a Session,
let bc = obj_filename.with_extension("bc");
let bc_deflated = obj_filename.with_extension("bc.deflate");
match fs::File::open(&bc).read_to_end().and_then(|data| {
fs::File::create(&bc_deflated).write(flate::deflate_bytes(data).as_slice())
fs::File::create(&bc_deflated)
.write(flate::deflate_bytes(data.as_slice()).as_slice())
}) {
Ok(()) => {}
Err(e) => {
Expand Down Expand Up @@ -1025,7 +1028,9 @@ fn link_natively(sess: &Session, dylib: bool, obj_filename: &Path,
if !prog.status.success() {
sess.err(format!("linking with `{}` failed: {}", cc_prog, prog.status));
sess.note(format!("{} arguments: '{}'", cc_prog, cc_args.connect("' '")));
sess.note(str::from_utf8_owned(prog.error + prog.output).unwrap());
let mut output = prog.error.clone();
output.push_all(prog.output.as_slice());
sess.note(str::from_utf8(output.as_slice()).unwrap().to_owned());
sess.abort_if_errors();
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lib.rs
Expand Up @@ -272,7 +272,7 @@ pub fn run_compiler(args: &[~str]) {
let ifile = matches.free.get(0).as_slice();
if ifile == "-" {
let contents = io::stdin().read_to_end().unwrap();
let src = str::from_utf8_owned(contents).unwrap();
let src = str::from_utf8(contents.as_slice()).unwrap().to_owned();
(d::StrInput(src), None)
} else {
(d::FileInput(Path::new(ifile)), Some(Path::new(ifile)))
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render.rs
Expand Up @@ -487,7 +487,7 @@ impl<'a> SourceCollector<'a> {
filename.ends_with("macros>") => return Ok(()),
Err(e) => return Err(e)
};
let contents = str::from_utf8_owned(contents).unwrap();
let contents = str::from_utf8(contents.as_slice()).unwrap();

// Remove the utf-8 BOM if any
let contents = if contents.starts_with("\ufeff") {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/markdown.rs
Expand Up @@ -22,7 +22,7 @@ use test::Collector;
fn load_string(input: &Path) -> io::IoResult<Option<~str>> {
let mut f = try!(io::File::open(input));
let d = try!(f.read_to_end());
Ok(str::from_utf8_owned(d))
Ok(str::from_utf8(d.as_slice()).map(|s| s.to_owned()))
}
macro_rules! load_or_return {
($input: expr, $cant_read: expr, $not_utf8: expr) => {
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/test.rs
Expand Up @@ -159,7 +159,8 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool,
if should_fail && out.status.success() {
fail!("test executable succeeded when it should have failed");
} else if !should_fail && !out.status.success() {
fail!("test executable failed:\n{}", str::from_utf8(out.error));
fail!("test executable failed:\n{}",
str::from_utf8(out.error.as_slice()));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/libserialize/json.rs
Expand Up @@ -1282,8 +1282,8 @@ pub fn from_reader(rdr: &mut io::Reader) -> DecodeResult<Json> {
Ok(c) => c,
Err(e) => return Err(IoError(e))
};
let s = match str::from_utf8_owned(contents) {
Some(s) => s,
let s = match str::from_utf8(contents.as_slice()) {
Some(s) => s.to_owned(),
None => return Err(ParseError(~"contents not utf-8", 0, 0))
};
let mut parser = Parser::new(s.chars());
Expand Down
8 changes: 4 additions & 4 deletions src/libstd/io/buffered.rs
Expand Up @@ -504,10 +504,10 @@ mod test {
fn test_read_until() {
let inner = MemReader::new(~[0, 1, 2, 1, 0]);
let mut reader = BufferedReader::with_capacity(2, inner);
assert_eq!(reader.read_until(0), Ok(~[0]));
assert_eq!(reader.read_until(2), Ok(~[1, 2]));
assert_eq!(reader.read_until(1), Ok(~[1]));
assert_eq!(reader.read_until(8), Ok(~[0]));
assert_eq!(reader.read_until(0), Ok(vec!(0)));
assert_eq!(reader.read_until(2), Ok(vec!(1, 2)));
assert_eq!(reader.read_until(1), Ok(vec!(1)));
assert_eq!(reader.read_until(8), Ok(vec!(0)));
assert!(reader.read_until(9).is_err());
}

Expand Down
24 changes: 12 additions & 12 deletions src/libstd/io/extensions.rs
Expand Up @@ -323,7 +323,7 @@ mod test {
fn read_bytes() {
let mut reader = MemReader::new(~[10, 11, 12, 13]);
let bytes = reader.read_exact(4).unwrap();
assert!(bytes == ~[10, 11, 12, 13]);
assert!(bytes == vec!(10, 11, 12, 13));
}

#[test]
Expand All @@ -332,7 +332,7 @@ mod test {
count: 0,
};
let bytes = reader.read_exact(4).unwrap();
assert!(bytes == ~[10, 11, 12, 13]);
assert!(bytes == vec!(10, 11, 12, 13));
}

#[test]
Expand All @@ -344,37 +344,37 @@ mod test {
#[test]
fn push_exact() {
let mut reader = MemReader::new(~[10, 11, 12, 13]);
let mut buf = ~[8, 9];
let mut buf = vec!(8, 9);
reader.push_exact(&mut buf, 4).unwrap();
assert!(buf == ~[8, 9, 10, 11, 12, 13]);
assert!(buf == vec!(8, 9, 10, 11, 12, 13));
}

#[test]
fn push_exact_partial() {
let mut reader = PartialReader {
count: 0,
};
let mut buf = ~[8, 9];
let mut buf = vec!(8, 9);
reader.push_exact(&mut buf, 4).unwrap();
assert!(buf == ~[8, 9, 10, 11, 12, 13]);
assert!(buf == vec!(8, 9, 10, 11, 12, 13));
}

#[test]
fn push_exact_eof() {
let mut reader = MemReader::new(~[10, 11]);
let mut buf = ~[8, 9];
let mut buf = vec!(8, 9);
assert!(reader.push_exact(&mut buf, 4).is_err());
assert!(buf == ~[8, 9, 10, 11]);
assert!(buf == vec!(8, 9, 10, 11));
}

#[test]
fn push_exact_error() {
let mut reader = ErroringLaterReader {
count: 0,
};
let mut buf = ~[8, 9];
let mut buf = vec!(8, 9);
assert!(reader.push_exact(&mut buf, 4).is_err());
assert!(buf == ~[8, 9, 10]);
assert!(buf == vec!(8, 9, 10));
}

#[test]
Expand All @@ -383,7 +383,7 @@ mod test {
count: 0,
};
let buf = reader.read_to_end().unwrap();
assert!(buf == ~[10, 11, 12, 13]);
assert!(buf == vec!(10, 11, 12, 13));
}

#[test]
Expand All @@ -393,7 +393,7 @@ mod test {
count: 0,
};
let buf = reader.read_to_end().unwrap();
assert!(buf == ~[10, 11]);
assert!(buf == vec!(10, 11));
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions src/libstd/io/fs.rs
Expand Up @@ -1074,7 +1074,7 @@ mod test {
check!(copy(&input, &output));

assert_eq!(check!(File::open(&output).read_to_end()),
(bytes!("foo")).to_owned());
(Vec::from_slice(bytes!("foo"))));
})

iotest!(fn copy_file_src_dir() {
Expand Down Expand Up @@ -1114,7 +1114,7 @@ mod test {
}
assert_eq!(check!(stat(&out)).size, check!(stat(&input)).size);
assert_eq!(check!(File::open(&out).read_to_end()),
(bytes!("foobar")).to_owned());
(Vec::from_slice(bytes!("foobar"))));
})

#[cfg(not(windows))] // apparently windows doesn't like symlinks
Expand Down Expand Up @@ -1146,7 +1146,7 @@ mod test {
}
assert_eq!(check!(stat(&out)).size, check!(stat(&input)).size);
assert_eq!(check!(File::open(&out).read_to_end()),
(bytes!("foobar")).to_owned());
(Vec::from_slice(bytes!("foobar"))));

// can't link to yourself
match link(&input, &input) {
Expand Down Expand Up @@ -1206,7 +1206,7 @@ mod test {
check!(file.fsync());
assert_eq!(check!(stat(&path)).size, 10);
assert_eq!(check!(File::open(&path).read_to_end()),
(bytes!("foobar", 0, 0, 0, 0)).to_owned());
(Vec::from_slice(bytes!("foobar", 0, 0, 0, 0))));

// Truncate to a smaller length, don't seek, and then write something.
// Ensure that the intermediate zeroes are all filled in (we're seeked
Expand All @@ -1217,7 +1217,7 @@ mod test {
check!(file.fsync());
assert_eq!(check!(stat(&path)).size, 9);
assert_eq!(check!(File::open(&path).read_to_end()),
(bytes!("fo", 0, 0, 0, 0, "wut")).to_owned());
(Vec::from_slice(bytes!("fo", 0, 0, 0, 0, "wut"))));
drop(file);
})

Expand Down
12 changes: 6 additions & 6 deletions src/libstd/io/mem.rs
Expand Up @@ -129,7 +129,7 @@ impl Seek for MemWriter {
///
/// let mut r = MemReader::new(~[0, 1, 2]);
///
/// assert_eq!(r.read_to_end().unwrap(), ~[0, 1, 2]);
/// assert_eq!(r.read_to_end().unwrap(), vec!(0, 1, 2));
/// ```
pub struct MemReader {
buf: ~[u8],
Expand Down Expand Up @@ -272,7 +272,7 @@ impl<'a> Seek for BufWriter<'a> {
/// let mut buf = [0, 1, 2, 3];
/// let mut r = BufReader::new(buf);
///
/// assert_eq!(r.read_to_end().unwrap(), ~[0, 1, 2, 3]);
/// assert_eq!(r.read_to_end().unwrap(), vec!(0, 1, 2, 3));
/// ```
pub struct BufReader<'a> {
buf: &'a [u8],
Expand Down Expand Up @@ -441,8 +441,8 @@ mod test {
assert_eq!(buf.slice(0, 3), &[5, 6, 7]);
assert!(reader.read(buf).is_err());
let mut reader = MemReader::new(~[0, 1, 2, 3, 4, 5, 6, 7]);
assert_eq!(reader.read_until(3).unwrap(), ~[0, 1, 2, 3]);
assert_eq!(reader.read_until(3).unwrap(), ~[4, 5, 6, 7]);
assert_eq!(reader.read_until(3).unwrap(), vec!(0, 1, 2, 3));
assert_eq!(reader.read_until(3).unwrap(), vec!(4, 5, 6, 7));
assert!(reader.read(buf).is_err());
}

Expand All @@ -465,8 +465,8 @@ mod test {
assert_eq!(buf.slice(0, 3), &[5, 6, 7]);
assert!(reader.read(buf).is_err());
let mut reader = BufReader::new(in_buf);
assert_eq!(reader.read_until(3).unwrap(), ~[0, 1, 2, 3]);
assert_eq!(reader.read_until(3).unwrap(), ~[4, 5, 6, 7]);
assert_eq!(reader.read_until(3).unwrap(), vec!(0, 1, 2, 3));
assert_eq!(reader.read_until(3).unwrap(), vec!(4, 5, 6, 7));
assert!(reader.read(buf).is_err());
}

Expand Down
28 changes: 14 additions & 14 deletions src/libstd/io/mod.rs
Expand Up @@ -225,8 +225,8 @@ use str::{StrSlice, OwnedStr};
use str;
use uint;
use unstable::finally::try_finally;
use slice::{OwnedVector, MutableVector, ImmutableVector, OwnedCloneableVector};
use slice;
use slice::{Vector, OwnedVector, MutableVector, ImmutableVector, OwnedCloneableVector};
use vec::Vec;

// Reexports
pub use self::stdio::stdin;
Expand Down Expand Up @@ -486,9 +486,9 @@ pub trait Reader {
/// or EOF. If `Ok(())` is returned, then all of the requested bytes were
/// pushed on to the vector, otherwise the amount `len` bytes couldn't be
/// read (an error was encountered), and the error is returned.
fn push_exact(&mut self, buf: &mut ~[u8], len: uint) -> IoResult<()> {
fn push_exact(&mut self, buf: &mut Vec<u8>, len: uint) -> IoResult<()> {
struct State<'a> {
buf: &'a mut ~[u8],
buf: &'a mut Vec<u8>,
total_read: uint
}

Expand Down Expand Up @@ -526,8 +526,8 @@ pub trait Reader {
/// have already been consumed from the underlying reader, and they are lost
/// (not returned as part of the error). If this is unacceptable, then it is
/// recommended to use the `push_exact` or `read` methods.
fn read_exact(&mut self, len: uint) -> IoResult<~[u8]> {
let mut buf = slice::with_capacity(len);
fn read_exact(&mut self, len: uint) -> IoResult<Vec<u8>> {
let mut buf = Vec::with_capacity(len);
match self.push_exact(&mut buf, len) {
Ok(()) => Ok(buf),
Err(e) => Err(e),
Expand All @@ -542,8 +542,8 @@ pub trait Reader {
/// discarded when an error is returned.
///
/// When EOF is encountered, all bytes read up to that point are returned.
fn read_to_end(&mut self) -> IoResult<~[u8]> {
let mut buf = slice::with_capacity(DEFAULT_BUF_SIZE);
fn read_to_end(&mut self) -> IoResult<Vec<u8>> {
let mut buf = Vec::with_capacity(DEFAULT_BUF_SIZE);
loop {
match self.push_exact(&mut buf, DEFAULT_BUF_SIZE) {
Ok(()) => {}
Expand All @@ -564,8 +564,8 @@ pub trait Reader {
/// UTF-8 bytes.
fn read_to_str(&mut self) -> IoResult<~str> {
self.read_to_end().and_then(|s| {
match str::from_utf8_owned(s) {
Some(s) => Ok(s),
match str::from_utf8(s.as_slice()) {
Some(s) => Ok(s.to_owned()),
None => Err(standard_error(InvalidInput)),
}
})
Expand Down Expand Up @@ -1198,8 +1198,8 @@ pub trait Buffer: Reader {
/// valid UTF-8 sequence of bytes.
fn read_line(&mut self) -> IoResult<~str> {
self.read_until('\n' as u8).and_then(|line|
match str::from_utf8_owned(line) {
Some(s) => Ok(s),
match str::from_utf8(line.as_slice()) {
Some(s) => Ok(s.to_owned()),
None => Err(standard_error(InvalidInput)),
}
)
Expand Down Expand Up @@ -1230,8 +1230,8 @@ pub trait Buffer: Reader {
/// have been read, otherwise the pending byte buffer is returned. This
/// is the reason that the byte buffer returned may not always contain the
/// delimiter.
fn read_until(&mut self, byte: u8) -> IoResult<~[u8]> {
let mut res = ~[];
fn read_until(&mut self, byte: u8) -> IoResult<Vec<u8>> {
let mut res = Vec::new();

let mut used;
loop {
Expand Down

0 comments on commit d0e60b7

Please sign in to comment.