Skip to content

Commit ac70aea

Browse files
committed
Address reviewer comments
Signed-off-by: Nick Cameron <[email protected]>
1 parent 1a2122f commit ac70aea

File tree

18 files changed

+74
-55
lines changed

18 files changed

+74
-55
lines changed

Diff for: library/std/src/fs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ impl Read for File {
703703
self.inner.read_vectored(bufs)
704704
}
705705

706-
fn read_buf(&mut self, cursor: BorrowedCursor<'_, '_>) -> 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: BorrowedCursor<'_, '_>) -> io::Result<()> {
758+
fn read_buf(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
759759
self.inner.read_buf(cursor)
760760
}
761761

Diff for: library/std/src/io/buffered/bufreader.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl<R: Read> Read for BufReader<R> {
266266
Ok(nread)
267267
}
268268

269-
fn read_buf(&mut self, mut cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
269+
fn read_buf(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
270270
// If we don't have any buffered data and we're doing a massive read
271271
// (larger than our internal buffer), bypass our internal buffer
272272
// entirely.
@@ -278,7 +278,7 @@ impl<R: Read> Read for BufReader<R> {
278278
let prev = cursor.written();
279279

280280
let mut rem = self.fill_buf()?;
281-
rem.read_buf(cursor.clone())?;
281+
rem.read_buf(cursor.reborrow())?;
282282

283283
self.consume(cursor.written() - prev); //slice impl of read_buf known to never unfill buf
284284

Diff for: library/std/src/io/buffered/bufreader/buffer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl Buffer {
9393
if self.pos >= self.filled {
9494
debug_assert!(self.pos == self.filled);
9595

96-
let mut buf: BorrowedBuf<'_> = (&mut *self.buf).into();
96+
let mut buf = BorrowedBuf::from(&mut *self.buf);
9797
// SAFETY: `self.filled` bytes will always have been initialized.
9898
unsafe {
9999
buf.set_init(self.filled);

Diff for: library/std/src/io/copy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<I: Write> BufferedCopySpec for BufWriter<I> {
106106

107107
if read_buf.capacity() >= DEFAULT_BUF_SIZE {
108108
let mut cursor = read_buf.unfilled();
109-
match reader.read_buf(cursor.clone()) {
109+
match reader.read_buf(cursor.reborrow()) {
110110
Ok(()) => {
111111
let bytes_read = cursor.written();
112112

Diff for: library/std/src/io/cursor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,10 @@ where
323323
Ok(n)
324324
}
325325

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

329-
Read::read_buf(&mut self.fill_buf()?, cursor.clone())?;
329+
Read::read_buf(&mut self.fill_buf()?, cursor.reborrow())?;
330330

331331
self.pos += (cursor.written() - prev_written) as u64;
332332

Diff for: library/std/src/io/impls.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl<R: Read + ?Sized> Read for &mut R {
2121
}
2222

2323
#[inline]
24-
fn read_buf(&mut self, cursor: BorrowedCursor<'_, '_>) -> 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: BorrowedCursor<'_, '_>) -> 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: BorrowedCursor<'_, '_>) -> 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,7 +427,7 @@ impl<A: Allocator> Read for VecDeque<u8, A> {
427427
}
428428

429429
#[inline]
430-
fn read_buf(&mut self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
430+
fn read_buf(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
431431
let (ref mut front, _) = self.as_slices();
432432
let n = cmp::min(cursor.capacity(), front.len());
433433
Read::read_buf(front, cursor)?;

Diff for: library/std/src/io/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>
370370
}
371371

372372
let mut cursor = read_buf.unfilled();
373-
match r.read_buf(cursor.clone()) {
373+
match r.read_buf(cursor.reborrow()) {
374374
Ok(()) => {}
375375
Err(e) if e.kind() == ErrorKind::Interrupted => continue,
376376
Err(e) => return Err(e),
@@ -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: BorrowedCursor<'_, '_>) -> 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
{
@@ -812,7 +812,7 @@ pub trait Read {
812812
///
813813
/// The default implementation delegates to `read`.
814814
#[unstable(feature = "read_buf", issue = "78485")]
815-
fn read_buf(&mut self, buf: BorrowedCursor<'_, '_>) -> Result<()> {
815+
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<()> {
816816
default_read_buf(|b| self.read(b), buf)
817817
}
818818

@@ -821,10 +821,10 @@ pub trait Read {
821821
/// This is equivalent to the [`read_exact`](Read::read_exact) method, except that it is passed a [`BorrowedCursor`] rather than `[u8]` to
822822
/// allow use with uninitialized buffers.
823823
#[unstable(feature = "read_buf", issue = "78485")]
824-
fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_, '_>) -> Result<()> {
824+
fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> Result<()> {
825825
while cursor.capacity() > 0 {
826826
let prev_written = cursor.written();
827-
match self.read_buf(cursor.clone()) {
827+
match self.read_buf(cursor.reborrow()) {
828828
Ok(()) => {}
829829
Err(e) if e.kind() == ErrorKind::Interrupted => continue,
830830
Err(e) => return Err(e),
@@ -2586,7 +2586,7 @@ impl<T: Read> Read for Take<T> {
25862586
Ok(n)
25872587
}
25882588

2589-
fn read_buf(&mut self, mut buf: BorrowedCursor<'_, '_>) -> Result<()> {
2589+
fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> Result<()> {
25902590
// Don't call into inner reader at all at EOF because it may still block
25912591
if self.limit == 0 {
25922592
return Ok(());
@@ -2609,7 +2609,7 @@ impl<T: Read> Read for Take<T> {
26092609
}
26102610

26112611
let mut cursor = sliced_buf.unfilled();
2612-
self.inner.read_buf(cursor.clone())?;
2612+
self.inner.read_buf(cursor.reborrow())?;
26132613

26142614
let new_init = cursor.init_ref().len();
26152615
let filled = sliced_buf.len();
@@ -2626,7 +2626,7 @@ impl<T: Read> Read for Take<T> {
26262626
self.limit -= filled as u64;
26272627
} else {
26282628
let written = buf.written();
2629-
self.inner.read_buf(buf.clone())?;
2629+
self.inner.read_buf(buf.reborrow())?;
26302630
self.limit -= (buf.written() - written) as u64;
26312631
}
26322632

Diff for: library/std/src/io/readbuf.rs

+42-23
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod tests;
66
use crate::cmp;
77
use crate::fmt::{self, Debug, Formatter};
88
use crate::io::{Result, Write};
9-
use crate::mem::MaybeUninit;
9+
use crate::mem::{self, MaybeUninit};
1010

1111
/// A borrowed byte buffer which is incrementally filled and initialized.
1212
///
@@ -23,9 +23,9 @@ use crate::mem::MaybeUninit;
2323
/// ```
2424
///
2525
/// A `BorrowedBuf` is created around some existing data (or capacity for data) via a unique reference
26-
/// (`&mut`). The `BorrowedBuf` can be configured (e.g., using `clear` or `set_init`), but otherwise
27-
/// is read-only. To write into the buffer, use `unfilled` to create a `BorrowedCursor`. The cursor
28-
/// has write-only access to the unfilled portion of the buffer (you can think of it like a
26+
/// (`&mut`). The `BorrowedBuf` can be configured (e.g., using `clear` or `set_init`), but cannot be
27+
/// directly written. To write into the buffer, use `unfilled` to create a `BorrowedCursor`. The cursor
28+
/// has write-only access to the unfilled portion of the buffer (you can think of it as a
2929
/// write-only iterator).
3030
///
3131
/// The lifetime `'data` is a bound on the lifetime of the underlying data.
@@ -55,7 +55,7 @@ impl<'data> From<&'data mut [u8]> for BorrowedBuf<'data> {
5555
let len = slice.len();
5656

5757
BorrowedBuf {
58-
//SAFETY: initialized data never becoming uninitialized is an invariant of BorrowedBuf
58+
// SAFETY: initialized data never becoming uninitialized is an invariant of BorrowedBuf
5959
buf: unsafe { (slice as *mut [u8]).as_uninit_slice_mut().unwrap() },
6060
filled: 0,
6161
init: len,
@@ -95,14 +95,21 @@ impl<'data> BorrowedBuf<'data> {
9595
/// Returns a shared reference to the filled portion of the buffer.
9696
#[inline]
9797
pub fn filled(&self) -> &[u8] {
98-
//SAFETY: We only slice the filled part of the buffer, which is always valid
98+
// SAFETY: We only slice the filled part of the buffer, which is always valid
9999
unsafe { MaybeUninit::slice_assume_init_ref(&self.buf[0..self.filled]) }
100100
}
101101

102102
/// Returns a cursor over the unfilled part of the buffer.
103103
#[inline]
104-
pub fn unfilled<'this>(&'this mut self) -> BorrowedCursor<'this, 'data> {
105-
BorrowedCursor { start: self.filled, buf: self }
104+
pub fn unfilled<'this>(&'this mut self) -> BorrowedCursor<'this> {
105+
BorrowedCursor {
106+
start: self.filled,
107+
// SAFETY: we never assign into `BorrowedCursor::buf`, so treating its
108+
// lifetime covariantly is safe.
109+
buf: unsafe {
110+
mem::transmute::<&'this mut BorrowedBuf<'data>, &'this mut BorrowedBuf<'this>>(self)
111+
},
112+
}
106113
}
107114

108115
/// Clears the buffer, resetting the filled region to empty.
@@ -141,25 +148,37 @@ impl<'data> BorrowedBuf<'data> {
141148
/// `BorrowedBuf` and can no longer be accessed or re-written by the cursor. I.e., the cursor tracks
142149
/// the unfilled part of the underlying `BorrowedBuf`.
143150
///
144-
/// The `'buf` lifetime is a bound on the lifetime of the underlying buffer. `'data` is a bound on
145-
/// that buffer's underlying data.
151+
/// The lifetime `'a` is a bound on the lifetime of the underlying buffer (which means it is a bound
152+
/// on the data in that buffer by transitivity).
146153
#[derive(Debug)]
147-
pub struct BorrowedCursor<'buf, 'data> {
154+
pub struct BorrowedCursor<'a> {
148155
/// The underlying buffer.
149-
buf: &'buf mut BorrowedBuf<'data>,
156+
// Safety invariant: we treat the type of buf as covariant in the lifetime of `BorrowedBuf` when
157+
// we create a `BorrowedCursor`. This is only safe if we never replace `buf` by assigning into
158+
// it, so don't do that!
159+
buf: &'a mut BorrowedBuf<'a>,
150160
/// The length of the filled portion of the underlying buffer at the time of the cursor's
151161
/// creation.
152162
start: usize,
153163
}
154164

155-
impl<'buf, 'data> BorrowedCursor<'buf, 'data> {
156-
/// Clone this cursor.
165+
impl<'a> BorrowedCursor<'a> {
166+
/// Reborrow this cursor by cloning it with a smaller lifetime.
157167
///
158-
/// Since a cursor maintains unique access to its underlying buffer, the cloned cursor is not
159-
/// accessible while the clone is alive.
168+
/// Since a cursor maintains unique access to its underlying buffer, the borrowed cursor is
169+
/// not accessible while the new cursor exists.
160170
#[inline]
161-
pub fn clone<'this>(&'this mut self) -> BorrowedCursor<'this, 'data> {
162-
BorrowedCursor { buf: self.buf, start: self.start }
171+
pub fn reborrow<'this>(&'this mut self) -> BorrowedCursor<'this> {
172+
BorrowedCursor {
173+
// SAFETY: we never assign into `BorrowedCursor::buf`, so treating its
174+
// lifetime covariantly is safe.
175+
buf: unsafe {
176+
mem::transmute::<&'this mut BorrowedBuf<'a>, &'this mut BorrowedBuf<'this>>(
177+
self.buf,
178+
)
179+
},
180+
start: self.start,
181+
}
163182
}
164183

165184
/// Returns the available space in the cursor.
@@ -170,8 +189,8 @@ impl<'buf, 'data> BorrowedCursor<'buf, 'data> {
170189

171190
/// Returns the number of bytes written to this cursor since it was created from a `BorrowedBuf`.
172191
///
173-
/// Note that if this cursor is a clone of another, then the count returned is the count written
174-
/// via either cursor, not the count since the cursor was cloned.
192+
/// Note that if this cursor is a reborrowed clone of another, then the count returned is the
193+
/// count written via either cursor, not the count since the cursor was reborrowed.
175194
#[inline]
176195
pub fn written(&self) -> usize {
177196
self.buf.filled - self.start
@@ -180,14 +199,14 @@ impl<'buf, 'data> BorrowedCursor<'buf, 'data> {
180199
/// Returns a shared reference to the initialized portion of the cursor.
181200
#[inline]
182201
pub fn init_ref(&self) -> &[u8] {
183-
//SAFETY: We only slice the initialized part of the buffer, which is always valid
202+
// SAFETY: We only slice the initialized part of the buffer, which is always valid
184203
unsafe { MaybeUninit::slice_assume_init_ref(&self.buf.buf[self.buf.filled..self.buf.init]) }
185204
}
186205

187206
/// Returns a mutable reference to the initialized portion of the cursor.
188207
#[inline]
189208
pub fn init_mut(&mut self) -> &mut [u8] {
190-
//SAFETY: We only slice the initialized part of the buffer, which is always valid
209+
// SAFETY: We only slice the initialized part of the buffer, which is always valid
191210
unsafe {
192211
MaybeUninit::slice_assume_init_mut(&mut self.buf.buf[self.buf.filled..self.buf.init])
193212
}
@@ -275,7 +294,7 @@ impl<'buf, 'data> BorrowedCursor<'buf, 'data> {
275294
}
276295
}
277296

278-
impl<'buf, 'data> Write for BorrowedCursor<'buf, 'data> {
297+
impl<'a> Write for BorrowedCursor<'a> {
279298
fn write(&mut self, buf: &[u8]) -> Result<usize> {
280299
self.append(buf);
281300
Ok(buf.len())

Diff for: library/std/src/io/readbuf/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ fn append() {
117117
}
118118

119119
#[test]
120-
fn clone_written() {
120+
fn reborrow_written() {
121121
let buf: &mut [_] = &mut [MaybeUninit::new(0); 32];
122122
let mut buf: BorrowedBuf<'_> = buf.into();
123123

124124
let mut cursor = buf.unfilled();
125125
cursor.append(&[1; 16]);
126126

127-
let mut cursor2 = cursor.clone();
127+
let mut cursor2 = cursor.reborrow();
128128
cursor2.append(&[2; 16]);
129129

130130
assert_eq!(cursor2.written(), 32);

Diff for: library/std/src/io/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Read for Empty {
4747
}
4848

4949
#[inline]
50-
fn read_buf(&mut self, _cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
50+
fn read_buf(&mut self, _cursor: BorrowedCursor<'_>) -> io::Result<()> {
5151
Ok(())
5252
}
5353
}
@@ -130,7 +130,7 @@ impl Read for Repeat {
130130
Ok(buf.len())
131131
}
132132

133-
fn read_buf(&mut self, mut buf: BorrowedCursor<'_, '_>) -> io::Result<()> {
133+
fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> io::Result<()> {
134134
// SAFETY: No uninit bytes are being written
135135
for slot in unsafe { buf.as_mut() } {
136136
slot.write(self.byte);

Diff for: library/std/src/sys/hermit/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl File {
312312
false
313313
}
314314

315-
pub fn read_buf(&self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
315+
pub fn read_buf(&self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
316316
crate::io::default_read_buf(|buf| self.read(buf), cursor)
317317
}
318318

Diff for: library/std/src/sys/solid/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl File {
358358
}
359359
}
360360

361-
pub fn read_buf(&self, mut cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
361+
pub fn read_buf(&self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
362362
unsafe {
363363
let len = cursor.capacity();
364364
let mut out_num_bytes = MaybeUninit::uninit();

Diff for: library/std/src/sys/unix/fd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl FileDesc {
131131
}
132132
}
133133

134-
pub fn read_buf(&self, mut cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
134+
pub fn read_buf(&self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> {
135135
let ret = cvt(unsafe {
136136
libc::read(
137137
self.as_raw_fd(),

Diff for: library/std/src/sys/unix/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ impl File {
10311031
self.0.read_at(buf, offset)
10321032
}
10331033

1034-
pub fn read_buf(&self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
1034+
pub fn read_buf(&self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
10351035
self.0.read_buf(cursor)
10361036
}
10371037

Diff for: library/std/src/sys/unsupported/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl File {
214214
self.0
215215
}
216216

217-
pub fn read_buf(&self, _cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
217+
pub fn read_buf(&self, _cursor: BorrowedCursor<'_>) -> io::Result<()> {
218218
self.0
219219
}
220220

Diff for: library/std/src/sys/wasi/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl File {
439439
true
440440
}
441441

442-
pub fn read_buf(&self, cursor: BorrowedCursor<'_, '_>) -> io::Result<()> {
442+
pub fn read_buf(&self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
443443
crate::io::default_read_buf(|buf| self.read(buf), cursor)
444444
}
445445

0 commit comments

Comments
 (0)