diff options
author | edwloef <edwin.frank.loeffler@gmail.com> | 2025-10-01 16:03:46 +0200 |
---|---|---|
committer | edwloef <edwin.frank.loeffler@gmail.com> | 2025-10-01 17:21:12 +0200 |
commit | 8dfea22c974fe18aca8322f1c73f8455dc42c66f (patch) | |
tree | 8423c1d51417bbc0feca8443f375b3e778ef7e45 | |
parent | 1e1a39441bd11aba541a48ba714d939490fc7b85 (diff) | |
download | rust-8dfea22c974fe18aca8322f1c73f8455dc42c66f.tar.gz rust-8dfea22c974fe18aca8322f1c73f8455dc42c66f.zip |
implement `Box::take`
-rw-r--r-- | library/alloc/src/boxed.rs | 31 |
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]> { |