@@ -1455,9 +1455,36 @@ where
14551455 }
14561456}
14571457
1458+ /// Specialization trait used for `From<&[T]>`.
1459+ #[ cfg( not( no_global_oom_handling) ) ]
1460+ trait BoxFromSlice < T > {
1461+ fn from_slice ( slice : & [ T ] ) -> Self ;
1462+ }
1463+
1464+ #[ cfg( not( no_global_oom_handling) ) ]
1465+ impl < T : Clone > BoxFromSlice < T > for Box < [ T ] > {
1466+ #[ inline]
1467+ default fn from_slice ( slice : & [ T ] ) -> Self {
1468+ slice. to_vec ( ) . into_boxed_slice ( )
1469+ }
1470+ }
1471+
1472+ #[ cfg( not( no_global_oom_handling) ) ]
1473+ impl < T : Copy > BoxFromSlice < T > for Box < [ T ] > {
1474+ #[ inline]
1475+ fn from_slice ( slice : & [ T ] ) -> Self {
1476+ let len = slice. len ( ) ;
1477+ let buf = RawVec :: with_capacity ( len) ;
1478+ unsafe {
1479+ ptr:: copy_nonoverlapping ( slice. as_ptr ( ) , buf. ptr ( ) , len) ;
1480+ buf. into_box ( slice. len ( ) ) . assume_init ( )
1481+ }
1482+ }
1483+ }
1484+
14581485#[ cfg( not( no_global_oom_handling) ) ]
14591486#[ stable( feature = "box_from_slice" , since = "1.17.0" ) ]
1460- impl < T : Copy > From < & [ T ] > for Box < [ T ] > {
1487+ impl < T : Clone > From < & [ T ] > for Box < [ T ] > {
14611488 /// Converts a `&[T]` into a `Box<[T]>`
14621489 ///
14631490 /// This conversion allocates on the heap
@@ -1471,19 +1498,15 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
14711498 ///
14721499 /// println!("{boxed_slice:?}");
14731500 /// ```
1501+ #[ inline]
14741502 fn from ( slice : & [ T ] ) -> Box < [ T ] > {
1475- let len = slice. len ( ) ;
1476- let buf = RawVec :: with_capacity ( len) ;
1477- unsafe {
1478- ptr:: copy_nonoverlapping ( slice. as_ptr ( ) , buf. ptr ( ) , len) ;
1479- buf. into_box ( slice. len ( ) ) . assume_init ( )
1480- }
1503+ <Self as BoxFromSlice < T > >:: from_slice ( slice)
14811504 }
14821505}
14831506
14841507#[ cfg( not( no_global_oom_handling) ) ]
14851508#[ stable( feature = "box_from_cow" , since = "1.45.0" ) ]
1486- impl < T : Copy > From < Cow < ' _ , [ T ] > > for Box < [ T ] > {
1509+ impl < T : Clone > From < Cow < ' _ , [ T ] > > for Box < [ T ] > {
14871510 /// Converts a `Cow<'_, [T]>` into a `Box<[T]>`
14881511 ///
14891512 /// When `cow` is the `Cow::Borrowed` variant, this
0 commit comments