Skip to content

Commit

Permalink
Unrolled build for rust-lang#132533
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#132533 - SUPERCILEX:patch-4, r=Mark-Simulacrum

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

See rust-lang/libs-team#473 and tracking issue rust-lang#117693.
  • Loading branch information
rust-timer authored Nov 25, 2024
2 parents 67a8c64 + 770b156 commit 039c07d
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions library/core/src/io/borrowed_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down

0 comments on commit 039c07d

Please sign in to comment.