File tree 2 files changed +18
-4
lines changed
2 files changed +18
-4
lines changed Original file line number Diff line number Diff line change @@ -1192,17 +1192,25 @@ impl<T: Default> Default for Box<T> {
1192
1192
1193
1193
#[ cfg( not( no_global_oom_handling) ) ]
1194
1194
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1195
- impl < T > Default for Box < [ T ] > {
1195
+ #[ rustc_const_unstable( feature = "const_default_impls" , issue = "87864" ) ]
1196
+ impl < T > const Default for Box < [ T ] > {
1196
1197
fn default ( ) -> Self {
1197
- Box :: < [ T ; 0 ] > :: new ( [ ] )
1198
+ let ptr: Unique < [ T ] > = Unique :: < [ T ; 0 ] > :: dangling ( ) ;
1199
+ Box ( ptr, Global )
1198
1200
}
1199
1201
}
1200
1202
1201
1203
#[ cfg( not( no_global_oom_handling) ) ]
1202
1204
#[ stable( feature = "default_box_extra" , since = "1.17.0" ) ]
1203
- impl Default for Box < str > {
1205
+ #[ rustc_const_unstable( feature = "const_default_impls" , issue = "87864" ) ]
1206
+ impl const Default for Box < str > {
1204
1207
fn default ( ) -> Self {
1205
- unsafe { from_boxed_utf8_unchecked ( Default :: default ( ) ) }
1208
+ // SAFETY: This is the same as `Unique::cast<U>` but with an unsized `U = str`.
1209
+ let ptr: Unique < str > = unsafe {
1210
+ let bytes: Unique < [ u8 ] > = Unique :: < [ u8 ; 0 ] > :: dangling ( ) ;
1211
+ Unique :: new_unchecked ( bytes. as_ptr ( ) as * mut str )
1212
+ } ;
1213
+ Box ( ptr, Global )
1206
1214
}
1207
1215
}
1208
1216
Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ pub const MY_VEC2: Vec<usize> = Default::default();
6
6
pub const MY_STRING : String = String :: new ( ) ;
7
7
pub const MY_STRING2 : String = Default :: default ( ) ;
8
8
9
+ pub const MY_BOXED_SLICE : Box < [ usize ] > = Default :: default ( ) ;
10
+ pub const MY_BOXED_STR : Box < str > = Default :: default ( ) ;
11
+
9
12
use std:: collections:: { BTreeMap , BTreeSet } ;
10
13
11
14
pub const MY_BTREEMAP : BTreeMap < usize , usize > = BTreeMap :: new ( ) ;
@@ -23,6 +26,9 @@ fn test_const() {
23
26
assert_eq ! ( MY_VEC , MY_VEC2 ) ;
24
27
assert_eq ! ( MY_STRING , MY_STRING2 ) ;
25
28
29
+ assert_eq ! ( MY_VEC , * MY_BOXED_SLICE ) ;
30
+ assert_eq ! ( MY_STRING , * MY_BOXED_STR ) ;
31
+
26
32
assert_eq ! ( MAP_LEN , 0 ) ;
27
33
assert_eq ! ( SET_LEN , 0 ) ;
28
34
assert ! ( MAP_IS_EMPTY && SET_IS_EMPTY ) ;
You can’t perform that action at this time.
0 commit comments