Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ReadBufRef #93359

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
9 changes: 5 additions & 4 deletions library/std/src/io/buffered/bufreader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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;

Expand Down Expand Up @@ -271,7 +272,7 @@ impl<R: Read> Read for BufReader<R> {
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.
Expand All @@ -283,7 +284,7 @@ impl<R: Read> Read for BufReader<R> {
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

Expand Down Expand Up @@ -386,7 +387,7 @@ impl<R: Read> BufRead for BufReader<R> {
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();
Expand Down
12 changes: 6 additions & 6 deletions library/std/src/io/buffered/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,43 +64,43 @@ 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(), []);

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(), []);

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]);

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);
}
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/io/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl<I: Write> BufferedCopySpec for BufWriter<I> {
}

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();

Expand Down Expand Up @@ -132,7 +132,7 @@ fn stack_buffer_copy<R: Read + ?Sized, W: Write + ?Sized>(
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),
Expand Down
6 changes: 3 additions & 3 deletions library/std/src/io/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions library/std/src/io/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,7 +20,7 @@ impl<R: Read + ?Sized> 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)
}

Expand Down Expand Up @@ -124,7 +124,7 @@ impl<R: Read + ?Sized> Read for Box<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)
}

Expand Down Expand Up @@ -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);

Expand Down
18 changes: 9 additions & 9 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -372,7 +372,7 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>
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),
Expand Down Expand Up @@ -464,7 +464,7 @@ pub(crate) fn default_read_exact<R: Read + ?Sized>(this: &mut R, mut buf: &mut [
}
}

pub(crate) fn default_read_buf<F>(read: F, buf: &mut ReadBuf<'_>) -> Result<()>
pub(crate) fn default_read_buf<F>(read: F, mut buf: ReadBufRef<'_, '_>) -> Result<()>
where
F: FnOnce(&mut [u8]) -> Result<usize>,
{
Expand Down Expand Up @@ -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)
}

Expand All @@ -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),
Expand Down Expand Up @@ -2565,7 +2565,7 @@ impl<T: Read> Read for Take<T> {
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(());
Expand All @@ -2589,7 +2589,7 @@ impl<T: Read> Read for Take<T> {
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();
Expand All @@ -2605,7 +2605,7 @@ impl<T: Read> Read for Take<T> {

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;
Expand Down
Loading