about summary refs log tree commit diff
path: root/library/alloc/src
diff options
context:
space:
mode:
authorJosh Stone <jistone@redhat.com>2022-04-11 11:43:21 -0700
committerJosh Stone <jistone@redhat.com>2022-04-11 12:14:18 -0700
commita2902ebe578c2b116d74b946f53ddfc6c1416cae (patch)
tree1fd22619d7cfcb3b99555d0f87b60af518df75f8 /library/alloc/src
parent625e4dd13a3abd0cc59807af66c3c4cd63440852 (diff)
downloadrust-a2902ebe578c2b116d74b946f53ddfc6c1416cae.tar.gz
rust-a2902ebe578c2b116d74b946f53ddfc6c1416cae.zip
impl const Default for Box<[T]> and Box<str>
Diffstat (limited to 'library/alloc/src')
-rw-r--r--library/alloc/src/boxed.rs16
1 files changed, 12 insertions, 4 deletions
diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs
index e6faf1df3a8..639e7f213ea 100644
--- a/library/alloc/src/boxed.rs
+++ b/library/alloc/src/boxed.rs
@@ -1192,17 +1192,25 @@ impl<T: Default> Default for Box<T> {
 
 #[cfg(not(no_global_oom_handling))]
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<T> Default for Box<[T]> {
+#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
+impl<T> const Default for Box<[T]> {
     fn default() -> Self {
-        Box::<[T; 0]>::new([])
+        let ptr: Unique<[T]> = Unique::<[T; 0]>::dangling();
+        Box(ptr, Global)
     }
 }
 
 #[cfg(not(no_global_oom_handling))]
 #[stable(feature = "default_box_extra", since = "1.17.0")]
-impl Default for Box<str> {
+#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
+impl const Default for Box<str> {
     fn default() -> Self {
-        unsafe { from_boxed_utf8_unchecked(Default::default()) }
+        // SAFETY: This is the same as `Unique::cast<U>` but with an unsized `U = str`.
+        let ptr: Unique<str> = unsafe {
+            let bytes: Unique<[u8]> = Unique::<[u8; 0]>::dangling();
+            Unique::new_unchecked(bytes.as_ptr() as *mut str)
+        };
+        Box(ptr, Global)
     }
 }