@@ -167,9 +167,9 @@ use core::task::{Context, Poll};
167
167
168
168
#[ cfg( not( no_global_oom_handling) ) ]
169
169
use crate :: alloc:: { handle_alloc_error, WriteCloneIntoRaw } ;
170
- use crate :: alloc:: { AllocError , Allocator , Global , Layout } ;
171
170
#[ cfg( not( no_global_oom_handling) ) ]
172
171
use crate :: borrow:: Cow ;
172
+ use crate :: falloc:: { AllocError , Allocator , Global , Layout } ;
173
173
use crate :: raw_vec:: RawVec ;
174
174
#[ cfg( not( no_global_oom_handling) ) ]
175
175
use crate :: str:: from_boxed_utf8_unchecked;
@@ -624,7 +624,9 @@ impl<T> Box<[T]> {
624
624
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
625
625
#[ must_use]
626
626
pub fn new_uninit_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
627
- unsafe { RawVec :: with_capacity ( len) . into_box ( len) }
627
+ unsafe {
628
+ <Global as Allocator >:: map_result ( RawVec :: with_capacity_in ( len, Global ) ) . into_box ( len)
629
+ }
628
630
}
629
631
630
632
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -649,7 +651,10 @@ impl<T> Box<[T]> {
649
651
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
650
652
#[ must_use]
651
653
pub fn new_zeroed_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
652
- unsafe { RawVec :: with_capacity_zeroed ( len) . into_box ( len) }
654
+ unsafe {
655
+ <Global as Allocator >:: map_result ( RawVec :: with_capacity_zeroed_in ( len, Global ) )
656
+ . into_box ( len)
657
+ }
653
658
}
654
659
655
660
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
@@ -675,14 +680,7 @@ impl<T> Box<[T]> {
675
680
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
676
681
#[ inline]
677
682
pub fn try_new_uninit_slice ( len : usize ) -> Result < Box < [ mem:: MaybeUninit < T > ] > , AllocError > {
678
- unsafe {
679
- let layout = match Layout :: array :: < mem:: MaybeUninit < T > > ( len) {
680
- Ok ( l) => l,
681
- Err ( _) => return Err ( AllocError ) ,
682
- } ;
683
- let ptr = Global . allocate ( layout) ?;
684
- Ok ( RawVec :: from_raw_parts_in ( ptr. as_mut_ptr ( ) as * mut _ , len, Global ) . into_box ( len) )
685
- }
683
+ unsafe { Ok ( RawVec :: with_capacity_in ( len, Global ) . map_err ( |_| AllocError ) ?. into_box ( len) ) }
686
684
}
687
685
688
686
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -708,12 +706,7 @@ impl<T> Box<[T]> {
708
706
#[ inline]
709
707
pub fn try_new_zeroed_slice ( len : usize ) -> Result < Box < [ mem:: MaybeUninit < T > ] > , AllocError > {
710
708
unsafe {
711
- let layout = match Layout :: array :: < mem:: MaybeUninit < T > > ( len) {
712
- Ok ( l) => l,
713
- Err ( _) => return Err ( AllocError ) ,
714
- } ;
715
- let ptr = Global . allocate_zeroed ( layout) ?;
716
- Ok ( RawVec :: from_raw_parts_in ( ptr. as_mut_ptr ( ) as * mut _ , len, Global ) . into_box ( len) )
709
+ Ok ( RawVec :: with_capacity_zeroed_in ( len, Global ) . map_err ( |_| AllocError ) ?. into_box ( len) )
717
710
}
718
711
}
719
712
}
@@ -741,12 +734,11 @@ impl<T, A: Allocator> Box<[T], A> {
741
734
///
742
735
/// assert_eq!(*values, [1, 2, 3])
743
736
/// ```
744
- #[ cfg( not( no_global_oom_handling) ) ]
745
737
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
746
738
// #[unstable(feature = "new_uninit", issue = "63291")]
747
739
#[ must_use]
748
- pub fn new_uninit_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A > {
749
- unsafe { RawVec :: with_capacity_in ( len, alloc) . into_box ( len) }
740
+ pub fn new_uninit_slice_in ( len : usize , alloc : A ) -> A :: Result < Box < [ mem:: MaybeUninit < T > ] , A > > {
741
+ unsafe { A :: map_result ( RawVec :: with_capacity_in ( len, alloc) . map ( |r| r . into_box ( len) ) ) }
750
742
}
751
743
752
744
/// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -769,12 +761,13 @@ impl<T, A: Allocator> Box<[T], A> {
769
761
/// ```
770
762
///
771
763
/// [zeroed]: mem::MaybeUninit::zeroed
772
- #[ cfg( not( no_global_oom_handling) ) ]
773
764
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
774
765
// #[unstable(feature = "new_uninit", issue = "63291")]
775
766
#[ must_use]
776
- pub fn new_zeroed_slice_in ( len : usize , alloc : A ) -> Box < [ mem:: MaybeUninit < T > ] , A > {
777
- unsafe { RawVec :: with_capacity_zeroed_in ( len, alloc) . into_box ( len) }
767
+ pub fn new_zeroed_slice_in ( len : usize , alloc : A ) -> A :: Result < Box < [ mem:: MaybeUninit < T > ] , A > > {
768
+ unsafe {
769
+ A :: map_result ( RawVec :: with_capacity_zeroed_in ( len, alloc) . map ( |r| r. into_box ( len) ) )
770
+ }
778
771
}
779
772
}
780
773
@@ -1474,7 +1467,7 @@ impl<T: Copy> BoxFromSlice<T> for Box<[T]> {
1474
1467
#[ inline]
1475
1468
fn from_slice ( slice : & [ T ] ) -> Self {
1476
1469
let len = slice. len ( ) ;
1477
- let buf = RawVec :: with_capacity ( len) ;
1470
+ let buf = < Global as Allocator > :: map_result ( RawVec :: with_capacity_in ( len, Global ) ) ;
1478
1471
unsafe {
1479
1472
ptr:: copy_nonoverlapping ( slice. as_ptr ( ) , buf. ptr ( ) , len) ;
1480
1473
buf. into_box ( slice. len ( ) ) . assume_init ( )
@@ -2014,9 +2007,8 @@ impl<I> FromIterator<I> for Box<[I]> {
2014
2007
}
2015
2008
}
2016
2009
2017
- #[ cfg( not( no_global_oom_handling) ) ]
2018
2010
#[ stable( feature = "box_slice_clone" , since = "1.3.0" ) ]
2019
- impl < T : Clone , A : Allocator + Clone > Clone for Box < [ T ] , A > {
2011
+ impl < T : Clone , A : Allocator < Result < Self > = Self > + Clone > Clone for Box < [ T ] , A > {
2020
2012
fn clone ( & self ) -> Self {
2021
2013
let alloc = Box :: allocator ( self ) . clone ( ) ;
2022
2014
self . to_vec_in ( alloc) . into_boxed_slice ( )
0 commit comments