Skip to content

Commit

Permalink
Auto merge of rust-lang#36777 - tmiasko:chain-read-eof, r=alexcrichton
Browse files Browse the repository at this point in the history
[std::io::Chain] Mark first as done only when reading into non-zero length buffer.

Fixes rust-lang#36771.
  • Loading branch information
bors authored Sep 28, 2016
2 parents d75c84a + 26f9949 commit 4f79716
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ impl<T: Read, U: Read> Read for Chain<T, U> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
if !self.done_first {
match self.first.read(buf)? {
0 => { self.done_first = true; }
0 if buf.len() != 0 => { self.done_first = true; }
n => return Ok(n),
}
}
Expand Down Expand Up @@ -1959,6 +1959,17 @@ mod tests {
cmp_bufread(chain1, chain2, &testdata[..]);
}

#[test]
fn chain_zero_length_read_is_not_eof() {
let a = b"A";
let b = b"B";
let mut s = String::new();
let mut chain = (&a[..]).chain(&b[..]);
chain.read(&mut []).unwrap();
chain.read_to_string(&mut s).unwrap();
assert_eq!("AB", s);
}

#[bench]
fn bench_read_to_end(b: &mut test::Bencher) {
b.iter(|| {
Expand Down

0 comments on commit 4f79716

Please sign in to comment.