Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 117ad4f

Browse files
authoredNov 25, 2024
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.
2 parents 6b141ee + e883a60 commit 117ad4f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
 

‎core/src/io/borrowed_buf.rs

+20
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ impl<'data> BorrowedBuf<'data> {
108108
}
109109
}
110110

111+
/// Returns a shared reference to the filled portion of the buffer with its original lifetime.
112+
#[inline]
113+
pub fn into_filled(self) -> &'data [u8] {
114+
// SAFETY: We only slice the filled part of the buffer, which is always valid
115+
unsafe {
116+
let buf = self.buf.get_unchecked(..self.filled);
117+
MaybeUninit::slice_assume_init_ref(buf)
118+
}
119+
}
120+
121+
/// Returns a mutable reference to the filled portion of the buffer with its original lifetime.
122+
#[inline]
123+
pub fn into_filled_mut(self) -> &'data mut [u8] {
124+
// SAFETY: We only slice the filled part of the buffer, which is always valid
125+
unsafe {
126+
let buf = self.buf.get_unchecked_mut(..self.filled);
127+
MaybeUninit::slice_assume_init_mut(buf)
128+
}
129+
}
130+
111131
/// Returns a cursor over the unfilled part of the buffer.
112132
#[inline]
113133
pub fn unfilled<'this>(&'this mut self) -> BorrowedCursor<'this> {

0 commit comments

Comments
 (0)
Failed to load comments.