summaryrefslogtreecommitdiffstats
path: root/library/std/src/io/impls.rs
diff options
context:
space:
mode:
Diffstat (limited to 'library/std/src/io/impls.rs')
-rw-r--r--library/std/src/io/impls.rs56
1 files changed, 55 insertions, 1 deletions
diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs
index e5048dcc8..a7428776d 100644
--- a/library/std/src/io/impls.rs
+++ b/library/std/src/io/impls.rs
@@ -9,6 +9,7 @@ use crate::io::{
self, BorrowedCursor, BufRead, ErrorKind, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write,
};
use crate::mem;
+use crate::str;
// =============================================================================
// Forwarding implementations
@@ -307,6 +308,17 @@ impl Read for &[u8] {
*self = &self[len..];
Ok(len)
}
+
+ #[inline]
+ fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
+ 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();
+ *self = &self[len..];
+ Ok(len)
+ }
}
#[stable(feature = "rust1", since = "1.0.0")]
@@ -336,7 +348,7 @@ impl Write for &mut [u8] {
#[inline]
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
let amt = cmp::min(data.len(), self.len());
- let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
+ let (a, b) = mem::take(self).split_at_mut(amt);
a.copy_from_slice(&data[..amt]);
*self = b;
Ok(amt)
@@ -434,6 +446,33 @@ impl<A: Allocator> Read for VecDeque<u8, A> {
self.drain(..n);
Ok(())
}
+
+ #[inline]
+ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
+ // The total len is known upfront so we can reserve it in a single call.
+ let len = self.len();
+ buf.reserve(len);
+
+ let (front, back) = self.as_slices();
+ buf.extend_from_slice(front);
+ buf.extend_from_slice(back);
+ self.clear();
+ Ok(len)
+ }
+
+ #[inline]
+ fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
+ // We have to use a single contiguous slice because the `VecDequeue` might be split in the
+ // middle of an UTF-8 character.
+ let len = self.len();
+ let content = self.make_contiguous();
+ let string = str::from_utf8(content).map_err(|_| {
+ io::const_io_error!(ErrorKind::InvalidData, "stream did not contain valid UTF-8")
+ })?;
+ buf.push_str(string);
+ self.clear();
+ Ok(len)
+ }
}
/// Write is implemented for `VecDeque<u8>` by appending to the `VecDeque`, growing it as needed.
@@ -446,6 +485,21 @@ impl<A: Allocator> Write for VecDeque<u8, A> {
}
#[inline]
+ fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
+ let len = bufs.iter().map(|b| b.len()).sum();
+ self.reserve(len);
+ for buf in bufs {
+ self.extend(&**buf);
+ }
+ Ok(len)
+ }
+
+ #[inline]
+ fn is_write_vectored(&self) -> bool {
+ true
+ }
+
+ #[inline]
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.extend(buf);
Ok(())