Skip to content

Trim BorrowedCursor API #143829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 6 additions & 29 deletions library/core/src/io/borrowed_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,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 {
Expand Down Expand Up @@ -188,9 +187,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> {
Expand All @@ -208,7 +204,6 @@ impl<'a> BorrowedCursor<'a> {
self.buf,
)
},
start: self.start,
}
}

Expand All @@ -218,23 +213,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.
Expand All @@ -247,15 +231,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
Expand Down Expand Up @@ -308,7 +283,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 {
Expand Down
8 changes: 2 additions & 6 deletions library/coretests/tests/io/borrowed_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -160,8 +158,6 @@ 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);
}
6 changes: 3 additions & 3 deletions library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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] };
Expand All @@ -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
Expand Down
Loading