From ce7de07866b5b1a6e2c5458ecf7d3470f8492fd1 Mon Sep 17 00:00:00 2001 From: Charles Lew Date: Mon, 28 Dec 2020 20:47:19 +0800 Subject: [PATCH] Add `Box::into_inner`. --- library/alloc/src/boxed.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 33b812ec59ff9..97cfae6875fdb 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -495,6 +495,23 @@ impl Box { let (raw, alloc) = Box::into_raw_with_allocator(boxed); unsafe { Box::from_raw_in(raw as *mut [T; 1], alloc) } } + + /// Consumes the `Box`, returning the wrapped value. + /// + /// # Examples + /// + /// ``` + /// #![feature(box_into_inner)] + /// + /// let c = Box::new(5); + /// + /// assert_eq!(Box::into_inner(c), 5); + /// ``` + #[unstable(feature = "box_into_inner", issue = "80437")] + #[inline] + pub fn into_inner(boxed: Self) -> T { + *boxed + } } impl Box<[T]> {