From 576a0ba2c478adac866f5fc0fb4124282514d3ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20du=20Garreau?= Date: Thu, 14 Mar 2024 11:20:59 +0100 Subject: [PATCH] `VecDeque::read_to_string`: avoid making the slices contiguous --- library/std/src/io/impls.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs index a95477d5eff8..8ba68bd87369 100644 --- a/library/std/src/io/impls.rs +++ b/library/std/src/io/impls.rs @@ -464,17 +464,8 @@ impl Read for VecDeque { #[inline] fn read_to_string(&mut self, buf: &mut String) -> io::Result { - // 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.try_reserve(len)?; - buf.push_str(string); - self.clear(); - Ok(len) + // SAFETY: We only append to the buffer + unsafe { io::append_to_string(buf, |buf| self.read_to_end(buf)) } } }