Rollup merge of #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/rust#78485 rust-lang/rust#117693
This commit is contained in:
León Orell Valerian Liehr 2025-07-17 03:58:32 +02:00 committed by GitHub
commit b9fd2bccfa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 11 additions and 38 deletions

View file

@ -149,7 +149,6 @@ impl<'data> BorrowedBuf<'data> {
#[inline]
pub fn unfilled<'this>(&'this mut self) -> BorrowedCursor<'this> {
BorrowedCursor {
start: self.filled,
// SAFETY: we never assign into `BorrowedCursor::buf`, so treating its
// lifetime covariantly is safe.
buf: unsafe {
@ -205,9 +204,6 @@ pub struct BorrowedCursor<'a> {
// we create a `BorrowedCursor`. This is only safe if we never replace `buf` by assigning into
// it, so don't do that!
buf: &'a mut BorrowedBuf<'a>,
/// The length of the filled portion of the underlying buffer at the time of the cursor's
/// creation.
start: usize,
}
impl<'a> BorrowedCursor<'a> {
@ -225,7 +221,6 @@ impl<'a> BorrowedCursor<'a> {
self.buf,
)
},
start: self.start,
}
}
@ -235,23 +230,12 @@ impl<'a> BorrowedCursor<'a> {
self.buf.capacity() - self.buf.filled
}
/// Returns the number of bytes written to this cursor since it was created from a `BorrowedBuf`.
/// Returns the number of bytes written to the `BorrowedBuf` this cursor was created from.
///
/// Note that if this cursor is a reborrowed clone of another, then the count returned is the
/// count written via either cursor, not the count since the cursor was reborrowed.
/// In particular, the count returned is shared by all reborrows of the cursor.
#[inline]
pub fn written(&self) -> usize {
self.buf.filled - self.start
}
/// Returns a shared reference to the initialized portion of the cursor.
#[inline]
pub fn init_ref(&self) -> &[u8] {
// SAFETY: We only slice the initialized part of the buffer, which is always valid
unsafe {
let buf = self.buf.buf.get_unchecked(self.buf.filled..self.buf.init);
buf.assume_init_ref()
}
self.buf.filled
}
/// Returns a mutable reference to the initialized portion of the cursor.
@ -264,15 +248,6 @@ impl<'a> BorrowedCursor<'a> {
}
}
/// Returns a mutable reference to the uninitialized part of the cursor.
///
/// It is safe to uninitialize any of these bytes.
#[inline]
pub fn uninit_mut(&mut self) -> &mut [MaybeUninit<u8>] {
// SAFETY: always in bounds
unsafe { self.buf.buf.get_unchecked_mut(self.buf.init..) }
}
/// Returns a mutable reference to the whole cursor.
///
/// # Safety
@ -325,7 +300,9 @@ impl<'a> BorrowedCursor<'a> {
/// Initializes all bytes in the cursor.
#[inline]
pub fn ensure_init(&mut self) -> &mut Self {
let uninit = self.uninit_mut();
// SAFETY: always in bounds and we never uninitialize these bytes.
let uninit = unsafe { self.buf.buf.get_unchecked_mut(self.buf.init..) };
// SAFETY: 0 is a valid value for MaybeUninit<u8> and the length matches the allocation
// since it is comes from a slice reference.
unsafe {

View file

@ -61,7 +61,7 @@ fn clear() {
assert_eq!(rbuf.filled().len(), 0);
assert_eq!(rbuf.unfilled().capacity(), 16);
assert_eq!(rbuf.unfilled().init_ref(), [255; 16]);
assert_eq!(rbuf.unfilled().init_mut(), [255; 16]);
}
#[test]
@ -124,7 +124,7 @@ fn reborrow_written() {
assert_eq!(cursor2.written(), 32);
assert_eq!(cursor.written(), 32);
assert_eq!(buf.unfilled().written(), 0);
assert_eq!(buf.unfilled().written(), 32);
assert_eq!(buf.init_len(), 32);
assert_eq!(buf.filled().len(), 32);
let filled = buf.filled();
@ -142,9 +142,7 @@ fn cursor_set_init() {
}
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);
rbuf.unfilled().advance(4);
@ -160,9 +158,7 @@ fn cursor_set_init() {
}
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

@ -489,7 +489,7 @@ pub(crate) fn default_read_to_end<R: Read + ?Sized>(
}
};
let unfilled_but_initialized = cursor.init_ref().len();
let unfilled_but_initialized = cursor.init_mut().len();
let bytes_read = cursor.written();
let was_fully_initialized = read_buf.init_len() == buf_len;
@ -3053,7 +3053,7 @@ impl<T: Read> Read for Take<T> {
// The condition above guarantees that `self.limit` fits in `usize`.
let limit = self.limit as usize;
let extra_init = cmp::min(limit, buf.init_ref().len());
let extra_init = cmp::min(limit, buf.init_mut().len());
// SAFETY: no uninit data is written to ibuf
let ibuf = unsafe { &mut buf.as_mut()[..limit] };
@ -3068,7 +3068,7 @@ impl<T: Read> Read for Take<T> {
let mut cursor = sliced_buf.unfilled();
let result = self.inner.read_buf(cursor.reborrow());
let new_init = cursor.init_ref().len();
let new_init = cursor.init_mut().len();
let filled = sliced_buf.len();
// cursor / sliced_buf / ibuf must drop here