about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-10-02 10:27:52 +0200
committerGitHub <noreply@github.com>2025-10-02 10:27:52 +0200
commitfbe6834ac05285566e6f298e8f73d5283f8da67e (patch)
treef881e381fde895155bbe2d27806a0411560cf306
parent8a18176c92af8ee396049b3f3b70721f2124d663 (diff)
parent8dfea22c974fe18aca8322f1c73f8455dc42c66f (diff)
downloadrust-fbe6834ac05285566e6f298e8f73d5283f8da67e.tar.gz
rust-fbe6834ac05285566e6f298e8f73d5283f8da67e.zip
Rollup merge of #147227 - edwloef:box_take, r=joboet
implement `Box::take`

Tracking issue: https://github.com/rust-lang/rust/issues/147212

I'm not entirely sure about the wording of the doc comment, if anyone has any suggestions that'd be great :)
-rw-r--r--library/alloc/src/boxed.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs
index 5a63d90b95f..49ff768bed1 100644
--- a/library/alloc/src/boxed.rs
+++ b/library/alloc/src/boxed.rs
@@ -619,6 +619,37 @@ impl<T, A: Allocator> Box<T, A> {
     pub fn into_inner(boxed: Self) -> T {
         *boxed
     }
+
+    /// Consumes the `Box` without consuming its allocation, returning the wrapped value and a `Box`
+    /// to the uninitialized memory where the wrapped value used to live.
+    ///
+    /// This can be used together with [`write`](Box::write) to reuse the allocation for multiple
+    /// boxed values.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(box_take)]
+    ///
+    /// let c = Box::new(5);
+    ///
+    /// // take the value out of the box
+    /// let (value, uninit) = Box::take(c);
+    /// assert_eq!(value, 5);
+    ///
+    /// // reuse the box for a second value
+    /// let c = Box::write(uninit, 6);
+    /// assert_eq!(*c, 6);
+    /// ```
+    #[unstable(feature = "box_take", issue = "147212")]
+    pub fn take(boxed: Self) -> (T, Box<mem::MaybeUninit<T>, A>) {
+        unsafe {
+            let (raw, alloc) = Box::into_raw_with_allocator(boxed);
+            let value = raw.read();
+            let uninit = Box::from_raw_in(raw.cast::<mem::MaybeUninit<T>>(), alloc);
+            (value, uninit)
+        }
+    }
 }
 
 impl<T> Box<[T]> {