about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2020-05-29 21:58:19 +0200
committerGitHub <noreply@github.com>2020-05-29 21:58:19 +0200
commitb387a1163691a97abb47db0b99dc6073a8076675 (patch)
tree85d34a053879fc126fc1b0e88b8e334d55195058 /src/liballoc
parent81207802a0d83a2d43cb57f3a67f1b948d3b108f (diff)
parenta8ed9aa9f016c60f5fa55326f1f6a30e2be9d83e (diff)
Rollup merge of #71095 - pickfire:box-from-array, r=dtolnay
impl From<[T; N]> for Box<[T]>

Based on https://github.com/rust-lang/rust/pull/68692
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/boxed.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs
index 8ef6090c743..8cc6f04c065 100644
--- a/src/liballoc/boxed.rs
+++ b/src/liballoc/boxed.rs
@@ -865,6 +865,25 @@ impl From<Box<str>> for Box<[u8]> {
     }
 }
 
+#[stable(feature = "box_from_array", since = "1.45.0")]
+impl<T, const N: usize> From<[T; N]> for Box<[T]>
+where
+    [T; N]: LengthAtMost32,
+{
+    /// Converts a `[T; N]` into a `Box<[T]>`
+    ///
+    /// This conversion moves the array to newly heap-allocated memory.
+    ///
+    /// # Examples
+    /// ```rust
+    /// let boxed: Box<[u8]> = Box::from([4, 2]);
+    /// println!("{:?}", boxed);
+    /// ```
+    fn from(array: [T; N]) -> Box<[T]> {
+        box array
+    }
+}
+
 #[stable(feature = "boxed_slice_try_from", since = "1.43.0")]
 impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]>
 where