Skip to content

Commit

Permalink
Improve several Read implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr committed Mar 14, 2024
1 parent 9ce37dc commit df0d955
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
22 changes: 22 additions & 0 deletions library/std/src/io/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,28 @@ where
self.pos += n as u64;
Ok(())
}

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
let content = self.remaining_slice();
let len = content.len();
buf.try_reserve(len)?;
buf.extend_from_slice(content);
self.pos += len as u64;

Ok(len)
}

fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
let content = crate::str::from_utf8(self.remaining_slice()).map_err(|_| {
io::const_io_error!(ErrorKind::InvalidData, "stream did not contain valid UTF-8")
})?;
let len = content.len();
buf.try_reserve(len)?;
buf.push_str(content);
self.pos += len as u64;

Ok(len)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
4 changes: 3 additions & 1 deletion library/std/src/io/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,9 @@ impl Read for &[u8] {
let content = str::from_utf8(self).map_err(|_| {
io::const_io_error!(ErrorKind::InvalidData, "stream did not contain valid UTF-8")
})?;
buf.push_str(content);
let len = self.len();
buf.try_reserve(len)?;
buf.push_str(content);
*self = &self[len..];
Ok(len)
}
Expand Down Expand Up @@ -470,6 +471,7 @@ impl<A: Allocator> Read for VecDeque<u8, A> {
let string = str::from_utf8(content).map_err(|_| {
io::const_io_error!(ErrorKind::InvalidData, "stream did not contain valid UTF-8")
})?;
buf.try_reserve(len)?;
buf.push_str(string);
self.clear();
Ok(len)
Expand Down
8 changes: 8 additions & 0 deletions library/std/src/os/unix/net/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,10 @@ impl io::Read for UnixStream {
io::Read::read(&mut &*self, buf)
}

fn read_buf(&mut self, buf: io::BorrowedCursor<'_>) -> io::Result<()> {
io::Read::read_buf(&mut &*self, buf)
}

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
io::Read::read_vectored(&mut &*self, bufs)
}
Expand All @@ -594,6 +598,10 @@ impl<'a> io::Read for &'a UnixStream {
self.0.read(buf)
}

fn read_buf(&mut self, buf: io::BorrowedCursor<'_>) -> io::Result<()> {
self.0.read_buf(buf)
}

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
self.0.read_vectored(bufs)
}
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,10 @@ impl Read for ChildStderr {
fn is_read_vectored(&self) -> bool {
self.inner.is_read_vectored()
}

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
self.inner.read_to_end(buf)
}
}

impl AsInner<AnonPipe> for ChildStderr {
Expand Down

0 comments on commit df0d955

Please sign in to comment.