@@ -158,7 +158,7 @@ use crate::vec::Vec;
158
158
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
159
159
pub struct Box < T : ?Sized , A : AllocRef = Global > ( Unique < T > , A ) ;
160
160
161
- impl < T > Box < T > {
161
+ impl < T > Box < T , Global > {
162
162
/// Allocates memory on the heap and then places `x` into it.
163
163
///
164
164
/// This doesn't actually allocate if `T` is zero-sized.
@@ -170,7 +170,7 @@ impl<T> Box<T> {
170
170
/// ```
171
171
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
172
172
#[ inline( always) ]
173
- pub fn new ( x : T ) -> Box < T > {
173
+ pub fn new ( x : T ) -> Self {
174
174
box x
175
175
}
176
176
@@ -192,15 +192,10 @@ impl<T> Box<T> {
192
192
///
193
193
/// assert_eq!(*five, 5)
194
194
/// ```
195
+ #[ inline]
195
196
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
196
197
pub fn new_uninit ( ) -> Box < mem:: MaybeUninit < T > > {
197
- let layout = alloc:: Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
198
- let ptr = Global
199
- . alloc ( layout, AllocInit :: Uninitialized )
200
- . unwrap_or_else ( |_| alloc:: handle_alloc_error ( layout) )
201
- . ptr
202
- . cast ( ) ;
203
- unsafe { Box :: from_raw ( ptr. as_ptr ( ) ) }
198
+ Self :: new_uninit_in ( Global )
204
199
}
205
200
206
201
/// Constructs a new `Box` with uninitialized contents, with the memory
@@ -221,22 +216,17 @@ impl<T> Box<T> {
221
216
/// ```
222
217
///
223
218
/// [zeroed]: ../../std/mem/union.MaybeUninit.html#method.zeroed
219
+ #[ inline]
224
220
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
225
221
pub fn new_zeroed ( ) -> Box < mem:: MaybeUninit < T > > {
226
- let layout = alloc:: Layout :: new :: < mem:: MaybeUninit < T > > ( ) ;
227
- let ptr = Global
228
- . alloc ( layout, AllocInit :: Zeroed )
229
- . unwrap_or_else ( |_| alloc:: handle_alloc_error ( layout) )
230
- . ptr
231
- . cast ( ) ;
232
- unsafe { Box :: from_raw ( ptr. as_ptr ( ) ) }
222
+ Self :: new_zeroed_in ( Global )
233
223
}
234
224
235
225
/// Constructs a new `Pin<Box<T>>`. If `T` does not implement `Unpin`, then
236
226
/// `x` will be pinned in memory and unable to be moved.
237
- #[ stable( feature = "pin" , since = "1.33.0" ) ]
238
227
#[ inline( always) ]
239
- pub fn pin ( x : T ) -> Pin < Box < T > > {
228
+ #[ stable( feature = "pin" , since = "1.33.0" ) ]
229
+ pub fn pin ( x : T ) -> Pin < Self > {
240
230
( box x) . into ( )
241
231
}
242
232
}
@@ -366,9 +356,10 @@ impl<T> Box<[T]> {
366
356
///
367
357
/// assert_eq!(*values, [1, 2, 3])
368
358
/// ```
359
+ #[ inline]
369
360
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
370
361
pub fn new_uninit_slice ( len : usize ) -> Box < [ mem:: MaybeUninit < T > ] > {
371
- unsafe { RawVec :: with_capacity ( len) . into_box ( len ) }
362
+ Self :: new_uninit_slice_in ( len, Global )
372
363
}
373
364
}
374
365
@@ -751,19 +742,19 @@ unsafe impl<#[may_dangle] T: ?Sized, A: AllocRef> Drop for Box<T, A> {
751
742
impl < T : Default , A : AllocRef + Default > Default for Box < T , A > {
752
743
/// Creates a `Box<T>`, with the `Default` value for T.
753
744
fn default ( ) -> Self {
754
- Self :: new_in ( T :: default ( ) , A :: default ( ) )
745
+ box T :: default ( )
755
746
}
756
747
}
757
748
758
749
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
759
- impl < T , A : AllocRef + Default > Default for Box < [ T ] , A > {
750
+ impl < T > Default for Box < [ T ] > {
760
751
fn default ( ) -> Self {
761
- unsafe { Self :: new_uninit_slice_in ( 0 , A :: default ( ) ) . assume_init ( ) }
752
+ Box :: < [ T ; 0 ] > :: new ( [ ] )
762
753
}
763
754
}
764
755
765
756
#[ stable( feature = "default_box_extra" , since = "1.17.0" ) ]
766
- impl < A : AllocRef + Default > Default for Box < str , A > {
757
+ impl Default for Box < str > {
767
758
fn default ( ) -> Self {
768
759
unsafe { from_boxed_utf8_unchecked ( Box :: default ( ) ) }
769
760
}
@@ -970,7 +961,7 @@ impl<T: Copy, A: AllocRef + Default> From<&[T]> for Box<[T], A> {
970
961
/// ```
971
962
fn from ( slice : & [ T ] ) -> Self {
972
963
let len = slice. len ( ) ;
973
- let buf = RawVec :: with_capacity_in ( len, A :: default ( ) ) ;
964
+ let buf = RawVec :: with_capacity_in ( len, Global ) ;
974
965
unsafe {
975
966
ptr:: copy_nonoverlapping ( slice. as_ptr ( ) , buf. ptr ( ) , len) ;
976
967
buf. into_box ( slice. len ( ) ) . assume_init ( )
@@ -1246,9 +1237,9 @@ impl<I> FromIterator<I> for Box<[I]> {
1246
1237
}
1247
1238
1248
1239
#[ stable( feature = "box_slice_clone" , since = "1.3.0" ) ]
1249
- impl < T : Clone > Clone for Box < [ T ] > {
1240
+ impl < T : Clone , A : AllocRef + Clone > Clone for Box < [ T ] , A > {
1250
1241
fn clone ( & self ) -> Self {
1251
- self . to_vec ( ) . into_boxed_slice ( )
1242
+ self . to_vec_in ( self . 1 . clone ( ) ) . into_boxed_slice ( )
1252
1243
}
1253
1244
}
1254
1245
0 commit comments