1
Fork 0

Auto merge of #122393 - a1phyr:specialize_read_buf_exact, r=joboet

Specialize many implementations of `Read::read_buf_exact`

This makes all implementations of `Read` that have a specialized `read_exact` implementation also have one for `read_buf_exact`.
This commit is contained in:
bors 2024-04-10 11:38:15 +00:00
commit e908cfd125
5 changed files with 74 additions and 18 deletions

View file

@ -322,6 +322,14 @@ impl<R: ?Sized + Read> Read for BufReader<R> {
crate::io::default_read_exact(self, buf) crate::io::default_read_exact(self, buf)
} }
fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
if self.buf.consume_with(cursor.capacity(), |claimed| cursor.append(claimed)) {
return Ok(());
}
crate::io::default_read_buf_exact(self, cursor)
}
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
let total_len = bufs.iter().map(|b| b.len()).sum::<usize>(); let total_len = bufs.iter().map(|b| b.len()).sum::<usize>();
if self.buf.pos() == self.buf.filled() && total_len >= self.capacity() { if self.buf.pos() == self.buf.filled() && total_len >= self.capacity() {

View file

@ -357,6 +357,13 @@ where
self.pos += n as u64; self.pos += n as u64;
Ok(()) Ok(())
} }
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
let n = cursor.capacity();
Read::read_buf_exact(&mut self.remaining_slice(), cursor)?;
self.pos += n as u64;
Ok(())
}
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]

View file

@ -50,6 +50,10 @@ impl<R: Read + ?Sized> Read for &mut R {
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
(**self).read_exact(buf) (**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")] #[stable(feature = "rust1", since = "1.0.0")]
impl<W: Write + ?Sized> Write for &mut W { 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<()> { fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
(**self).read_exact(buf) (**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")] #[stable(feature = "rust1", since = "1.0.0")]
impl<W: Write + ?Sized> Write for Box<W> { impl<W: Write + ?Sized> Write for Box<W> {
@ -301,6 +309,22 @@ impl Read for &[u8] {
Ok(()) Ok(())
} }
#[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] #[inline]
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
let len = self.len(); let len = self.len();

View file

@ -582,6 +582,29 @@ where
Ok(()) Ok(())
} }
pub(crate) fn default_read_buf_exact<R: Read + ?Sized>(
this: &mut R,
mut cursor: BorrowedCursor<'_>,
) -> Result<()> {
while cursor.capacity() > 0 {
let prev_written = cursor.written();
match this.read_buf(cursor.reborrow()) {
Ok(()) => {}
Err(e) if e.is_interrupted() => continue,
Err(e) => return Err(e),
}
if cursor.written() == prev_written {
return Err(error::const_io_error!(
ErrorKind::UnexpectedEof,
"failed to fill whole buffer"
));
}
}
Ok(())
}
/// The `Read` trait allows for reading bytes from a source. /// The `Read` trait allows for reading bytes from a source.
/// ///
/// Implementors of the `Read` trait are called 'readers'. /// Implementors of the `Read` trait are called 'readers'.
@ -978,24 +1001,8 @@ pub trait Read {
/// ///
/// If this function returns an error, all bytes read will be appended to `cursor`. /// If this function returns an error, all bytes read will be appended to `cursor`.
#[unstable(feature = "read_buf", issue = "78485")] #[unstable(feature = "read_buf", issue = "78485")]
fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> Result<()> { fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<()> {
while cursor.capacity() > 0 { default_read_buf_exact(self, cursor)
let prev_written = cursor.written();
match self.read_buf(cursor.reborrow()) {
Ok(()) => {}
Err(e) if e.is_interrupted() => continue,
Err(e) => return Err(e),
}
if cursor.written() == prev_written {
return Err(error::const_io_error!(
ErrorKind::UnexpectedEof,
"failed to fill whole buffer"
));
}
}
Ok(())
} }
/// Creates a "by reference" adaptor for this instance of `Read`. /// Creates a "by reference" adaptor for this instance of `Read`.

View file

@ -451,6 +451,9 @@ impl Read for Stdin {
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
self.lock().read_exact(buf) self.lock().read_exact(buf)
} }
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
self.lock().read_buf_exact(cursor)
}
} }
#[stable(feature = "read_shared_stdin", since = "1.78.0")] #[stable(feature = "read_shared_stdin", since = "1.78.0")]
@ -477,6 +480,9 @@ impl Read for &Stdin {
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
self.lock().read_exact(buf) self.lock().read_exact(buf)
} }
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
self.lock().read_buf_exact(cursor)
}
} }
// only used by platform-dependent io::copy specializations, i.e. unused on some platforms // only used by platform-dependent io::copy specializations, i.e. unused on some platforms
@ -517,6 +523,10 @@ impl Read for StdinLock<'_> {
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
self.inner.read_exact(buf) self.inner.read_exact(buf)
} }
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
self.inner.read_buf_exact(cursor)
}
} }
impl SpecReadByte for StdinLock<'_> { impl SpecReadByte for StdinLock<'_> {