@@ -68,6 +68,7 @@ use core::ops::{CoerceUnsized, Deref, DerefMut};
68
68
use core:: ops:: { BoxPlace , Boxed , InPlace , Place , Placer } ;
69
69
use core:: ptr:: { self , Unique } ;
70
70
use core:: convert:: From ;
71
+ use str:: from_boxed_utf8_unchecked;
71
72
72
73
/// A value that represents the heap. This is the default place that the `box`
73
74
/// keyword allocates into when no place is supplied.
@@ -320,8 +321,7 @@ impl<T> Default for Box<[T]> {
320
321
#[ stable( feature = "default_box_extra" , since = "1.17.0" ) ]
321
322
impl Default for Box < str > {
322
323
fn default ( ) -> Box < str > {
323
- let default: Box < [ u8 ] > = Default :: default ( ) ;
324
- unsafe { mem:: transmute ( default) }
324
+ unsafe { from_boxed_utf8_unchecked ( Default :: default ( ) ) }
325
325
}
326
326
}
327
327
@@ -366,7 +366,7 @@ impl Clone for Box<str> {
366
366
let buf = RawVec :: with_capacity ( len) ;
367
367
unsafe {
368
368
ptr:: copy_nonoverlapping ( self . as_ptr ( ) , buf. ptr ( ) , len) ;
369
- mem :: transmute ( buf. into_box ( ) ) // bytes to str ~magic
369
+ from_boxed_utf8_unchecked ( buf. into_box ( ) )
370
370
}
371
371
}
372
372
}
@@ -441,8 +441,16 @@ impl<'a, T: Copy> From<&'a [T]> for Box<[T]> {
441
441
#[ stable( feature = "box_from_slice" , since = "1.17.0" ) ]
442
442
impl < ' a > From < & ' a str > for Box < str > {
443
443
fn from ( s : & ' a str ) -> Box < str > {
444
- let boxed: Box < [ u8 ] > = Box :: from ( s. as_bytes ( ) ) ;
445
- unsafe { mem:: transmute ( boxed) }
444
+ unsafe { from_boxed_utf8_unchecked ( Box :: from ( s. as_bytes ( ) ) ) }
445
+ }
446
+ }
447
+
448
+ #[ stable( feature = "boxed_str_conv" , since = "1.18.0" ) ]
449
+ impl From < Box < str > > for Box < [ u8 ] > {
450
+ fn from ( s : Box < str > ) -> Self {
451
+ unsafe {
452
+ mem:: transmute ( s)
453
+ }
446
454
}
447
455
}
448
456
0 commit comments