From b1c0672284bd33a3c313976685c92c085c9c51a6 Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Wed, 26 Jan 2022 19:37:00 -0800 Subject: [PATCH 1/3] add readbufref --- library/std/src/fs.rs | 6 +- library/std/src/io/buffered/bufreader.rs | 8 +- library/std/src/io/buffered/tests.rs | 12 +- library/std/src/io/copy.rs | 4 +- library/std/src/io/cursor.rs | 6 +- library/std/src/io/impls.rs | 8 +- library/std/src/io/mod.rs | 18 +-- library/std/src/io/readbuf.rs | 169 +++++++++++++++++++++++ library/std/src/io/tests.rs | 10 +- library/std/src/io/util.rs | 6 +- library/std/src/io/util/tests.rs | 8 +- library/std/src/sys/hermit/fs.rs | 4 +- library/std/src/sys/solid/fs.rs | 2 +- library/std/src/sys/unix/fd.rs | 4 +- library/std/src/sys/unix/fs.rs | 4 +- library/std/src/sys/unsupported/fs.rs | 4 +- library/std/src/sys/wasi/fs.rs | 4 +- library/std/src/sys/windows/fs.rs | 4 +- library/std/src/sys/windows/handle.rs | 4 +- 19 files changed, 227 insertions(+), 58 deletions(-) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 0b65336a5a7da..b63d6734c9162 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -13,7 +13,7 @@ mod tests; use crate::ffi::OsString; use crate::fmt; -use crate::io::{self, IoSlice, IoSliceMut, Read, ReadBuf, Seek, SeekFrom, Write}; +use crate::io::{self, IoSlice, IoSliceMut, Read, ReadBufRef, Seek, SeekFrom, Write}; use crate::path::{Path, PathBuf}; use crate::sys::fs as fs_imp; use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner}; @@ -624,7 +624,7 @@ impl Read for File { self.inner.read_vectored(bufs) } - fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + fn read_buf(&mut self, buf: ReadBufRef<'_, '_>) -> io::Result<()> { self.inner.read_buf(buf) } @@ -676,7 +676,7 @@ impl Read for &File { self.inner.read(buf) } - fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + fn read_buf(&mut self, buf: ReadBufRef<'_, '_>) -> io::Result<()> { self.inner.read_buf(buf) } diff --git a/library/std/src/io/buffered/bufreader.rs b/library/std/src/io/buffered/bufreader.rs index e7eee4436249b..b366bfffc1633 100644 --- a/library/std/src/io/buffered/bufreader.rs +++ b/library/std/src/io/buffered/bufreader.rs @@ -1,7 +1,7 @@ use crate::cmp; use crate::fmt; use crate::io::{ - self, BufRead, IoSliceMut, Read, ReadBuf, Seek, SeekFrom, SizeHint, DEFAULT_BUF_SIZE, + self, BufRead, IoSliceMut, Read, ReadBuf, ReadBufRef, Seek, SeekFrom, SizeHint, DEFAULT_BUF_SIZE, }; use crate::mem::MaybeUninit; @@ -271,7 +271,7 @@ impl Read for BufReader { Ok(nread) } - fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + fn read_buf(&mut self, mut buf: ReadBufRef<'_, '_>) -> io::Result<()> { // If we don't have any buffered data and we're doing a massive read // (larger than our internal buffer), bypass our internal buffer // entirely. @@ -283,7 +283,7 @@ impl Read for BufReader { let prev = buf.filled_len(); let mut rem = self.fill_buf()?; - rem.read_buf(buf)?; + rem.read_buf(buf.reborrow())?; self.consume(buf.filled_len() - prev); //slice impl of read_buf known to never unfill buf @@ -386,7 +386,7 @@ impl BufRead for BufReader { readbuf.assume_init(self.init); } - self.inner.read_buf(&mut readbuf)?; + self.inner.read_buf(readbuf.borrow())?; self.cap = readbuf.filled_len(); self.init = readbuf.initialized_len(); diff --git a/library/std/src/io/buffered/tests.rs b/library/std/src/io/buffered/tests.rs index 9d429e7090e83..5b5c46f460569 100644 --- a/library/std/src/io/buffered/tests.rs +++ b/library/std/src/io/buffered/tests.rs @@ -64,7 +64,7 @@ fn test_buffered_reader_read_buf() { let mut buf = [MaybeUninit::uninit(); 3]; let mut buf = ReadBuf::uninit(&mut buf); - reader.read_buf(&mut buf).unwrap(); + reader.read_buf(buf.borrow()).unwrap(); assert_eq!(buf.filled(), [5, 6, 7]); assert_eq!(reader.buffer(), []); @@ -72,7 +72,7 @@ fn test_buffered_reader_read_buf() { let mut buf = [MaybeUninit::uninit(); 2]; let mut buf = ReadBuf::uninit(&mut buf); - reader.read_buf(&mut buf).unwrap(); + reader.read_buf(buf.borrow()).unwrap(); assert_eq!(buf.filled(), [0, 1]); assert_eq!(reader.buffer(), []); @@ -80,7 +80,7 @@ fn test_buffered_reader_read_buf() { let mut buf = [MaybeUninit::uninit(); 1]; let mut buf = ReadBuf::uninit(&mut buf); - reader.read_buf(&mut buf).unwrap(); + reader.read_buf(buf.borrow()).unwrap(); assert_eq!(buf.filled(), [2]); assert_eq!(reader.buffer(), [3]); @@ -88,19 +88,19 @@ fn test_buffered_reader_read_buf() { let mut buf = [MaybeUninit::uninit(); 3]; let mut buf = ReadBuf::uninit(&mut buf); - reader.read_buf(&mut buf).unwrap(); + reader.read_buf(buf.borrow()).unwrap(); assert_eq!(buf.filled(), [3]); assert_eq!(reader.buffer(), []); - reader.read_buf(&mut buf).unwrap(); + reader.read_buf(buf.borrow()).unwrap(); assert_eq!(buf.filled(), [3, 4]); assert_eq!(reader.buffer(), []); buf.clear(); - reader.read_buf(&mut buf).unwrap(); + reader.read_buf(buf.borrow()).unwrap(); assert_eq!(buf.filled_len(), 0); } diff --git a/library/std/src/io/copy.rs b/library/std/src/io/copy.rs index 6ab9666230524..8efef36dcf75a 100644 --- a/library/std/src/io/copy.rs +++ b/library/std/src/io/copy.rs @@ -95,7 +95,7 @@ impl BufferedCopySpec for BufWriter { } if read_buf.capacity() >= DEFAULT_BUF_SIZE { - match reader.read_buf(&mut read_buf) { + match reader.read_buf(read_buf.borrow()) { Ok(()) => { let bytes_read = read_buf.filled_len(); @@ -132,7 +132,7 @@ fn stack_buffer_copy( let mut len = 0; loop { - match reader.read_buf(&mut buf) { + match reader.read_buf(buf.borrow()) { Ok(()) => {} Err(e) if e.kind() == ErrorKind::Interrupted => continue, Err(e) => return Err(e), diff --git a/library/std/src/io/cursor.rs b/library/std/src/io/cursor.rs index fc19704becee2..9e17409f80c7c 100644 --- a/library/std/src/io/cursor.rs +++ b/library/std/src/io/cursor.rs @@ -4,7 +4,7 @@ mod tests; use crate::io::prelude::*; use crate::cmp; -use crate::io::{self, ErrorKind, IoSlice, IoSliceMut, ReadBuf, SeekFrom}; +use crate::io::{self, ErrorKind, IoSlice, IoSliceMut, ReadBufRef, SeekFrom}; use core::convert::TryInto; @@ -324,10 +324,10 @@ where Ok(n) } - fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + fn read_buf(&mut self, mut buf: ReadBufRef<'_, '_>) -> io::Result<()> { let prev_filled = buf.filled_len(); - Read::read_buf(&mut self.fill_buf()?, buf)?; + Read::read_buf(&mut self.fill_buf()?, buf.reborrow())?; self.pos += (buf.filled_len() - prev_filled) as u64; diff --git a/library/std/src/io/impls.rs b/library/std/src/io/impls.rs index 64d2457bce159..1c6ec76622cb9 100644 --- a/library/std/src/io/impls.rs +++ b/library/std/src/io/impls.rs @@ -5,7 +5,7 @@ use crate::alloc::Allocator; use crate::cmp; use crate::fmt; use crate::io::{ - self, BufRead, ErrorKind, IoSlice, IoSliceMut, Read, ReadBuf, Seek, SeekFrom, Write, + self, BufRead, ErrorKind, IoSlice, IoSliceMut, Read, ReadBufRef, Seek, SeekFrom, Write, }; use crate::mem; @@ -20,7 +20,7 @@ impl Read for &mut R { } #[inline] - fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + fn read_buf(&mut self, buf: ReadBufRef<'_, '_>) -> io::Result<()> { (**self).read_buf(buf) } @@ -124,7 +124,7 @@ impl Read for Box { } #[inline] - fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + fn read_buf(&mut self, buf: ReadBufRef<'_, '_>) -> io::Result<()> { (**self).read_buf(buf) } @@ -248,7 +248,7 @@ impl Read for &[u8] { } #[inline] - fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + fn read_buf(&mut self, mut buf: ReadBufRef<'_, '_>) -> io::Result<()> { let amt = cmp::min(buf.remaining(), self.len()); let (a, b) = self.split_at(amt); diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 71a59fb580321..697bc890ac088 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -281,7 +281,7 @@ pub use self::{ }; #[unstable(feature = "read_buf", issue = "78485")] -pub use self::readbuf::ReadBuf; +pub use self::readbuf::{ReadBuf, ReadBufRef}; pub(crate) use error::const_io_error; mod buffered; @@ -372,7 +372,7 @@ pub(crate) fn default_read_to_end(r: &mut R, buf: &mut Vec read_buf.assume_init(initialized); } - match r.read_buf(&mut read_buf) { + match r.read_buf(read_buf.borrow()) { Ok(()) => {} Err(e) if e.kind() == ErrorKind::Interrupted => continue, Err(e) => return Err(e), @@ -464,7 +464,7 @@ pub(crate) fn default_read_exact(this: &mut R, mut buf: &mut [ } } -pub(crate) fn default_read_buf(read: F, buf: &mut ReadBuf<'_>) -> Result<()> +pub(crate) fn default_read_buf(read: F, mut buf: ReadBufRef<'_, '_>) -> Result<()> where F: FnOnce(&mut [u8]) -> Result, { @@ -811,7 +811,7 @@ pub trait Read { /// /// The default implementation delegates to `read`. #[unstable(feature = "read_buf", issue = "78485")] - fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<()> { + fn read_buf(&mut self, buf: ReadBufRef<'_, '_>) -> Result<()> { default_read_buf(|b| self.read(b), buf) } @@ -820,10 +820,10 @@ pub trait Read { /// This is equivalent to the [`read_exact`](Read::read_exact) method, except that it is passed a [`ReadBuf`] rather than `[u8]` to /// allow use with uninitialized buffers. #[unstable(feature = "read_buf", issue = "78485")] - fn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<()> { + fn read_buf_exact(&mut self, mut buf: ReadBufRef<'_, '_>) -> Result<()> { while buf.remaining() > 0 { let prev_filled = buf.filled().len(); - match self.read_buf(buf) { + match self.read_buf(buf.reborrow()) { Ok(()) => {} Err(e) if e.kind() == ErrorKind::Interrupted => continue, Err(e) => return Err(e), @@ -2565,7 +2565,7 @@ impl Read for Take { Ok(n) } - fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<()> { + fn read_buf(&mut self, mut buf: ReadBufRef<'_, '_>) -> Result<()> { // Don't call into inner reader at all at EOF because it may still block if self.limit == 0 { return Ok(()); @@ -2589,7 +2589,7 @@ impl Read for Take { sliced_buf.assume_init(extra_init); } - self.inner.read_buf(&mut sliced_buf)?; + self.inner.read_buf(sliced_buf.borrow())?; let new_init = sliced_buf.initialized_len(); let filled = sliced_buf.filled_len(); @@ -2605,7 +2605,7 @@ impl Read for Take { self.limit -= filled as u64; } else { - self.inner.read_buf(buf)?; + self.inner.read_buf(buf.reborrow())?; //inner may unfill self.limit -= buf.filled_len().saturating_sub(prev_filled) as u64; diff --git a/library/std/src/io/readbuf.rs b/library/std/src/io/readbuf.rs index c655bc889308c..69b02f5238dff 100644 --- a/library/std/src/io/readbuf.rs +++ b/library/std/src/io/readbuf.rs @@ -58,6 +58,12 @@ impl<'a> ReadBuf<'a> { ReadBuf { buf, filled: 0, initialized: 0 } } + /// Creates a new [`ReadBufRef`] referencing this `ReadBuf`. + #[inline] + pub fn borrow(&mut self) -> ReadBufRef<'_, 'a> { + ReadBufRef {read_buf: self} + } + /// Returns the total capacity of the buffer. #[inline] pub fn capacity(&self) -> usize { @@ -243,3 +249,166 @@ impl<'a> ReadBuf<'a> { self.initialized } } + + +/// A wrapper around [`&mut ReadBuf`](ReadBuf) which prevents the buffer that the `ReadBuf` points to from being replaced. +#[derive(Debug)] +pub struct ReadBufRef<'a, 'b> { + read_buf: &'a mut ReadBuf<'b> +} + +impl<'a, 'b> ReadBufRef<'a, 'b> { + /// Creates a new `ReadBufRef` referencing the same `ReadBuf` as this one. + pub fn reborrow(&mut self) -> ReadBufRef<'_, 'b> { + ReadBufRef {read_buf: self.read_buf} + } + + /// Returns the total capacity of the buffer. + #[inline] + pub fn capacity(&self) -> usize { + self.read_buf.capacity() + } + + /// Returns a shared reference to the filled portion of the buffer. + #[inline] + pub fn filled(&self) -> &[u8] { + self.read_buf.filled() + } + + /// Returns a mutable reference to the filled portion of the buffer. + #[inline] + pub fn filled_mut(&mut self) -> &mut [u8] { + self.read_buf.filled_mut() + } + + /// Returns a shared reference to the initialized portion of the buffer. + /// + /// This includes the filled portion. + #[inline] + pub fn initialized(&self) -> &[u8] { + self.read_buf.initialized() + } + + /// Returns a mutable reference to the initialized portion of the buffer. + /// + /// This includes the filled portion. + #[inline] + pub fn initialized_mut(&mut self) -> &mut [u8] { + self.read_buf.initialized_mut() + } + + /// Returns a mutable reference to the unfilled part of the buffer without ensuring that it has been fully + /// initialized. + /// + /// # Safety + /// + /// The caller must not de-initialize portions of the buffer that have already been initialized. + #[inline] + pub unsafe fn unfilled_mut(&mut self) -> &mut [MaybeUninit] { + self.read_buf.unfilled_mut() + } + + /// Returns a mutable reference to the uninitialized part of the buffer. + /// + /// It is safe to uninitialize any of these bytes. + #[inline] + pub fn uninitialized_mut(&mut self) -> &mut [MaybeUninit] { + self.read_buf.uninitialized_mut() + } + + /// Returns a mutable reference to the unfilled part of the buffer, ensuring it is fully initialized. + /// + /// Since `ReadBuf` tracks the region of the buffer that has been initialized, this is effectively "free" after + /// the first use. + #[inline] + pub fn initialize_unfilled(&mut self) -> &mut [u8] { + self.read_buf.initialize_unfilled() + } + + /// Returns a mutable reference to the first `n` bytes of the unfilled part of the buffer, ensuring it is + /// fully initialized. + /// + /// # Panics + /// + /// Panics if `self.remaining()` is less than `n`. + #[inline] + pub fn initialize_unfilled_to(&mut self, n: usize) -> &mut [u8] { + self.read_buf.initialize_unfilled_to(n) + } + + /// Returns the number of bytes at the end of the slice that have not yet been filled. + #[inline] + pub fn remaining(&self) -> usize { + self.read_buf.remaining() + } + + /// Clears the buffer, resetting the filled region to empty. + /// + /// The number of initialized bytes is not changed, and the contents of the buffer are not modified. + #[inline] + pub fn clear(&mut self) { + self.read_buf.clear() + } + + /// Increases the size of the filled region of the buffer. + /// + /// The number of initialized bytes is not changed. + /// + /// # Panics + /// + /// Panics if the filled region of the buffer would become larger than the initialized region. + #[inline] + pub fn add_filled(&mut self, n: usize) { + self.read_buf.add_filled(n) + } + + /// Sets the size of the filled region of the buffer. + /// + /// The number of initialized bytes is not changed. + /// + /// Note that this can be used to *shrink* the filled region of the buffer in addition to growing it (for + /// example, by a `Read` implementation that compresses data in-place). + /// + /// # Panics + /// + /// Panics if the filled region of the buffer would become larger than the initialized region. + #[inline] + pub fn set_filled(&mut self, n: usize) { + self.read_buf.set_filled(n) + } + + /// Asserts that the first `n` unfilled bytes of the buffer are initialized. + /// + /// `ReadBuf` assumes that bytes are never de-initialized, so this method does nothing when called with fewer + /// bytes than are already known to be initialized. + /// + /// # Safety + /// + /// The caller must ensure that the first `n` unfilled bytes of the buffer have already been initialized. + #[inline] + pub unsafe fn assume_init(&mut self, n: usize) { + self.read_buf.assume_init(n) + } + + /// Appends data to the buffer, advancing the written position and possibly also the initialized position. + /// + /// # Panics + /// + /// Panics if `self.remaining()` is less than `buf.len()`. + #[inline] + pub fn append(&mut self, buf: &[u8]) { + self.read_buf.append(buf) + } + + /// Returns the amount of bytes that have been filled. + #[inline] + pub fn filled_len(&self) -> usize { + self.read_buf.filled_len() + } + + /// Returns the amount of bytes that have been initialized. + #[inline] + pub fn initialized_len(&self) -> usize { + self.read_buf.initialized_len() + } +} \ No newline at end of file diff --git a/library/std/src/io/tests.rs b/library/std/src/io/tests.rs index eb62634856462..de7bfcaa85366 100644 --- a/library/std/src/io/tests.rs +++ b/library/std/src/io/tests.rs @@ -163,20 +163,20 @@ fn read_buf_exact() { let mut buf = ReadBuf::new(&mut buf); let mut c = Cursor::new(&b""[..]); - assert_eq!(c.read_buf_exact(&mut buf).unwrap_err().kind(), io::ErrorKind::UnexpectedEof); + assert_eq!(c.read_buf_exact(buf.borrow()).unwrap_err().kind(), io::ErrorKind::UnexpectedEof); let mut c = Cursor::new(&b"123456789"[..]); - c.read_buf_exact(&mut buf).unwrap(); + c.read_buf_exact(buf.borrow()).unwrap(); assert_eq!(buf.filled(), b"1234"); buf.clear(); - c.read_buf_exact(&mut buf).unwrap(); + c.read_buf_exact(buf.borrow()).unwrap(); assert_eq!(buf.filled(), b"5678"); buf.clear(); - assert_eq!(c.read_buf_exact(&mut buf).unwrap_err().kind(), io::ErrorKind::UnexpectedEof); + assert_eq!(c.read_buf_exact(buf.borrow()).unwrap_err().kind(), io::ErrorKind::UnexpectedEof); } #[test] @@ -599,6 +599,6 @@ fn bench_take_read_buf(b: &mut test::Bencher) { let mut rbuf = ReadBuf::uninit(&mut buf); - [255; 128].take(64).read_buf(&mut rbuf).unwrap(); + [255; 128].take(64).read_buf(rbuf.borrow()).unwrap(); }); } diff --git a/library/std/src/io/util.rs b/library/std/src/io/util.rs index c1300cd67c086..27b57a2bca233 100644 --- a/library/std/src/io/util.rs +++ b/library/std/src/io/util.rs @@ -5,7 +5,7 @@ mod tests; use crate::fmt; use crate::io::{ - self, BufRead, IoSlice, IoSliceMut, Read, ReadBuf, Seek, SeekFrom, SizeHint, Write, + self, BufRead, IoSlice, IoSliceMut, Read, ReadBufRef, Seek, SeekFrom, SizeHint, Write, }; /// A reader which is always at EOF. @@ -47,7 +47,7 @@ impl Read for Empty { } #[inline] - fn read_buf(&mut self, _buf: &mut ReadBuf<'_>) -> io::Result<()> { + fn read_buf(&mut self, _buf: ReadBufRef<'_, '_>) -> io::Result<()> { Ok(()) } } @@ -130,7 +130,7 @@ impl Read for Repeat { Ok(buf.len()) } - fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + fn read_buf(&mut self, mut buf: ReadBufRef<'_, '_>) -> io::Result<()> { // SAFETY: No uninit bytes are being written for slot in unsafe { buf.unfilled_mut() } { slot.write(self.byte); diff --git a/library/std/src/io/util/tests.rs b/library/std/src/io/util/tests.rs index 08972a59a833f..9f1bc09ef0e5a 100644 --- a/library/std/src/io/util/tests.rs +++ b/library/std/src/io/util/tests.rs @@ -81,25 +81,25 @@ fn empty_reads() { let mut buf = []; let mut buf = ReadBuf::uninit(&mut buf); - e.read_buf(&mut buf).unwrap(); + e.read_buf(buf.borrow()).unwrap(); assert_eq!(buf.filled_len(), 0); assert_eq!(buf.initialized_len(), 0); let mut buf = [MaybeUninit::uninit()]; let mut buf = ReadBuf::uninit(&mut buf); - e.read_buf(&mut buf).unwrap(); + e.read_buf(buf.borrow()).unwrap(); assert_eq!(buf.filled_len(), 0); assert_eq!(buf.initialized_len(), 0); let mut buf = [MaybeUninit::uninit(); 1024]; let mut buf = ReadBuf::uninit(&mut buf); - e.read_buf(&mut buf).unwrap(); + e.read_buf(buf.borrow()).unwrap(); assert_eq!(buf.filled_len(), 0); assert_eq!(buf.initialized_len(), 0); let mut buf = [MaybeUninit::uninit(); 1024]; let mut buf = ReadBuf::uninit(&mut buf); - e.by_ref().read_buf(&mut buf).unwrap(); + e.by_ref().read_buf(buf.borrow()).unwrap(); assert_eq!(buf.filled_len(), 0); assert_eq!(buf.initialized_len(), 0); } diff --git a/library/std/src/sys/hermit/fs.rs b/library/std/src/sys/hermit/fs.rs index fa9a7fb19e463..1d3d3f5865ccc 100644 --- a/library/std/src/sys/hermit/fs.rs +++ b/library/std/src/sys/hermit/fs.rs @@ -2,7 +2,7 @@ use crate::ffi::{CStr, CString, OsString}; use crate::fmt; use crate::hash::{Hash, Hasher}; use crate::io::{self, Error, ErrorKind}; -use crate::io::{IoSlice, IoSliceMut, ReadBuf, SeekFrom}; +use crate::io::{IoSlice, IoSliceMut, ReadBufRef, SeekFrom}; use crate::os::unix::ffi::OsStrExt; use crate::path::{Path, PathBuf}; use crate::sys::cvt; @@ -312,7 +312,7 @@ impl File { false } - pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + pub fn read_buf(&self, buf: ReadBufRef<'_, '_>) -> io::Result<()> { crate::io::default_read_buf(|buf| self.read(buf), buf) } diff --git a/library/std/src/sys/solid/fs.rs b/library/std/src/sys/solid/fs.rs index a6ed10f7789d2..843f8d9f862ca 100644 --- a/library/std/src/sys/solid/fs.rs +++ b/library/std/src/sys/solid/fs.rs @@ -339,7 +339,7 @@ impl File { } } - pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + pub fn read_buf(&self, buf: ReadBufRef<'_, '_>) -> io::Result<()> { unsafe { let len = buf.remaining(); let mut out_num_bytes = MaybeUninit::uninit(); diff --git a/library/std/src/sys/unix/fd.rs b/library/std/src/sys/unix/fd.rs index 3de7c68a6866d..fef3199b0838b 100644 --- a/library/std/src/sys/unix/fd.rs +++ b/library/std/src/sys/unix/fd.rs @@ -4,7 +4,7 @@ mod tests; use crate::cmp; -use crate::io::{self, IoSlice, IoSliceMut, Read, ReadBuf}; +use crate::io::{self, IoSlice, IoSliceMut, Read, ReadBufRef}; use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; use crate::sys::cvt; use crate::sys_common::{AsInner, FromInner, IntoInner}; @@ -115,7 +115,7 @@ impl FileDesc { } } - pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + pub fn read_buf(&self, mut buf: ReadBufRef<'_, '_>) -> io::Result<()> { let ret = cvt(unsafe { libc::read( self.as_raw_fd(), diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs index 8bd0b9b14afed..afbe02cd9df57 100644 --- a/library/std/src/sys/unix/fs.rs +++ b/library/std/src/sys/unix/fs.rs @@ -2,7 +2,7 @@ use crate::os::unix::prelude::*; use crate::ffi::{CStr, CString, OsStr, OsString}; use crate::fmt; -use crate::io::{self, Error, IoSlice, IoSliceMut, ReadBuf, SeekFrom}; +use crate::io::{self, Error, IoSlice, IoSliceMut, ReadBufRef, SeekFrom}; use crate::mem; use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd}; use crate::path::{Path, PathBuf}; @@ -892,7 +892,7 @@ impl File { self.0.read_at(buf, offset) } - pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + pub fn read_buf(&self, buf: ReadBufRef<'_, '_>) -> io::Result<()> { self.0.read_buf(buf) } diff --git a/library/std/src/sys/unsupported/fs.rs b/library/std/src/sys/unsupported/fs.rs index d1d2847cd33be..eeb9debceee9d 100644 --- a/library/std/src/sys/unsupported/fs.rs +++ b/library/std/src/sys/unsupported/fs.rs @@ -1,7 +1,7 @@ use crate::ffi::OsString; use crate::fmt; use crate::hash::{Hash, Hasher}; -use crate::io::{self, IoSlice, IoSliceMut, ReadBuf, SeekFrom}; +use crate::io::{self, IoSlice, IoSliceMut, ReadBufRef, SeekFrom}; use crate::path::{Path, PathBuf}; use crate::sys::time::SystemTime; use crate::sys::unsupported; @@ -206,7 +206,7 @@ impl File { self.0 } - pub fn read_buf(&self, _buf: &mut ReadBuf<'_>) -> io::Result<()> { + pub fn read_buf(&self, _buf: ReadBufRef<'_, '_>) -> io::Result<()> { self.0 } diff --git a/library/std/src/sys/wasi/fs.rs b/library/std/src/sys/wasi/fs.rs index cd6815bfc2136..47e6962fd603c 100644 --- a/library/std/src/sys/wasi/fs.rs +++ b/library/std/src/sys/wasi/fs.rs @@ -3,7 +3,7 @@ use super::fd::WasiFd; use crate::ffi::{CStr, CString, OsStr, OsString}; use crate::fmt; -use crate::io::{self, IoSlice, IoSliceMut, ReadBuf, SeekFrom}; +use crate::io::{self, IoSlice, IoSliceMut, ReadBufRef, SeekFrom}; use crate::iter; use crate::mem::{self, ManuallyDrop}; use crate::os::raw::c_int; @@ -423,7 +423,7 @@ impl File { true } - pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + pub fn read_buf(&self, buf: ReadBufRef<'_, '_>) -> io::Result<()> { crate::io::default_read_buf(|buf| self.read(buf), buf) } diff --git a/library/std/src/sys/windows/fs.rs b/library/std/src/sys/windows/fs.rs index cb83ee2469a1c..fe7ce505498f6 100644 --- a/library/std/src/sys/windows/fs.rs +++ b/library/std/src/sys/windows/fs.rs @@ -2,7 +2,7 @@ use crate::os::windows::prelude::*; use crate::ffi::OsString; use crate::fmt; -use crate::io::{self, Error, IoSlice, IoSliceMut, ReadBuf, SeekFrom}; +use crate::io::{self, Error, IoSlice, IoSliceMut, ReadBufRef, SeekFrom}; use crate::mem; use crate::os::windows::io::{AsHandle, BorrowedHandle}; use crate::path::{Path, PathBuf}; @@ -420,7 +420,7 @@ impl File { self.handle.read_at(buf, offset) } - pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + pub fn read_buf(&self, buf: ReadBufRef<'_, '_>) -> io::Result<()> { self.handle.read_buf(buf) } diff --git a/library/std/src/sys/windows/handle.rs b/library/std/src/sys/windows/handle.rs index daab39bb00cbc..04b0bb3fcae19 100644 --- a/library/std/src/sys/windows/handle.rs +++ b/library/std/src/sys/windows/handle.rs @@ -1,7 +1,7 @@ #![unstable(issue = "none", feature = "windows_handle")] use crate::cmp; -use crate::io::{self, ErrorKind, IoSlice, IoSliceMut, Read, ReadBuf}; +use crate::io::{self, ErrorKind, IoSlice, IoSliceMut, Read, ReadBufRef}; use crate::mem; use crate::os::windows::io::{ AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle, RawHandle, @@ -130,7 +130,7 @@ impl Handle { } } - pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> { + pub fn read_buf(&self, mut buf: ReadBufRef<'_, '_>) -> io::Result<()> { let mut read = 0; let len = cmp::min(buf.remaining(), ::MAX as usize) as c::DWORD; let res = cvt(unsafe { From 325455f411f681178318e4d68571f9894560219d Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Wed, 26 Jan 2022 20:09:03 -0800 Subject: [PATCH 2/3] formatting --- library/std/src/io/buffered/bufreader.rs | 3 ++- library/std/src/io/readbuf.rs | 9 ++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/library/std/src/io/buffered/bufreader.rs b/library/std/src/io/buffered/bufreader.rs index b366bfffc1633..bf135a96c07ee 100644 --- a/library/std/src/io/buffered/bufreader.rs +++ b/library/std/src/io/buffered/bufreader.rs @@ -1,7 +1,8 @@ use crate::cmp; use crate::fmt; use crate::io::{ - self, BufRead, IoSliceMut, Read, ReadBuf, ReadBufRef, Seek, SeekFrom, SizeHint, DEFAULT_BUF_SIZE, + self, BufRead, IoSliceMut, Read, ReadBuf, ReadBufRef, Seek, SeekFrom, SizeHint, + DEFAULT_BUF_SIZE, }; use crate::mem::MaybeUninit; diff --git a/library/std/src/io/readbuf.rs b/library/std/src/io/readbuf.rs index 69b02f5238dff..c7ed0c855e0be 100644 --- a/library/std/src/io/readbuf.rs +++ b/library/std/src/io/readbuf.rs @@ -61,7 +61,7 @@ impl<'a> ReadBuf<'a> { /// Creates a new [`ReadBufRef`] referencing this `ReadBuf`. #[inline] pub fn borrow(&mut self) -> ReadBufRef<'_, 'a> { - ReadBufRef {read_buf: self} + ReadBufRef { read_buf: self } } /// Returns the total capacity of the buffer. @@ -250,17 +250,16 @@ impl<'a> ReadBuf<'a> { } } - /// A wrapper around [`&mut ReadBuf`](ReadBuf) which prevents the buffer that the `ReadBuf` points to from being replaced. #[derive(Debug)] pub struct ReadBufRef<'a, 'b> { - read_buf: &'a mut ReadBuf<'b> + read_buf: &'a mut ReadBuf<'b>, } impl<'a, 'b> ReadBufRef<'a, 'b> { /// Creates a new `ReadBufRef` referencing the same `ReadBuf` as this one. pub fn reborrow(&mut self) -> ReadBufRef<'_, 'b> { - ReadBufRef {read_buf: self.read_buf} + ReadBufRef { read_buf: self.read_buf } } /// Returns the total capacity of the buffer. @@ -411,4 +410,4 @@ impl<'a, 'b> ReadBufRef<'a, 'b> { pub fn initialized_len(&self) -> usize { self.read_buf.initialized_len() } -} \ No newline at end of file +} From f168e975fe685f30896571bc0749dfd983fcdce2 Mon Sep 17 00:00:00 2001 From: DrMeepster <19316085+DrMeepster@users.noreply.github.com> Date: Sun, 30 Jan 2022 22:54:05 -0800 Subject: [PATCH 3/3] impl deref --- library/std/src/io/readbuf.rs | 42 ++++++----------------------------- 1 file changed, 7 insertions(+), 35 deletions(-) diff --git a/library/std/src/io/readbuf.rs b/library/std/src/io/readbuf.rs index c7ed0c855e0be..dcae3ff2f8ff8 100644 --- a/library/std/src/io/readbuf.rs +++ b/library/std/src/io/readbuf.rs @@ -7,6 +7,8 @@ use crate::cmp; use crate::fmt::{self, Debug, Formatter}; use crate::mem::MaybeUninit; +use core::ops::Deref; + /// A wrapper around a byte buffer that is incrementally filled and initialized. /// /// This type is a sort of "double cursor". It tracks three regions in the buffer: a region at the beginning of the @@ -262,32 +264,12 @@ impl<'a, 'b> ReadBufRef<'a, 'b> { ReadBufRef { read_buf: self.read_buf } } - /// Returns the total capacity of the buffer. - #[inline] - pub fn capacity(&self) -> usize { - self.read_buf.capacity() - } - - /// Returns a shared reference to the filled portion of the buffer. - #[inline] - pub fn filled(&self) -> &[u8] { - self.read_buf.filled() - } - /// Returns a mutable reference to the filled portion of the buffer. #[inline] pub fn filled_mut(&mut self) -> &mut [u8] { self.read_buf.filled_mut() } - /// Returns a shared reference to the initialized portion of the buffer. - /// - /// This includes the filled portion. - #[inline] - pub fn initialized(&self) -> &[u8] { - self.read_buf.initialized() - } - /// Returns a mutable reference to the initialized portion of the buffer. /// /// This includes the filled portion. @@ -335,12 +317,6 @@ impl<'a, 'b> ReadBufRef<'a, 'b> { self.read_buf.initialize_unfilled_to(n) } - /// Returns the number of bytes at the end of the slice that have not yet been filled. - #[inline] - pub fn remaining(&self) -> usize { - self.read_buf.remaining() - } - /// Clears the buffer, resetting the filled region to empty. /// /// The number of initialized bytes is not changed, and the contents of the buffer are not modified. @@ -398,16 +374,12 @@ impl<'a, 'b> ReadBufRef<'a, 'b> { pub fn append(&mut self, buf: &[u8]) { self.read_buf.append(buf) } +} - /// Returns the amount of bytes that have been filled. - #[inline] - pub fn filled_len(&self) -> usize { - self.read_buf.filled_len() - } +impl<'a, 'b> Deref for ReadBufRef<'a, 'b> { + type Target = ReadBuf<'b>; - /// Returns the amount of bytes that have been initialized. - #[inline] - pub fn initialized_len(&self) -> usize { - self.read_buf.initialized_len() + fn deref(&self) -> &ReadBuf<'b> { + &*self.read_buf } }