Skip to content

Commit 1a2122f

Browse files
committed
non-linux platforms
Signed-off-by: Nick Cameron <[email protected]>
1 parent b56cf67 commit 1a2122f

File tree

21 files changed

+205
-140
lines changed

21 files changed

+205
-140
lines changed

library/std/src/fs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod tests;
1313

1414
use crate::ffi::OsString;
1515
use crate::fmt;
16-
use crate::io::{self, BorrowCursor, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
16+
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write};
1717
use crate::path::{Path, PathBuf};
1818
use crate::sys::fs as fs_imp;
1919
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
@@ -703,7 +703,7 @@ impl Read for File {
703703
self.inner.read_vectored(bufs)
704704
}
705705

706-
fn read_buf(&mut self, cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
706+
fn read_buf(&mut self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
707707
self.inner.read_buf(cursor)
708708
}
709709

@@ -755,7 +755,7 @@ impl Read for &File {
755755
self.inner.read(buf)
756756
}
757757

758-
fn read_buf(&mut self, cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
758+
fn read_buf(&mut self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
759759
self.inner.read_buf(cursor)
760760
}
761761

library/std/src/io/buffered/bufreader.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ mod buffer;
22

33
use crate::fmt;
44
use crate::io::{
5-
self, BorrowBuf, BorrowCursor, BufRead, IoSliceMut, Read, Seek, SeekFrom, SizeHint,
6-
DEFAULT_BUF_SIZE,
5+
self, BorrowedCursor, BufRead, IoSliceMut, Read, Seek, SeekFrom, SizeHint, DEFAULT_BUF_SIZE,
76
};
87
use buffer::Buffer;
98

@@ -267,7 +266,7 @@ impl<R: Read> Read for BufReader<R> {
267266
Ok(nread)
268267
}
269268

270-
fn read_buf(&mut self, mut cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
269+
fn read_buf(&mut self, mut cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
271270
// If we don't have any buffered data and we're doing a massive read
272271
// (larger than our internal buffer), bypass our internal buffer
273272
// entirely.

library/std/src/io/buffered/bufreader/buffer.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/// that user code which wants to do reads from a `BufReader` via `buffer` + `consume` can do so
1010
/// without encountering any runtime bounds checks.
1111
use crate::cmp;
12-
use crate::io::{self, Read, ReadBuf};
12+
use crate::io::{self, BorrowedBuf, Read};
1313
use crate::mem::MaybeUninit;
1414

1515
pub struct Buffer {
@@ -93,11 +93,15 @@ impl Buffer {
9393
if self.pos >= self.filled {
9494
debug_assert!(self.pos == self.filled);
9595

96-
let mut readbuf = ReadBuf::uninit(&mut self.buf);
96+
let mut buf: BorrowedBuf<'_> = (&mut *self.buf).into();
97+
// SAFETY: `self.filled` bytes will always have been initialized.
98+
unsafe {
99+
buf.set_init(self.filled);
100+
}
97101

98-
reader.read_buf(&mut readbuf)?;
102+
reader.read_buf(buf.unfilled())?;
99103

100-
self.filled = readbuf.filled_len();
104+
self.filled = buf.len();
101105
self.pos = 0;
102106
}
103107
Ok(self.buffer())

library/std/src/io/buffered/tests.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::io::prelude::*;
2-
use crate::io::{self, BorrowBuf, BufReader, BufWriter, ErrorKind, IoSlice, LineWriter, SeekFrom};
2+
use crate::io::{
3+
self, BorrowedBuf, BufReader, BufWriter, ErrorKind, IoSlice, LineWriter, SeekFrom,
4+
};
35
use crate::mem::MaybeUninit;
46
use crate::panic;
57
use crate::sync::atomic::{AtomicUsize, Ordering};
@@ -62,31 +64,31 @@ fn test_buffered_reader_read_buf() {
6264
let mut reader = BufReader::with_capacity(2, inner);
6365

6466
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 3];
65-
let mut buf: BorrowBuf<'_> = buf.into();
67+
let mut buf: BorrowedBuf<'_> = buf.into();
6668

6769
reader.read_buf(buf.unfilled()).unwrap();
6870

6971
assert_eq!(buf.filled(), [5, 6, 7]);
7072
assert_eq!(reader.buffer(), []);
7173

7274
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 2];
73-
let mut buf: BorrowBuf<'_> = buf.into();
75+
let mut buf: BorrowedBuf<'_> = buf.into();
7476

7577
reader.read_buf(buf.unfilled()).unwrap();
7678

7779
assert_eq!(buf.filled(), [0, 1]);
7880
assert_eq!(reader.buffer(), []);
7981

8082
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 1];
81-
let mut buf: BorrowBuf<'_> = buf.into();
83+
let mut buf: BorrowedBuf<'_> = buf.into();
8284

8385
reader.read_buf(buf.unfilled()).unwrap();
8486

8587
assert_eq!(buf.filled(), [2]);
8688
assert_eq!(reader.buffer(), [3]);
8789

8890
let buf: &mut [_] = &mut [MaybeUninit::uninit(); 3];
89-
let mut buf: BorrowBuf<'_> = buf.into();
91+
let mut buf: BorrowedBuf<'_> = buf.into();
9092

9193
reader.read_buf(buf.unfilled()).unwrap();
9294

library/std/src/io/copy.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{BorrowBuf, BufWriter, ErrorKind, Read, Result, Write, DEFAULT_BUF_SIZE};
1+
use super::{BorrowedBuf, BufWriter, ErrorKind, Read, Result, Write, DEFAULT_BUF_SIZE};
22
use crate::mem::MaybeUninit;
33

44
/// Copies the entire contents of a reader into a writer.
@@ -97,7 +97,7 @@ impl<I: Write> BufferedCopySpec for BufWriter<I> {
9797

9898
loop {
9999
let buf = writer.buffer_mut();
100-
let mut read_buf: BorrowBuf<'_> = buf.spare_capacity_mut().into();
100+
let mut read_buf: BorrowedBuf<'_> = buf.spare_capacity_mut().into();
101101

102102
unsafe {
103103
// SAFETY: init is either 0 or the init_len from the previous iteration.
@@ -117,7 +117,7 @@ impl<I: Write> BufferedCopySpec for BufWriter<I> {
117117
init = read_buf.init_len() - bytes_read;
118118
len += bytes_read as u64;
119119

120-
// SAFETY: BorrowBuf guarantees all of its filled bytes are init
120+
// SAFETY: BorrowedBuf guarantees all of its filled bytes are init
121121
unsafe { buf.set_len(buf.len() + bytes_read) };
122122

123123
// 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>(
139139
writer: &mut W,
140140
) -> Result<u64> {
141141
let buf: &mut [_] = &mut [MaybeUninit::uninit(); DEFAULT_BUF_SIZE];
142-
let mut buf: BorrowBuf<'_> = buf.into();
142+
let mut buf: BorrowedBuf<'_> = buf.into();
143143

144144
let mut len = 0;
145145

library/std/src/io/cursor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::io::prelude::*;
55

66
use crate::alloc::Allocator;
77
use crate::cmp;
8-
use crate::io::{self, BorrowCursor, ErrorKind, IoSlice, IoSliceMut, SeekFrom};
8+
use crate::io::{self, BorrowedCursor, ErrorKind, IoSlice, IoSliceMut, SeekFrom};
99

1010
/// A `Cursor` wraps an in-memory buffer and provides it with a
1111
/// [`Seek`] implementation.
@@ -323,7 +323,7 @@ where
323323
Ok(n)
324324
}
325325

326-
fn read_buf(&mut self, mut cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
326+
fn read_buf(&mut self, mut cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
327327
let prev_written = cursor.written();
328328

329329
Read::read_buf(&mut self.fill_buf()?, cursor.clone())?;

library/std/src/io/impls.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::cmp;
66
use crate::collections::VecDeque;
77
use crate::fmt;
88
use crate::io::{
9-
self, BorrowCursor, BufRead, ErrorKind, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write,
9+
self, BorrowedCursor, BufRead, ErrorKind, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write,
1010
};
1111
use crate::mem;
1212

@@ -21,7 +21,7 @@ impl<R: Read + ?Sized> Read for &mut R {
2121
}
2222

2323
#[inline]
24-
fn read_buf(&mut self, cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
24+
fn read_buf(&mut self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
2525
(**self).read_buf(cursor)
2626
}
2727

@@ -125,7 +125,7 @@ impl<R: Read + ?Sized> Read for Box<R> {
125125
}
126126

127127
#[inline]
128-
fn read_buf(&mut self, cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
128+
fn read_buf(&mut self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
129129
(**self).read_buf(cursor)
130130
}
131131

@@ -249,7 +249,7 @@ impl Read for &[u8] {
249249
}
250250

251251
#[inline]
252-
fn read_buf(&mut self, mut cursor: BorrowCursor<'_, '_>) -> io::Result<()> {
252+
fn read_buf(&mut self, mut cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
253253
let amt = cmp::min(cursor.capacity(), self.len());
254254
let (a, b) = self.split_at(amt);
255255

@@ -427,10 +427,10 @@ impl<A: Allocator> Read for VecDeque<u8, A> {
427427
}
428428

429429
#[inline]
430-
fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
430+
fn read_buf(&mut self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
431431
let (ref mut front, _) = self.as_slices();
432-
let n = cmp::min(buf.remaining(), front.len());
433-
Read::read_buf(front, buf)?;
432+
let n = cmp::min(cursor.capacity(), front.len());
433+
Read::read_buf(front, cursor)?;
434434
self.drain(..n);
435435
Ok(())
436436
}

library/std/src/io/mod.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ pub use self::{
278278
};
279279

280280
#[unstable(feature = "read_buf", issue = "78485")]
281-
pub use self::readbuf::{BorrowBuf, BorrowCursor};
281+
pub use self::readbuf::{BorrowedBuf, BorrowedCursor};
282282
pub(crate) use error::const_io_error;
283283

284284
mod buffered;
@@ -362,7 +362,7 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>
362362
buf.reserve(32); // buf is full, need more space
363363
}
364364

365-
let mut read_buf: BorrowBuf<'_> = buf.spare_capacity_mut().into();
365+
let mut read_buf: BorrowedBuf<'_> = buf.spare_capacity_mut().into();
366366

367367
// SAFETY: These bytes were initialized but not filled in the previous loop
368368
unsafe {
@@ -383,7 +383,7 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>
383383
// store how much was initialized but not filled
384384
initialized = cursor.init_ref().len();
385385

386-
// SAFETY: BorrowBuf's invariants mean this much memory is initialized.
386+
// SAFETY: BorrowedBuf's invariants mean this much memory is initialized.
387387
unsafe {
388388
let new_len = read_buf.filled().len() + buf.len();
389389
buf.set_len(new_len);
@@ -462,7 +462,7 @@ pub(crate) fn default_read_exact<R: Read + ?Sized>(this: &mut R, mut buf: &mut [
462462
}
463463
}
464464

465-
pub(crate) fn default_read_buf<F>(read: F, mut cursor: BorrowCursor<'_, '_>) -> Result<()>
465+
pub(crate) fn default_read_buf<F>(read: F, mut cursor: BorrowedCursor<'_, '_>) -> Result<()>
466466
where
467467
F: FnOnce(&mut [u8]) -> Result<usize>,
468468
{
@@ -805,24 +805,23 @@ pub trait Read {
805805
default_read_exact(self, buf)
806806
}
807807

808-
// TODO naming, if should the method be read_cursor? Or should we change the names of the data structures?
809808
/// Pull some bytes from this source into the specified buffer.
810809
///
811-
/// This is equivalent to the [`read`](Read::read) method, except that it is passed a [`BorrowCursor`] rather than `[u8]` to allow use
810+
/// This is equivalent to the [`read`](Read::read) method, except that it is passed a [`BorrowedCursor`] rather than `[u8]` to allow use
812811
/// with uninitialized buffers. The new data will be appended to any existing contents of `buf`.
813812
///
814813
/// The default implementation delegates to `read`.
815814
#[unstable(feature = "read_buf", issue = "78485")]
816-
fn read_buf(&mut self, buf: BorrowCursor<'_, '_>) -> Result<()> {
815+
fn read_buf(&mut self, buf: BorrowedCursor<'_, '_>) -> Result<()> {
817816
default_read_buf(|b| self.read(b), buf)
818817
}
819818

820819
/// Read the exact number of bytes required to fill `cursor`.
821820
///
822-
/// This is equivalent to the [`read_exact`](Read::read_exact) method, except that it is passed a [`BorrowCursor`] rather than `[u8]` to
821+
/// This is equivalent to the [`read_exact`](Read::read_exact) method, except that it is passed a [`BorrowedCursor`] rather than `[u8]` to
823822
/// allow use with uninitialized buffers.
824823
#[unstable(feature = "read_buf", issue = "78485")]
825-
fn read_buf_exact(&mut self, mut cursor: BorrowCursor<'_, '_>) -> Result<()> {
824+
fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_, '_>) -> Result<()> {
826825
while cursor.capacity() > 0 {
827826
let prev_written = cursor.written();
828827
match self.read_buf(cursor.clone()) {
@@ -2587,7 +2586,7 @@ impl<T: Read> Read for Take<T> {
25872586
Ok(n)
25882587
}
25892588

2590-
fn read_buf(&mut self, mut buf: BorrowCursor<'_, '_>) -> Result<()> {
2589+
fn read_buf(&mut self, mut buf: BorrowedCursor<'_, '_>) -> Result<()> {
25912590
// Don't call into inner reader at all at EOF because it may still block
25922591
if self.limit == 0 {
25932592
return Ok(());
@@ -2602,7 +2601,7 @@ impl<T: Read> Read for Take<T> {
26022601
// SAFETY: no uninit data is written to ibuf
26032602
let ibuf = unsafe { &mut buf.as_mut()[..limit] };
26042603

2605-
let mut sliced_buf: BorrowBuf<'_> = ibuf.into();
2604+
let mut sliced_buf: BorrowedBuf<'_> = ibuf.into();
26062605

26072606
// SAFETY: extra_init bytes of ibuf are known to be initialized
26082607
unsafe {

0 commit comments

Comments
 (0)