about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-11-25 07:01:40 +0100
committerGitHub <noreply@github.com>2024-11-25 07:01:40 +0100
commit813d3e7781eff4256e56aa9f06acfb80f071dada (patch)
tree8be8ec38788b119f75cad8fec6a01fe6fd78752f
parent4c534de677a70fa67dc04e4c1a908ec1f69b09df (diff)
parent770b15693a6a815b643ffe3aabdf2ed0ce8daa67 (diff)
downloadrust-813d3e7781eff4256e56aa9f06acfb80f071dada.tar.gz
rust-813d3e7781eff4256e56aa9f06acfb80f071dada.zip
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.
-rw-r--r--library/core/src/io/borrowed_buf.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/library/core/src/io/borrowed_buf.rs b/library/core/src/io/borrowed_buf.rs
index dbc60aa8154..4227e503ba7 100644
--- a/library/core/src/io/borrowed_buf.rs
+++ b/library/core/src/io/borrowed_buf.rs
@@ -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> {