Skip to content

Commit fbbc558

Browse files
authored
Rollup merge of rust-lang#143829 - a1phyr:trim_borrowed_buf, r=ChrisDenton
Trim `BorrowedCursor` API This PR removes some method from the unstable `BorrowedCursor` type. A rational for each change can be found in the message of each commit. I don't think that an ACP is required for this, please tell me if it is not the case. Cc rust-lang#78485 rust-lang#117693
2 parents acdfc83 + 65df668 commit fbbc558

File tree

3 files changed

+11
-38
lines changed

3 files changed

+11
-38
lines changed

library/core/src/io/borrowed_buf.rs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ impl<'data> BorrowedBuf<'data> {
149149
#[inline]
150150
pub fn unfilled<'this>(&'this mut self) -> BorrowedCursor<'this> {
151151
BorrowedCursor {
152-
start: self.filled,
153152
// SAFETY: we never assign into `BorrowedCursor::buf`, so treating its
154153
// lifetime covariantly is safe.
155154
buf: unsafe {
@@ -205,9 +204,6 @@ pub struct BorrowedCursor<'a> {
205204
// we create a `BorrowedCursor`. This is only safe if we never replace `buf` by assigning into
206205
// it, so don't do that!
207206
buf: &'a mut BorrowedBuf<'a>,
208-
/// The length of the filled portion of the underlying buffer at the time of the cursor's
209-
/// creation.
210-
start: usize,
211207
}
212208

213209
impl<'a> BorrowedCursor<'a> {
@@ -225,7 +221,6 @@ impl<'a> BorrowedCursor<'a> {
225221
self.buf,
226222
)
227223
},
228-
start: self.start,
229224
}
230225
}
231226

@@ -235,23 +230,12 @@ impl<'a> BorrowedCursor<'a> {
235230
self.buf.capacity() - self.buf.filled
236231
}
237232

238-
/// Returns the number of bytes written to this cursor since it was created from a `BorrowedBuf`.
233+
/// Returns the number of bytes written to the `BorrowedBuf` this cursor was created from.
239234
///
240-
/// Note that if this cursor is a reborrowed clone of another, then the count returned is the
241-
/// count written via either cursor, not the count since the cursor was reborrowed.
235+
/// In particular, the count returned is shared by all reborrows of the cursor.
242236
#[inline]
243237
pub fn written(&self) -> usize {
244-
self.buf.filled - self.start
245-
}
246-
247-
/// Returns a shared reference to the initialized portion of the cursor.
248-
#[inline]
249-
pub fn init_ref(&self) -> &[u8] {
250-
// SAFETY: We only slice the initialized part of the buffer, which is always valid
251-
unsafe {
252-
let buf = self.buf.buf.get_unchecked(self.buf.filled..self.buf.init);
253-
buf.assume_init_ref()
254-
}
238+
self.buf.filled
255239
}
256240

257241
/// Returns a mutable reference to the initialized portion of the cursor.
@@ -264,15 +248,6 @@ impl<'a> BorrowedCursor<'a> {
264248
}
265249
}
266250

267-
/// Returns a mutable reference to the uninitialized part of the cursor.
268-
///
269-
/// It is safe to uninitialize any of these bytes.
270-
#[inline]
271-
pub fn uninit_mut(&mut self) -> &mut [MaybeUninit<u8>] {
272-
// SAFETY: always in bounds
273-
unsafe { self.buf.buf.get_unchecked_mut(self.buf.init..) }
274-
}
275-
276251
/// Returns a mutable reference to the whole cursor.
277252
///
278253
/// # Safety
@@ -325,7 +300,9 @@ impl<'a> BorrowedCursor<'a> {
325300
/// Initializes all bytes in the cursor.
326301
#[inline]
327302
pub fn ensure_init(&mut self) -> &mut Self {
328-
let uninit = self.uninit_mut();
303+
// SAFETY: always in bounds and we never uninitialize these bytes.
304+
let uninit = unsafe { self.buf.buf.get_unchecked_mut(self.buf.init..) };
305+
329306
// SAFETY: 0 is a valid value for MaybeUninit<u8> and the length matches the allocation
330307
// since it is comes from a slice reference.
331308
unsafe {

library/coretests/tests/io/borrowed_buf.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn clear() {
6161
assert_eq!(rbuf.filled().len(), 0);
6262
assert_eq!(rbuf.unfilled().capacity(), 16);
6363

64-
assert_eq!(rbuf.unfilled().init_ref(), [255; 16]);
64+
assert_eq!(rbuf.unfilled().init_mut(), [255; 16]);
6565
}
6666

6767
#[test]
@@ -124,7 +124,7 @@ fn reborrow_written() {
124124
assert_eq!(cursor2.written(), 32);
125125
assert_eq!(cursor.written(), 32);
126126

127-
assert_eq!(buf.unfilled().written(), 0);
127+
assert_eq!(buf.unfilled().written(), 32);
128128
assert_eq!(buf.init_len(), 32);
129129
assert_eq!(buf.filled().len(), 32);
130130
let filled = buf.filled();
@@ -142,9 +142,7 @@ fn cursor_set_init() {
142142
}
143143

144144
assert_eq!(rbuf.init_len(), 8);
145-
assert_eq!(rbuf.unfilled().init_ref().len(), 8);
146145
assert_eq!(rbuf.unfilled().init_mut().len(), 8);
147-
assert_eq!(rbuf.unfilled().uninit_mut().len(), 8);
148146
assert_eq!(unsafe { rbuf.unfilled().as_mut().len() }, 16);
149147

150148
rbuf.unfilled().advance(4);
@@ -160,9 +158,7 @@ fn cursor_set_init() {
160158
}
161159

162160
assert_eq!(rbuf.init_len(), 12);
163-
assert_eq!(rbuf.unfilled().init_ref().len(), 8);
164161
assert_eq!(rbuf.unfilled().init_mut().len(), 8);
165-
assert_eq!(rbuf.unfilled().uninit_mut().len(), 4);
166162
assert_eq!(unsafe { rbuf.unfilled().as_mut().len() }, 12);
167163
}
168164

library/std/src/io/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
489489
}
490490
};
491491

492-
let unfilled_but_initialized = cursor.init_ref().len();
492+
let unfilled_but_initialized = cursor.init_mut().len();
493493
let bytes_read = cursor.written();
494494
let was_fully_initialized = read_buf.init_len() == buf_len;
495495

@@ -3053,7 +3053,7 @@ impl<T: Read> Read for Take<T> {
30533053
// The condition above guarantees that `self.limit` fits in `usize`.
30543054
let limit = self.limit as usize;
30553055

3056-
let extra_init = cmp::min(limit, buf.init_ref().len());
3056+
let extra_init = cmp::min(limit, buf.init_mut().len());
30573057

30583058
// SAFETY: no uninit data is written to ibuf
30593059
let ibuf = unsafe { &mut buf.as_mut()[..limit] };
@@ -3068,7 +3068,7 @@ impl<T: Read> Read for Take<T> {
30683068
let mut cursor = sliced_buf.unfilled();
30693069
let result = self.inner.read_buf(cursor.reborrow());
30703070

3071-
let new_init = cursor.init_ref().len();
3071+
let new_init = cursor.init_mut().len();
30723072
let filled = sliced_buf.len();
30733073

30743074
// cursor / sliced_buf / ibuf must drop here

0 commit comments

Comments
 (0)