auto merge of #12106 : qpliu/rust/memreader-fill-eof, r=sfackler

I don't know if anything depends on MemReader::fill returning an empty slice instead of EndOfFile, but I'm pretty sure that MemReader::read_until should not go into an infinite loop.
This commit is contained in:
bors 2014-02-08 21:16:28 -08:00
commit 58985e168b

View File

@ -177,7 +177,11 @@ fn seek(&mut self, _pos: i64, _style: SeekStyle) -> IoResult<()> { fail!() }
impl Buffer for MemReader {
fn fill<'a>(&'a mut self) -> IoResult<&'a [u8]> {
Ok(self.buf.slice_from(self.pos))
if self.pos < self.buf.len() {
Ok(self.buf.slice_from(self.pos))
} else {
Err(io::standard_error(io::EndOfFile))
}
}
fn consume(&mut self, amt: uint) { self.pos += amt; }
}
@ -308,7 +312,11 @@ fn seek(&mut self, _pos: i64, _style: SeekStyle) -> IoResult<()> { fail!() }
impl<'a> Buffer for BufReader<'a> {
fn fill<'a>(&'a mut self) -> IoResult<&'a [u8]> {
Ok(self.buf.slice_from(self.pos))
if self.pos < self.buf.len() {
Ok(self.buf.slice_from(self.pos))
} else {
Err(io::standard_error(io::EndOfFile))
}
}
fn consume(&mut self, amt: uint) { self.pos += amt; }
}
@ -421,6 +429,10 @@ fn test_mem_reader() {
assert_eq!(reader.read(buf), Ok(3));
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!(reader.read(buf).is_err());
}
#[test]
@ -441,6 +453,10 @@ fn test_buf_reader() {
assert_eq!(reader.read(buf), Ok(3));
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!(reader.read(buf).is_err());
}
#[test]