@@ -68,6 +68,7 @@ use core::ops::{CoerceUnsized, Deref, DerefMut};
6868use core:: ops:: { BoxPlace , Boxed , InPlace , Place , Placer } ;
6969use core:: ptr:: { self , Unique } ;
7070use core:: convert:: From ;
71+ use str:: from_boxed_utf8_unchecked;
7172
7273/// A value that represents the heap. This is the default place that the `box`
7374/// keyword allocates into when no place is supplied.
@@ -320,8 +321,7 @@ impl<T> Default for Box<[T]> {
320321#[ stable( feature = "default_box_extra" , since = "1.17.0" ) ]
321322impl Default for Box < str > {
322323 fn default ( ) -> Box < str > {
323- let default: Box < [ u8 ] > = Default :: default ( ) ;
324- unsafe { mem:: transmute ( default) }
324+ unsafe { from_boxed_utf8_unchecked ( Default :: default ( ) ) }
325325 }
326326}
327327
@@ -366,7 +366,7 @@ impl Clone for Box<str> {
366366 let buf = RawVec :: with_capacity ( len) ;
367367 unsafe {
368368 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 ( ) )
370370 }
371371 }
372372}
@@ -441,8 +441,16 @@ impl<'a, T: Copy> From<&'a [T]> for Box<[T]> {
441441#[ stable( feature = "box_from_slice" , since = "1.17.0" ) ]
442442impl < ' a > From < & ' a str > for Box < str > {
443443 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+ }
446454 }
447455}
448456
0 commit comments