]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #80438 - crlf0710:box_into_inner, r=m-ou-se
authorYuki Okushi <huyuumi.dev@gmail.com>
Wed, 10 Feb 2021 03:24:19 +0000 (12:24 +0900)
committerGitHub <noreply@github.com>
Wed, 10 Feb 2021 03:24:19 +0000 (12:24 +0900)
Add `Box::into_inner`.

This adds a `Box::into_inner` method to the `Box` type. <del>I actually suggest deprecating the compiler magic of `*b` if this gets stablized in the future.</del>

r? `@m-ou-se`

library/alloc/src/boxed.rs

index e87303749b423376fe514971d1f2efa432017cdc..354efdefc2103166f7169f8e4699ddd26c2c83a2 100644 (file)
@@ -509,6 +509,23 @@ pub fn into_boxed_slice(boxed: Self) -> Box<[T], A> {
         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<T> Box<[T]> {