diff options
| author | Benoît du Garreau <bdgdlm@outlook.com> | 2024-03-12 18:08:29 +0100 |
|---|---|---|
| committer | Benoît du Garreau <bdgdlm@outlook.com> | 2024-03-12 18:28:55 +0100 |
| commit | 87348093954be36b2583dda2e6e842950d346254 (patch) | |
| tree | 31e17e7696321a9881e892fd58dcbaa2898020a7 /library/std/src/io/impls.rs | |
| parent | 7de1a1f6db26cf7af43cca74819118428e6317ee (diff) | |
| download | rust-87348093954be36b2583dda2e6e842950d346254.tar.gz rust-87348093954be36b2583dda2e6e842950d346254.zip | |
Specialize many implementations of `Read::read_buf_exact`
Diffstat (limited to 'library/std/src/io/impls.rs')
| -rw-r--r-- | library/std/src/io/impls.rs | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs index cb972abd2b8..ee7ed4bcc9a 100644 --- a/library/std/src/io/impls.rs +++ b/library/std/src/io/impls.rs @@ -50,6 +50,10 @@ impl<R: Read + ?Sized> Read for &mut R { fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { (**self).read_exact(buf) } + #[inline] + fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> { + (**self).read_buf_exact(cursor) + } } #[stable(feature = "rust1", since = "1.0.0")] impl<W: Write + ?Sized> Write for &mut W { @@ -154,6 +158,10 @@ impl<R: Read + ?Sized> Read for Box<R> { fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { (**self).read_exact(buf) } + #[inline] + fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> { + (**self).read_buf_exact(cursor) + } } #[stable(feature = "rust1", since = "1.0.0")] impl<W: Write + ?Sized> Write for Box<W> { @@ -302,6 +310,22 @@ impl Read for &[u8] { } #[inline] + fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> { + if cursor.capacity() > self.len() { + return Err(io::const_io_error!( + ErrorKind::UnexpectedEof, + "failed to fill whole buffer" + )); + } + let (a, b) = self.split_at(cursor.capacity()); + + cursor.append(a); + + *self = b; + Ok(()) + } + + #[inline] fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { let len = self.len(); buf.try_reserve(len)?; |
