about summary refs log tree commit diff
path: root/library/std/src/io/impls.rs
diff options
context:
space:
mode:
authorBenoît du Garreau <bdgdlm@outlook.com>2024-05-14 16:16:33 +0200
committerBenoît du Garreau <bdgdlm@outlook.com>2024-05-14 16:16:33 +0200
commitcfb04795a1183ca59a4d63060f3443accb9e59bd (patch)
tree4d96c2569fc37487ab4ccf5bacac651e7bc4515d /library/std/src/io/impls.rs
parent31026b7fe3e510a646eddeda838d1f0859f892e7 (diff)
downloadrust-cfb04795a1183ca59a4d63060f3443accb9e59bd.tar.gz
rust-cfb04795a1183ca59a4d63060f3443accb9e59bd.zip
Fix `read_exact` and `read_buf_exact` for `&[u8]` and `io:Cursor`
Diffstat (limited to 'library/std/src/io/impls.rs')
-rw-r--r--library/std/src/io/impls.rs6
1 files changed, 6 insertions, 0 deletions
diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs
index 46f04c7cd39..a8a2e9413e1 100644
--- a/library/std/src/io/impls.rs
+++ b/library/std/src/io/impls.rs
@@ -287,6 +287,9 @@ impl Read for &[u8] {
     #[inline]
     fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
         if buf.len() > self.len() {
+            // `read_exact` makes no promise about the content of `buf` if it
+            // fails so don't bother about that.
+            *self = &self[self.len()..];
             return Err(io::Error::READ_EXACT_EOF);
         }
         let (a, b) = self.split_at(buf.len());
@@ -307,6 +310,9 @@ impl Read for &[u8] {
     #[inline]
     fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
         if cursor.capacity() > self.len() {
+            // Append everything we can to the cursor.
+            cursor.append(*self);
+            *self = &self[self.len()..];
             return Err(io::Error::READ_EXACT_EOF);
         }
         let (a, b) = self.split_at(cursor.capacity());