Skip to content

std::io::util cleanup + fixes #16513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions src/libstd/io/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ impl<R: Reader> Reader for LimitReader<R> {
}

let len = cmp::min(self.limit, buf.len());
self.inner.read(buf.mut_slice_to(len)).map(|len| {
self.limit -= len;
len
})
let res = self.inner.read(buf.mut_slice_to(len));
match res {
Ok(len) => self.limit -= len,
_ => {}
}
res
}
}

Expand All @@ -67,6 +69,8 @@ impl<R: Buffer> Buffer for LimitReader<R> {
}

fn consume(&mut self, amt: uint) {
// Don't let callers reset the limit by passing an overlarge value
let amt = cmp::min(amt, self.limit);
self.limit -= amt;
self.inner.consume(amt);
}
Expand Down Expand Up @@ -97,6 +101,7 @@ impl Buffer for ZeroReader {
static DATA: [u8, ..64] = [0, ..64];
Ok(DATA.as_slice())
}

fn consume(&mut self, _amt: uint) {}
}

Expand All @@ -117,7 +122,10 @@ impl Buffer for NullReader {
fn consume(&mut self, _amt: uint) {}
}

/// A `Writer` which multiplexes writes to a set of `Writers`.
/// A `Writer` which multiplexes writes to a set of `Writer`s.
///
/// The `Writer`s are delegated to in order. If any `Writer` returns an error,
/// that error is returned immediately and remaining `Writer`s are not called.
pub struct MultiWriter {
writers: Vec<Box<Writer>>
}
Expand All @@ -132,24 +140,22 @@ impl MultiWriter {
impl Writer for MultiWriter {
#[inline]
fn write(&mut self, buf: &[u8]) -> io::IoResult<()> {
let mut ret = Ok(());
for writer in self.writers.mut_iter() {
ret = ret.and(writer.write(buf));
try!(writer.write(buf));
}
return ret;
Ok(())
}

#[inline]
fn flush(&mut self) -> io::IoResult<()> {
let mut ret = Ok(());
for writer in self.writers.mut_iter() {
ret = ret.and(writer.flush());
try!(writer.flush());
}
return ret;
Ok(())
}
}

/// A `Reader` which chains input from multiple `Readers`, reading each to
/// A `Reader` which chains input from multiple `Reader`s, reading each to
/// completion before moving onto the next.
pub struct ChainedReader<I, R> {
readers: I,
Expand Down Expand Up @@ -229,17 +235,16 @@ pub fn copy<R: Reader, W: Writer>(r: &mut R, w: &mut W) -> io::IoResult<()> {
}
}

/// A `Reader` which converts an `Iterator<u8>` into a `Reader`.
/// An adaptor converting an `Iterator<u8>` to a `Reader`.
pub struct IterReader<T> {
iter: T,
}

impl<T: Iterator<u8>> IterReader<T> {
/// Create a new `IterReader` which will read from the specified `Iterator`.
/// Creates a new `IterReader` which will read from the specified
/// `Iterator`.
pub fn new(iter: T) -> IterReader<T> {
IterReader {
iter: iter,
}
IterReader { iter: iter }
}
}

Expand All @@ -251,7 +256,7 @@ impl<T: Iterator<u8>> Reader for IterReader<T> {
*slot = elt;
len += 1;
}
if len == 0 {
if len == 0 && buf.len() != 0 {
Err(io::standard_error(io::EndOfFile))
} else {
Ok(len)
Expand Down Expand Up @@ -297,6 +302,14 @@ mod test {
assert_eq!(0, r.limit());
}

#[test]
fn test_limit_reader_overlong_consume() {
let mut r = MemReader::new(vec![0, 1, 2, 3, 4, 5]);
let mut r = LimitReader::new(r.by_ref(), 1);
r.consume(2);
assert_eq!(vec![], r.read_to_end().unwrap());
}

#[test]
fn test_null_writer() {
let mut s = NullWriter;
Expand Down Expand Up @@ -415,4 +428,11 @@ mod test {

assert_eq!(r.read(buf).unwrap_err().kind, io::EndOfFile);
}

#[test]
fn iter_reader_zero_length() {
let mut r = IterReader::new(range(0u8, 8));
let mut buf = [];
assert_eq!(Ok(0), r.read(buf));
}
}