@@ -1455,9 +1455,35 @@ where
1455
1455
}
1456
1456
}
1457
1457
1458
+ /// Specialization trait used for `From<&[T]>`.
1459
+ trait BoxFromSlice < T > {
1460
+ fn from_slice ( slice : & [ T ] ) -> Self ;
1461
+ }
1462
+
1463
+ #[ cfg( not( no_global_oom_handling) ) ]
1464
+ impl < T : Clone > BoxFromSlice < T > for Box < [ T ] > {
1465
+ #[ inline]
1466
+ default fn from_slice ( slice : & [ T ] ) -> Self {
1467
+ slice. to_vec ( ) . into_boxed_slice ( )
1468
+ }
1469
+ }
1470
+
1471
+ #[ cfg( not( no_global_oom_handling) ) ]
1472
+ impl < T : Copy > BoxFromSlice < T > for Box < [ T ] > {
1473
+ #[ inline]
1474
+ fn from_slice ( slice : & [ T ] ) -> Self {
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
+ }
1481
+ }
1482
+ }
1483
+
1458
1484
#[ cfg( not( no_global_oom_handling) ) ]
1459
1485
#[ stable( feature = "box_from_slice" , since = "1.17.0" ) ]
1460
- impl < T : Copy > From < & [ T ] > for Box < [ T ] > {
1486
+ impl < T : Clone > From < & [ T ] > for Box < [ T ] > {
1461
1487
/// Converts a `&[T]` into a `Box<[T]>`
1462
1488
///
1463
1489
/// This conversion allocates on the heap
@@ -1471,19 +1497,15 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
1471
1497
///
1472
1498
/// println!("{boxed_slice:?}");
1473
1499
/// ```
1500
+ #[ inline]
1474
1501
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
- }
1502
+ <Self as BoxFromSlice < T > >:: from_slice ( slice)
1481
1503
}
1482
1504
}
1483
1505
1484
1506
#[ cfg( not( no_global_oom_handling) ) ]
1485
1507
#[ stable( feature = "box_from_cow" , since = "1.45.0" ) ]
1486
- impl < T : Copy > From < Cow < ' _ , [ T ] > > for Box < [ T ] > {
1508
+ impl < T : Clone > From < Cow < ' _ , [ T ] > > for Box < [ T ] > {
1487
1509
/// Converts a `Cow<'_, [T]>` into a `Box<[T]>`
1488
1510
///
1489
1511
/// When `cow` is the `Cow::Borrowed` variant, this
0 commit comments