From cba6e102ec49d81086a780c12ef3a51f1c2bd340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20du=20Garreau?= Date: Mon, 19 Dec 2022 20:36:21 +0100 Subject: [PATCH] `io::Chain`: specialize some `BufRead` methods --- library/std/src/io/mod.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 23159459a70..3617b956e80 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -2429,9 +2429,7 @@ impl BufRead for Chain { fn fill_buf(&mut self) -> Result<&[u8]> { if !self.done_first { match self.first.fill_buf()? { - buf if buf.is_empty() => { - self.done_first = true; - } + buf if buf.is_empty() => self.done_first = true, buf => return Ok(buf), } } @@ -2441,6 +2439,21 @@ fn fill_buf(&mut self) -> Result<&[u8]> { fn consume(&mut self, amt: usize) { if !self.done_first { self.first.consume(amt) } else { self.second.consume(amt) } } + + fn read_until(&mut self, byte: u8, buf: &mut Vec) -> Result { + let mut read = 0; + if !self.done_first { + let n = self.first.read_until(byte, buf)?; + read += n; + + match buf.last() { + Some(b) if *b == byte && n != 0 => return Ok(read), + _ => self.done_first = true, + } + } + read += self.second.read_until(byte, buf)?; + Ok(read) + } } impl SizeHint for Chain {