1
Fork 0

non-linux platforms

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron 2022-06-07 08:43:54 +01:00
parent b56cf67ce1
commit 1a2122fff0
21 changed files with 205 additions and 140 deletions

View file

@ -13,7 +13,7 @@ mod tests;
use crate::ffi::OsString;
use crate::fmt;
use crate::io::{self, BorrowCursor, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
use crate::path::{Path, PathBuf};
use crate::sys::fs as fs_imp;
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
@ -703,7 +703,7 @@ impl Read for File {
self.inner.read_vectored(bufs)
}
fn read_buf(&mut self, cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
fn read_buf(&mut self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
self.inner.read_buf(cursor)
}
@ -755,7 +755,7 @@ impl Read for &File {
self.inner.read(buf)
}
fn read_buf(&mut self, cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
fn read_buf(&mut self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
self.inner.read_buf(cursor)
}

View file

@ -2,8 +2,7 @@ mod buffer;
use crate::fmt;
use crate::io::{
self, BorrowBuf, BorrowCursor, BufRead, IoSliceMut, Read, Seek, SeekFrom, SizeHint,
DEFAULT_BUF_SIZE,
self, BorrowedCursor, BufRead, IoSliceMut, Read, Seek, SeekFrom, SizeHint, DEFAULT_BUF_SIZE,
};
use buffer::Buffer;
@ -267,7 +266,7 @@ impl<R: Read> Read for BufReader<R> {
Ok(nread)
}
fn read_buf(&mut self, mut cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
fn read_buf(&mut self, mut cursor: BorrowedCursor<'_, '_>) -> 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.

View file

@ -9,7 +9,7 @@
/// that user code which wants to do reads from a `BufReader` via `buffer` + `consume` can do so
/// without encountering any runtime bounds checks.
use crate::cmp;
use crate::io::{self, Read, ReadBuf};
use crate::io::{self, BorrowedBuf, Read};
use crate::mem::MaybeUninit;
pub struct Buffer {
@ -93,11 +93,15 @@ impl Buffer {
if self.pos >= self.filled {
debug_assert!(self.pos == self.filled);
let mut readbuf = ReadBuf::uninit(&mut self.buf);
let mut buf: BorrowedBuf<'_> = (&mut *self.buf).into();
// SAFETY: `self.filled` bytes will always have been initialized.
unsafe {
buf.set_init(self.filled);
}
reader.read_buf(&mut readbuf)?;
reader.read_buf(buf.unfilled())?;
self.filled = readbuf.filled_len();
self.filled = buf.len();
self.pos = 0;
}
Ok(self.buffer())

View file

@ -1,5 +1,7 @@
use crate::io::prelude::*;
use crate::io::{self, BorrowBuf, BufReader, BufWriter, ErrorKind, IoSlice, LineWriter, SeekFrom};
use crate::io::{
self, BorrowedBuf, BufReader, BufWriter, ErrorKind, IoSlice, LineWriter, SeekFrom,
};
use crate::mem::MaybeUninit;
use crate::panic;
use crate::sync::atomic::{AtomicUsize, Ordering};
@ -62,7 +64,7 @@ fn test_buffered_reader_read_buf() {
let mut reader = BufReader::with_capacity(2, inner);
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 3];
let mut buf: BorrowBuf<'_> = buf.into();
let mut buf: BorrowedBuf<'_> = buf.into();
reader.read_buf(buf.unfilled()).unwrap();
@ -70,7 +72,7 @@ fn test_buffered_reader_read_buf() {
assert_eq!(reader.buffer(), []);
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 2];
let mut buf: BorrowBuf<'_> = buf.into();
let mut buf: BorrowedBuf<'_> = buf.into();
reader.read_buf(buf.unfilled()).unwrap();
@ -78,7 +80,7 @@ fn test_buffered_reader_read_buf() {
assert_eq!(reader.buffer(), []);
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 1];
let mut buf: BorrowBuf<'_> = buf.into();
let mut buf: BorrowedBuf<'_> = buf.into();
reader.read_buf(buf.unfilled()).unwrap();
@ -86,7 +88,7 @@ fn test_buffered_reader_read_buf() {
assert_eq!(reader.buffer(), [3]);
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 3];
let mut buf: BorrowBuf<'_> = buf.into();
let mut buf: BorrowedBuf<'_> = buf.into();
reader.read_buf(buf.unfilled()).unwrap();

View file

@ -1,4 +1,4 @@
use super::{BorrowBuf, BufWriter, ErrorKind, Read, Result, Write, DEFAULT_BUF_SIZE};
use super::{BorrowedBuf, BufWriter, ErrorKind, Read, Result, Write, DEFAULT_BUF_SIZE};
use crate::mem::MaybeUninit;
/// Copies the entire contents of a reader into a writer.
@ -97,7 +97,7 @@ impl<I: Write> BufferedCopySpec for BufWriter<I> {
loop {
let buf = writer.buffer_mut();
let mut read_buf: BorrowBuf<'_> = buf.spare_capacity_mut().into();
let mut read_buf: BorrowedBuf<'_> = buf.spare_capacity_mut().into();
unsafe {
// SAFETY: init is either 0 or the init_len from the previous iteration.
@ -117,7 +117,7 @@ impl<I: Write> BufferedCopySpec for BufWriter<I> {
init = read_buf.init_len() - bytes_read;
len += bytes_read as u64;
// SAFETY: BorrowBuf guarantees all of its filled bytes are init
// SAFETY: BorrowedBuf guarantees all of its filled bytes are init
unsafe { buf.set_len(buf.len() + bytes_read) };
// Read again if the buffer still has enough capacity, as BufWriter itself would do
@ -139,7 +139,7 @@ fn stack_buffer_copy<R: Read + ?Sized, W: Write + ?Sized>(
writer: &mut W,
) -> Result<u64> {
let buf: &mut [_] = &mut [MaybeUninit::uninit(); DEFAULT_BUF_SIZE];
let mut buf: BorrowBuf<'_> = buf.into();
let mut buf: BorrowedBuf<'_> = buf.into();
let mut len = 0;

View file

@ -5,7 +5,7 @@ use crate::io::prelude::*;
use crate::alloc::Allocator;
use crate::cmp;
use crate::io::{self, BorrowCursor, ErrorKind, IoSlice, IoSliceMut, SeekFrom};
use crate::io::{self, BorrowedCursor, ErrorKind, IoSlice, IoSliceMut, SeekFrom};
/// A `Cursor` wraps an in-memory buffer and provides it with a
/// [`Seek`] implementation.
@ -323,7 +323,7 @@ where
Ok(n)
}
fn read_buf(&mut self, mut cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
fn read_buf(&mut self, mut cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
let prev_written = cursor.written();
Read::read_buf(&mut self.fill_buf()?, cursor.clone())?;

View file

@ -6,7 +6,7 @@ use crate::cmp;
use crate::collections::VecDeque;
use crate::fmt;
use crate::io::{
self, BorrowCursor, BufRead, ErrorKind, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write,
self, BorrowedCursor, BufRead, ErrorKind, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write,
};
use crate::mem;
@ -21,7 +21,7 @@ impl<R: Read + ?Sized> Read for &mut R {
}
#[inline]
fn read_buf(&mut self, cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
fn read_buf(&mut self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
(**self).read_buf(cursor)
}
@ -125,7 +125,7 @@ impl<R: Read + ?Sized> Read for Box<R> {
}
#[inline]
fn read_buf(&mut self, cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
fn read_buf(&mut self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
(**self).read_buf(cursor)
}
@ -249,7 +249,7 @@ impl Read for &[u8] {
}
#[inline]
fn read_buf(&mut self, mut cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
fn read_buf(&mut self, mut cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
let amt = cmp::min(cursor.capacity(), self.len());
let (a, b) = self.split_at(amt);
@ -427,10 +427,10 @@ impl<A: Allocator> Read for VecDeque<u8, A> {
}
#[inline]
fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
fn read_buf(&mut self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
let (ref mut front, _) = self.as_slices();
let n = cmp::min(buf.remaining(), front.len());
Read::read_buf(front, buf)?;
let n = cmp::min(cursor.capacity(), front.len());
Read::read_buf(front, cursor)?;
self.drain(..n);
Ok(())
}

View file

@ -278,7 +278,7 @@ pub use self::{
};
#[unstable(feature = "read_buf", issue = "78485")]
pub use self::readbuf::{BorrowBuf, BorrowCursor};
pub use self::readbuf::{BorrowedBuf, BorrowedCursor};
pub(crate) use error::const_io_error;
mod buffered;
@ -362,7 +362,7 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>
buf.reserve(32); // buf is full, need more space
}
let mut read_buf: BorrowBuf<'_> = buf.spare_capacity_mut().into();
let mut read_buf: BorrowedBuf<'_> = buf.spare_capacity_mut().into();
// SAFETY: These bytes were initialized but not filled in the previous loop
unsafe {
@ -383,7 +383,7 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>
// store how much was initialized but not filled
initialized = cursor.init_ref().len();
// SAFETY: BorrowBuf's invariants mean this much memory is initialized.
// SAFETY: BorrowedBuf's invariants mean this much memory is initialized.
unsafe {
let new_len = read_buf.filled().len() + buf.len();
buf.set_len(new_len);
@ -462,7 +462,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, mut cursor: BorrowCursor<'_, '_>) -> Result<()>
pub(crate) fn default_read_buf<F>(read: F, mut cursor: BorrowedCursor<'_, '_>) -> Result<()>
where
F: FnOnce(&mut [u8]) -> Result<usize>,
{
@ -805,24 +805,23 @@ pub trait Read {
default_read_exact(self, buf)
}
// TODO naming, if should the method be read_cursor? Or should we change the names of the data structures?
/// Pull some bytes from this source into the specified buffer.
///
/// This is equivalent to the [`read`](Read::read) method, except that it is passed a [`BorrowCursor`] rather than `[u8]` to allow use
/// This is equivalent to the [`read`](Read::read) method, except that it is passed a [`BorrowedCursor`] rather than `[u8]` to allow use
/// with uninitialized buffers. The new data will be appended to any existing contents of `buf`.
///
/// The default implementation delegates to `read`.
#[unstable(feature = "read_buf", issue = "78485")]
fn read_buf(&mut self, buf: BorrowCursor<'_, '_>) -> Result<()> {
fn read_buf(&mut self, buf: BorrowedCursor<'_, '_>) -> Result<()> {
default_read_buf(|b| self.read(b), buf)
}
/// Read the exact number of bytes required to fill `cursor`.
///
/// This is equivalent to the [`read_exact`](Read::read_exact) method, except that it is passed a [`BorrowCursor`] rather than `[u8]` to
/// This is equivalent to the [`read_exact`](Read::read_exact) method, except that it is passed a [`BorrowedCursor`] rather than `[u8]` to
/// allow use with uninitialized buffers.
#[unstable(feature = "read_buf", issue = "78485")]
fn read_buf_exact(&mut self, mut cursor: BorrowCursor<'_, '_>) -> Result<()> {
fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_, '_>) -> Result<()> {
while cursor.capacity() > 0 {
let prev_written = cursor.written();
match self.read_buf(cursor.clone()) {
@ -2587,7 +2586,7 @@ impl<T: Read> Read for Take<T> {
Ok(n)
}
fn read_buf(&mut self, mut buf: BorrowCursor<'_, '_>) -> Result<()> {
fn read_buf(&mut self, mut buf: BorrowedCursor<'_, '_>) -> Result<()> {
// Don't call into inner reader at all at EOF because it may still block
if self.limit == 0 {
return Ok(());
@ -2602,7 +2601,7 @@ impl<T: Read> Read for Take<T> {
// SAFETY: no uninit data is written to ibuf
let ibuf = unsafe { &mut buf.as_mut()[..limit] };
let mut sliced_buf: BorrowBuf<'_> = ibuf.into();
let mut sliced_buf: BorrowedBuf<'_> = ibuf.into();
// SAFETY: extra_init bytes of ibuf are known to be initialized
unsafe {

View file

@ -5,6 +5,7 @@ mod tests;
use crate::cmp;
use crate::fmt::{self, Debug, Formatter};
use crate::io::{Result, Write};
use crate::mem::MaybeUninit;
/// A borrowed byte buffer which is incrementally filled and initialized.
@ -21,58 +22,58 @@ use crate::mem::MaybeUninit;
/// [ initialized | uninitialized ]
/// ```
///
/// A `BorrowBuf` is created around some existing data (or capacity for data) via a unique reference
/// (`&mut`). The `BorrowBuf` can be configured (e.g., using `clear` or `set_init`), but otherwise
/// is read-only. To write into the buffer, use `unfilled` to create a `BorrowCursor`. The cursor
/// A `BorrowedBuf` is created around some existing data (or capacity for data) via a unique reference
/// (`&mut`). The `BorrowedBuf` can be configured (e.g., using `clear` or `set_init`), but otherwise
/// is read-only. To write into the buffer, use `unfilled` to create a `BorrowedCursor`. The cursor
/// has write-only access to the unfilled portion of the buffer (you can think of it like a
/// write-only iterator).
///
/// The lifetime `'a` is a bound on the lifetime of the underlying data.
pub struct BorrowBuf<'a> {
/// The lifetime `'data` is a bound on the lifetime of the underlying data.
pub struct BorrowedBuf<'data> {
/// The buffer's underlying data.
buf: &'a mut [MaybeUninit<u8>],
buf: &'data mut [MaybeUninit<u8>],
/// The length of `self.buf` which is known to be filled.
filled: usize,
/// The length of `self.buf` which is known to be initialized.
initialized: usize,
init: usize,
}
impl Debug for BorrowBuf<'_> {
impl Debug for BorrowedBuf<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("ReadBuf")
.field("init", &self.initialized)
f.debug_struct("BorrowedBuf")
.field("init", &self.init)
.field("filled", &self.filled)
.field("capacity", &self.capacity())
.finish()
}
}
/// Create a new `BorrowBuf` from a fully initialized slice.
impl<'a> From<&'a mut [u8]> for BorrowBuf<'a> {
/// Create a new `BorrowedBuf` from a fully initialized slice.
impl<'data> From<&'data mut [u8]> for BorrowedBuf<'data> {
#[inline]
fn from(slice: &'a mut [u8]) -> BorrowBuf<'a> {
fn from(slice: &'data mut [u8]) -> BorrowedBuf<'data> {
let len = slice.len();
BorrowBuf {
//SAFETY: initialized data never becoming uninitialized is an invariant of BorrowBuf
BorrowedBuf {
//SAFETY: initialized data never becoming uninitialized is an invariant of BorrowedBuf
buf: unsafe { (slice as *mut [u8]).as_uninit_slice_mut().unwrap() },
filled: 0,
initialized: len,
init: len,
}
}
}
/// Create a new `BorrowBuf` from an uninitialized buffer.
/// Create a new `BorrowedBuf` from an uninitialized buffer.
///
/// Use `set_init` if part of the buffer is known to be already initialized.
impl<'a> From<&'a mut [MaybeUninit<u8>]> for BorrowBuf<'a> {
impl<'data> From<&'data mut [MaybeUninit<u8>]> for BorrowedBuf<'data> {
#[inline]
fn from(buf: &'a mut [MaybeUninit<u8>]) -> BorrowBuf<'a> {
BorrowBuf { buf, filled: 0, initialized: 0 }
fn from(buf: &'data mut [MaybeUninit<u8>]) -> BorrowedBuf<'data> {
BorrowedBuf { buf, filled: 0, init: 0 }
}
}
impl<'a> BorrowBuf<'a> {
impl<'data> BorrowedBuf<'data> {
/// Returns the total capacity of the buffer.
#[inline]
pub fn capacity(&self) -> usize {
@ -88,7 +89,7 @@ impl<'a> BorrowBuf<'a> {
/// Returns the length of the initialized part of the buffer.
#[inline]
pub fn init_len(&self) -> usize {
self.initialized
self.init
}
/// Returns a shared reference to the filled portion of the buffer.
@ -100,8 +101,8 @@ impl<'a> BorrowBuf<'a> {
/// Returns a cursor over the unfilled part of the buffer.
#[inline]
pub fn unfilled<'this>(&'this mut self) -> BorrowCursor<'this, 'a> {
BorrowCursor { start: self.filled, buf: self }
pub fn unfilled<'this>(&'this mut self) -> BorrowedCursor<'this, 'data> {
BorrowedCursor { start: self.filled, buf: self }
}
/// Clears the buffer, resetting the filled region to empty.
@ -115,7 +116,7 @@ impl<'a> BorrowBuf<'a> {
/// Asserts that the first `n` bytes of the buffer are initialized.
///
/// `BorrowBuf` assumes that bytes are never de-initialized, so this method does nothing when called with fewer
/// `BorrowedBuf` 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
@ -123,42 +124,42 @@ impl<'a> BorrowBuf<'a> {
/// The caller must ensure that the first `n` unfilled bytes of the buffer have already been initialized.
#[inline]
pub unsafe fn set_init(&mut self, n: usize) -> &mut Self {
self.initialized = cmp::max(self.initialized, n);
self.init = cmp::max(self.init, n);
self
}
}
/// A writeable view of the unfilled portion of a [`BorrowBuf`](BorrowBuf).
/// A writeable view of the unfilled portion of a [`BorrowedBuf`](BorrowedBuf).
///
/// Provides access to the initialized and uninitialized parts of the underlying `BorrowBuf`.
/// Data can be written directly to the cursor by using [`append`](BorrowCursor::append) or
/// Provides access to the initialized and uninitialized parts of the underlying `BorrowedBuf`.
/// Data can be written directly to the cursor by using [`append`](BorrowedCursor::append) or
/// indirectly by getting a slice of part or all of the cursor and writing into the slice. In the
/// indirect case, the caller must call [`advance`](BorrowCursor::advance) after writing to inform
/// indirect case, the caller must call [`advance`](BorrowedCursor::advance) after writing to inform
/// the cursor how many bytes have been written.
///
/// Once data is written to the cursor, it becomes part of the filled portion of the underlying
/// `BorrowBuf` and can no longer be accessed or re-written by the cursor. I.e., the cursor tracks
/// the unfilled part of the underlying `BorrowBuf`.
/// `BorrowedBuf` and can no longer be accessed or re-written by the cursor. I.e., the cursor tracks
/// the unfilled part of the underlying `BorrowedBuf`.
///
/// The `'buf` lifetime is a bound on the lifetime of the underlying buffer. `'data` is a bound on
/// that buffer's underlying data.
#[derive(Debug)]
pub struct BorrowCursor<'buf, 'data> {
pub struct BorrowedCursor<'buf, 'data> {
/// The underlying buffer.
buf: &'buf mut BorrowBuf<'data>,
buf: &'buf mut BorrowedBuf<'data>,
/// The length of the filled portion of the underlying buffer at the time of the cursor's
/// creation.
start: usize,
}
impl<'buf, 'data> BorrowCursor<'buf, 'data> {
impl<'buf, 'data> BorrowedCursor<'buf, 'data> {
/// Clone this cursor.
///
/// Since a cursor maintains unique access to its underlying buffer, the cloned cursor is not
/// accessible while the clone is alive.
#[inline]
pub fn clone<'this>(&'this mut self) -> BorrowCursor<'this, 'data> {
BorrowCursor { buf: self.buf, start: self.start }
pub fn clone<'this>(&'this mut self) -> BorrowedCursor<'this, 'data> {
BorrowedCursor { buf: self.buf, start: self.start }
}
/// Returns the available space in the cursor.
@ -167,7 +168,7 @@ impl<'buf, 'data> BorrowCursor<'buf, 'data> {
self.buf.capacity() - self.buf.filled
}
/// Returns the number of bytes written to this cursor since it was created from a `BorrowBuf`.
/// Returns the number of bytes written to this cursor since it was created from a `BorrowedBuf`.
///
/// Note that if this cursor is a clone of another, then the count returned is the count written
/// via either cursor, not the count since the cursor was cloned.
@ -180,9 +181,7 @@ impl<'buf, 'data> BorrowCursor<'buf, 'data> {
#[inline]
pub fn init_ref(&self) -> &[u8] {
//SAFETY: We only slice the initialized part of the buffer, which is always valid
unsafe {
MaybeUninit::slice_assume_init_ref(&self.buf.buf[self.buf.filled..self.buf.initialized])
}
unsafe { MaybeUninit::slice_assume_init_ref(&self.buf.buf[self.buf.filled..self.buf.init]) }
}
/// Returns a mutable reference to the initialized portion of the cursor.
@ -190,9 +189,7 @@ impl<'buf, 'data> BorrowCursor<'buf, 'data> {
pub fn init_mut(&mut self) -> &mut [u8] {
//SAFETY: We only slice the initialized part of the buffer, which is always valid
unsafe {
MaybeUninit::slice_assume_init_mut(
&mut self.buf.buf[self.buf.filled..self.buf.initialized],
)
MaybeUninit::slice_assume_init_mut(&mut self.buf.buf[self.buf.filled..self.buf.init])
}
}
@ -201,7 +198,7 @@ impl<'buf, 'data> BorrowCursor<'buf, 'data> {
/// It is safe to uninitialize any of these bytes.
#[inline]
pub fn uninit_mut(&mut self) -> &mut [MaybeUninit<u8>] {
&mut self.buf.buf[self.buf.initialized..]
&mut self.buf.buf[self.buf.init..]
}
/// Returns a mutable reference to the whole cursor.
@ -227,7 +224,7 @@ impl<'buf, 'data> BorrowCursor<'buf, 'data> {
#[inline]
pub unsafe fn advance(&mut self, n: usize) -> &mut Self {
self.buf.filled += n;
self.buf.initialized = cmp::max(self.buf.initialized, self.buf.filled);
self.buf.init = cmp::max(self.buf.init, self.buf.filled);
self
}
@ -237,14 +234,14 @@ impl<'buf, 'data> BorrowCursor<'buf, 'data> {
for byte in self.uninit_mut() {
byte.write(0);
}
self.buf.initialized = self.buf.capacity();
self.buf.init = self.buf.capacity();
self
}
/// Asserts that the first `n` unfilled bytes of the cursor are initialized.
///
/// `BorrowBuf` assumes that bytes are never de-initialized, so this method does nothing when
/// `BorrowedBuf` 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
@ -252,7 +249,7 @@ impl<'buf, 'data> BorrowCursor<'buf, 'data> {
/// The caller must ensure that the first `n` bytes of the buffer have already been initialized.
#[inline]
pub unsafe fn set_init(&mut self, n: usize) -> &mut Self {
self.buf.initialized = cmp::max(self.buf.initialized, self.buf.filled + n);
self.buf.init = cmp::max(self.buf.init, self.buf.filled + n);
self
}
@ -272,9 +269,19 @@ impl<'buf, 'data> BorrowCursor<'buf, 'data> {
// SAFETY: We just added the entire contents of buf to the filled section.
unsafe {
self.set_init(buf.len());
}
self.buf.filled += buf.len();
}
}
impl<'buf, 'data> Write for BorrowedCursor<'buf, 'data> {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.append(buf);
Ok(buf.len())
}
fn flush(&mut self) -> Result<()> {
Ok(())
}
}

View file

@ -1,11 +1,11 @@
use super::BorrowBuf;
use super::BorrowedBuf;
use crate::mem::MaybeUninit;
/// Test that BorrowBuf has the correct numbers when created with new
/// Test that BorrowedBuf has the correct numbers when created with new
#[test]
fn new() {
let buf: &mut [_] = &mut [0; 16];
let mut rbuf: BorrowBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_> = buf.into();
assert_eq!(rbuf.filled().len(), 0);
assert_eq!(rbuf.init_len(), 16);
@ -13,11 +13,11 @@ fn new() {
assert_eq!(rbuf.unfilled().capacity(), 16);
}
/// Test that BorrowBuf has the correct numbers when created with uninit
/// Test that BorrowedBuf has the correct numbers when created with uninit
#[test]
fn uninit() {
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 16];
let mut rbuf: BorrowBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_> = buf.into();
assert_eq!(rbuf.filled().len(), 0);
assert_eq!(rbuf.init_len(), 0);
@ -28,7 +28,7 @@ fn uninit() {
#[test]
fn initialize_unfilled() {
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 16];
let mut rbuf: BorrowBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_> = buf.into();
rbuf.unfilled().ensure_init();
@ -36,9 +36,9 @@ fn initialize_unfilled() {
}
#[test]
fn add_filled() {
fn addvance_filled() {
let buf: &mut [_] = &mut [0; 16];
let mut rbuf: BorrowBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_> = buf.into();
unsafe {
rbuf.unfilled().advance(1);
@ -51,7 +51,7 @@ fn add_filled() {
#[test]
fn clear() {
let buf: &mut [_] = &mut [255; 16];
let mut rbuf: BorrowBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_> = buf.into();
unsafe {
rbuf.unfilled().advance(16);
@ -71,7 +71,7 @@ fn clear() {
#[test]
fn set_init() {
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 16];
let mut rbuf: BorrowBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_> = buf.into();
unsafe {
rbuf.set_init(8);
@ -99,7 +99,7 @@ fn set_init() {
#[test]
fn append() {
let buf: &mut [_] = &mut [MaybeUninit::new(255); 16];
let mut rbuf: BorrowBuf<'_> = buf.into();
let mut rbuf: BorrowedBuf<'_> = buf.into();
rbuf.unfilled().append(&[0; 8]);
@ -115,3 +115,61 @@ fn append() {
assert_eq!(rbuf.filled().len(), 16);
assert_eq!(rbuf.filled(), [1; 16]);
}
#[test]
fn clone_written() {
let buf: &mut [_] = &mut [MaybeUninit::new(0); 32];
let mut buf: BorrowedBuf<'_> = buf.into();
let mut cursor = buf.unfilled();
cursor.append(&[1; 16]);
let mut cursor2 = cursor.clone();
cursor2.append(&[2; 16]);
assert_eq!(cursor2.written(), 32);
assert_eq!(cursor.written(), 32);
assert_eq!(buf.unfilled().written(), 0);
assert_eq!(buf.init_len(), 32);
assert_eq!(buf.filled().len(), 32);
let filled = buf.filled();
assert_eq!(&filled[..16], [1; 16]);
assert_eq!(&filled[16..], [2; 16]);
}
#[test]
fn cursor_set_init() {
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 16];
let mut rbuf: BorrowedBuf<'_> = buf.into();
unsafe {
rbuf.unfilled().set_init(8);
}
assert_eq!(rbuf.init_len(), 8);
assert_eq!(rbuf.unfilled().init_ref().len(), 8);
assert_eq!(rbuf.unfilled().init_mut().len(), 8);
assert_eq!(rbuf.unfilled().uninit_mut().len(), 8);
assert_eq!(unsafe { rbuf.unfilled().as_mut() }.len(), 16);
unsafe {
rbuf.unfilled().advance(4);
}
unsafe {
rbuf.unfilled().set_init(2);
}
assert_eq!(rbuf.init_len(), 8);
unsafe {
rbuf.unfilled().set_init(8);
}
assert_eq!(rbuf.init_len(), 12);
assert_eq!(rbuf.unfilled().init_ref().len(), 8);
assert_eq!(rbuf.unfilled().init_mut().len(), 8);
assert_eq!(rbuf.unfilled().uninit_mut().len(), 4);
assert_eq!(unsafe { rbuf.unfilled().as_mut() }.len(), 12);
}

View file

@ -1,4 +1,4 @@
use super::{repeat, BorrowBuf, Cursor, SeekFrom};
use super::{repeat, BorrowedBuf, Cursor, SeekFrom};
use crate::cmp::{self, min};
use crate::io::{self, IoSlice, IoSliceMut};
use crate::io::{BufRead, BufReader, Read, Seek, Write};
@ -160,7 +160,7 @@ fn read_exact_slice() {
#[test]
fn read_buf_exact() {
let buf: &mut [_] = &mut [0; 4];
let mut buf: BorrowBuf<'_> = buf.into();
let mut buf: BorrowedBuf<'_> = buf.into();
let mut c = Cursor::new(&b""[..]);
assert_eq!(c.read_buf_exact(buf.unfilled()).unwrap_err().kind(), io::ErrorKind::UnexpectedEof);
@ -616,7 +616,7 @@ fn bench_take_read_buf(b: &mut test::Bencher) {
b.iter(|| {
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 64];
let mut buf: BorrowBuf<'_> = buf.into();
let mut buf: BorrowedBuf<'_> = buf.into();
[255; 128].take(64).read_buf(buf.unfilled()).unwrap();
});

View file

@ -5,7 +5,7 @@ mod tests;
use crate::fmt;
use crate::io::{
self, BorrowCursor, BufRead, IoSlice, IoSliceMut, Read, Seek, SeekFrom, SizeHint, Write,
self, BorrowedCursor, BufRead, IoSlice, IoSliceMut, Read, 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, _cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
fn read_buf(&mut self, _cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
Ok(())
}
}
@ -130,7 +130,7 @@ impl Read for Repeat {
Ok(buf.len())
}
fn read_buf(&mut self, mut buf: BorrowCursor<'_, '_>) -> io::Result<()> {
fn read_buf(&mut self, mut buf: BorrowedCursor<'_, '_>) -> io::Result<()> {
// SAFETY: No uninit bytes are being written
for slot in unsafe { buf.as_mut() } {
slot.write(self.byte);

View file

@ -1,7 +1,7 @@
use crate::cmp::{max, min};
use crate::io::prelude::*;
use crate::io::{
copy, empty, repeat, sink, BorrowBuf, BufWriter, Empty, Repeat, Result, SeekFrom, Sink,
copy, empty, repeat, sink, BorrowedBuf, BufWriter, Empty, Repeat, Result, SeekFrom, Sink,
DEFAULT_BUF_SIZE,
};
@ -80,25 +80,25 @@ fn empty_reads() {
assert_eq!(e.by_ref().read(&mut [0; 1024]).unwrap(), 0);
let buf: &mut [MaybeUninit<_>] = &mut [];
let mut buf: BorrowBuf<'_> = buf.into();
let mut buf: BorrowedBuf<'_> = buf.into();
e.read_buf(buf.unfilled()).unwrap();
assert_eq!(buf.len(), 0);
assert_eq!(buf.init_len(), 0);
let buf: &mut [_] = &mut [MaybeUninit::uninit()];
let mut buf: BorrowBuf<'_> = buf.into();
let mut buf: BorrowedBuf<'_> = buf.into();
e.read_buf(buf.unfilled()).unwrap();
assert_eq!(buf.len(), 0);
assert_eq!(buf.init_len(), 0);
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 1024];
let mut buf: BorrowBuf<'_> = buf.into();
let mut buf: BorrowedBuf<'_> = buf.into();
e.read_buf(buf.unfilled()).unwrap();
assert_eq!(buf.len(), 0);
assert_eq!(buf.init_len(), 0);
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 1024];
let mut buf: BorrowBuf<'_> = buf.into();
let mut buf: BorrowedBuf<'_> = buf.into();
e.by_ref().read_buf(buf.unfilled()).unwrap();
assert_eq!(buf.len(), 0);
assert_eq!(buf.init_len(), 0);

View file

@ -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::{BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
use crate::os::unix::ffi::OsStrExt;
use crate::path::{Path, PathBuf};
use crate::sys::cvt;
@ -312,8 +312,8 @@ impl File {
false
}
pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
crate::io::default_read_buf(|buf| self.read(buf), buf)
pub fn read_buf(&self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
crate::io::default_read_buf(|buf| self.read(buf), cursor)
}
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {

View file

@ -2,7 +2,7 @@ use super::{abi, error};
use crate::{
ffi::{CStr, CString, OsStr, OsString},
fmt,
io::{self, IoSlice, IoSliceMut, ReadBuf, SeekFrom},
io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom},
mem::MaybeUninit,
os::raw::{c_int, c_short},
os::solid::ffi::OsStrExt,
@ -358,13 +358,13 @@ impl File {
}
}
pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
pub fn read_buf(&self, mut cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
unsafe {
let len = buf.remaining();
let len = cursor.capacity();
let mut out_num_bytes = MaybeUninit::uninit();
error::SolidError::err_if_negative(abi::SOLID_FS_Read(
self.fd.raw(),
buf.unfilled_mut().as_mut_ptr() as *mut u8,
cursor.as_mut().as_mut_ptr() as *mut u8,
len,
out_num_bytes.as_mut_ptr(),
))
@ -376,9 +376,7 @@ impl File {
// Safety: `num_bytes_read` bytes were written to the unfilled
// portion of the buffer
buf.assume_init(num_bytes_read);
buf.add_filled(num_bytes_read);
cursor.advance(num_bytes_read);
Ok(())
}

View file

@ -4,7 +4,7 @@
mod tests;
use crate::cmp;
use crate::io::{self, BorrowCursor, IoSlice, IoSliceMut, Read};
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read};
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
use crate::sys::cvt;
use crate::sys_common::{AsInner, FromInner, IntoInner};
@ -131,7 +131,7 @@ impl FileDesc {
}
}
pub fn read_buf(&self, mut cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
pub fn read_buf(&self, mut cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
let ret = cvt(unsafe {
libc::read(
self.as_raw_fd(),

View file

@ -2,7 +2,7 @@ use crate::os::unix::prelude::*;
use crate::ffi::{CStr, CString, OsStr, OsString};
use crate::fmt;
use crate::io::{self, BorrowCursor, Error, IoSlice, IoSliceMut, SeekFrom};
use crate::io::{self, BorrowedCursor, Error, IoSlice, IoSliceMut, SeekFrom};
use crate::mem;
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd};
use crate::path::{Path, PathBuf};
@ -1031,7 +1031,7 @@ impl File {
self.0.read_at(buf, offset)
}
pub fn read_buf(&self, cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
pub fn read_buf(&self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
self.0.read_buf(cursor)
}

View file

@ -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, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
use crate::path::{Path, PathBuf};
use crate::sys::time::SystemTime;
use crate::sys::unsupported;
@ -214,7 +214,7 @@ impl File {
self.0
}
pub fn read_buf(&self, _buf: &mut ReadBuf<'_>) -> io::Result<()> {
pub fn read_buf(&self, _cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
self.0
}

View file

@ -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, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
use crate::iter;
use crate::mem::{self, ManuallyDrop};
use crate::os::raw::c_int;
@ -439,8 +439,8 @@ impl File {
true
}
pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
crate::io::default_read_buf(|buf| self.read(buf), buf)
pub fn read_buf(&self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
crate::io::default_read_buf(|buf| self.read(buf), cursor)
}
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {

View file

@ -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, BorrowedCursor, Error, IoSlice, IoSliceMut, SeekFrom};
use crate::mem;
use crate::os::windows::io::{AsHandle, BorrowedHandle};
use crate::path::{Path, PathBuf};
@ -415,8 +415,8 @@ impl File {
self.handle.read_at(buf, offset)
}
pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
self.handle.read_buf(buf)
pub fn read_buf(&self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
self.handle.read_buf(cursor)
}
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {

View file

@ -4,7 +4,7 @@
mod tests;
use crate::cmp;
use crate::io::{self, ErrorKind, IoSlice, IoSliceMut, Read, ReadBuf};
use crate::io::{self, BorrowedCursor, ErrorKind, IoSlice, IoSliceMut, Read};
use crate::mem;
use crate::os::windows::io::{
AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle, RawHandle,
@ -112,18 +112,16 @@ impl Handle {
}
}
pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
let res = unsafe {
self.synchronous_read(buf.unfilled_mut().as_mut_ptr(), buf.remaining(), None)
};
pub fn read_buf(&self, mut cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
let res =
unsafe { self.synchronous_read(cursor.as_mut().as_mut_ptr(), cursor.capacity(), None) };
match res {
Ok(read) => {
// Safety: `read` bytes were written to the initialized portion of the buffer
unsafe {
buf.assume_init(read as usize);
cursor.advance(read as usize);
}
buf.add_filled(read as usize);
Ok(())
}