Rollup merge of #132533 - SUPERCILEX:patch-4, r=Mark-Simulacrum

Add BorrowedBuf::into_filled{,_mut} methods to allow returning buffer with original lifetime

See https://github.com/rust-lang/libs-team/issues/473 and tracking issue https://github.com/rust-lang/rust/issues/117693.
This commit is contained in:
Matthias Krüger 2024-11-25 07:01:40 +01:00 committed by GitHub
commit 813d3e7781
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -108,6 +108,26 @@ impl<'data> BorrowedBuf<'data> {
}
}
/// Returns a shared reference to the filled portion of the buffer with its original lifetime.
#[inline]
pub fn into_filled(self) -> &'data [u8] {
// SAFETY: We only slice the filled part of the buffer, which is always valid
unsafe {
let buf = self.buf.get_unchecked(..self.filled);
MaybeUninit::slice_assume_init_ref(buf)
}
}
/// Returns a mutable reference to the filled portion of the buffer with its original lifetime.
#[inline]
pub fn into_filled_mut(self) -> &'data mut [u8] {
// SAFETY: We only slice the filled part of the buffer, which is always valid
unsafe {
let buf = self.buf.get_unchecked_mut(..self.filled);
MaybeUninit::slice_assume_init_mut(buf)
}
}
/// Returns a cursor over the unfilled part of the buffer.
#[inline]
pub fn unfilled<'this>(&'this mut self) -> BorrowedCursor<'this> {