diff options
| author | Benoît du Garreau <bdgdlm@outlook.com> | 2024-05-14 16:16:33 +0200 |
|---|---|---|
| committer | Benoît du Garreau <bdgdlm@outlook.com> | 2024-05-14 16:16:33 +0200 |
| commit | cfb04795a1183ca59a4d63060f3443accb9e59bd (patch) | |
| tree | 4d96c2569fc37487ab4ccf5bacac651e7bc4515d /library/std/src/io/impls.rs | |
| parent | 31026b7fe3e510a646eddeda838d1f0859f892e7 (diff) | |
| download | rust-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.rs | 6 |
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()); |
