about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBenoît du Garreau <bdgdlm@outlook.com>2024-03-14 11:20:59 +0100
committerBenoît du Garreau <bdgdlm@outlook.com>2024-04-12 09:44:05 +0200
commit23211b638a78bc7b3e69b8e8cdd25abebeb0f7ea (patch)
tree5e11d2b9d0b7514771645aae1bcfaf80e89f9dc0
parentb07c1f7f4d46f083725a03f85a3c6cd3447267ae (diff)
downloadrust-23211b638a78bc7b3e69b8e8cdd25abebeb0f7ea.tar.gz
rust-23211b638a78bc7b3e69b8e8cdd25abebeb0f7ea.zip
`VecDeque::read_to_string`: avoid making the slices contiguous
-rw-r--r--library/std/src/io/impls.rs11
1 files changed, 2 insertions, 9 deletions
diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs
index 9f9ee4af5c8..46f04c7cd39 100644
--- a/library/std/src/io/impls.rs
+++ b/library/std/src/io/impls.rs
@@ -474,15 +474,8 @@ impl<A: Allocator> Read for VecDeque<u8, A> {
 
     #[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::Error::INVALID_UTF8)?;
-        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)) }
     }
 }