Skip to content

Commit 7e790d3

Browse files
committed
VecDeque::read_to_string: avoid making the slices contiguous
1 parent df0d955 commit 7e790d3

File tree

1 file changed

+2
-11
lines changed

1 file changed

+2
-11
lines changed

library/std/src/io/impls.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -464,17 +464,8 @@ impl<A: Allocator> Read for VecDeque<u8, A> {
464464

465465
#[inline]
466466
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
467-
// We have to use a single contiguous slice because the `VecDequeue` might be split in the
468-
// middle of an UTF-8 character.
469-
let len = self.len();
470-
let content = self.make_contiguous();
471-
let string = str::from_utf8(content).map_err(|_| {
472-
io::const_io_error!(ErrorKind::InvalidData, "stream did not contain valid UTF-8")
473-
})?;
474-
buf.try_reserve(len)?;
475-
buf.push_str(string);
476-
self.clear();
477-
Ok(len)
467+
// SAFETY: We only append to the buffer
468+
unsafe { io::append_to_string(buf, |buf| self.read_to_end(buf)) }
478469
}
479470
}
480471

0 commit comments

Comments
 (0)