Skip to content

Commit 5d32908

Browse files
committed
Use AllocRef parameter in Box
1 parent bec0646 commit 5d32908

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

src/liballoc/boxed.rs

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ use crate::vec::Vec;
158158
#[stable(feature = "rust1", since = "1.0.0")]
159159
pub struct Box<T: ?Sized, A: AllocRef = Global>(Unique<T>, A);
160160

161-
impl<T> Box<T> {
161+
impl<T> Box<T, Global> {
162162
/// Allocates memory on the heap and then places `x` into it.
163163
///
164164
/// This doesn't actually allocate if `T` is zero-sized.
@@ -170,7 +170,7 @@ impl<T> Box<T> {
170170
/// ```
171171
#[stable(feature = "rust1", since = "1.0.0")]
172172
#[inline(always)]
173-
pub fn new(x: T) -> Box<T> {
173+
pub fn new(x: T) -> Self {
174174
box x
175175
}
176176

@@ -192,15 +192,10 @@ impl<T> Box<T> {
192192
///
193193
/// assert_eq!(*five, 5)
194194
/// ```
195+
#[inline]
195196
#[unstable(feature = "new_uninit", issue = "63291")]
196197
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)
204199
}
205200

206201
/// Constructs a new `Box` with uninitialized contents, with the memory
@@ -221,22 +216,17 @@ impl<T> Box<T> {
221216
/// ```
222217
///
223218
/// [zeroed]: ../../std/mem/union.MaybeUninit.html#method.zeroed
219+
#[inline]
224220
#[unstable(feature = "new_uninit", issue = "63291")]
225221
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)
233223
}
234224

235225
/// Constructs a new `Pin<Box<T>>`. If `T` does not implement `Unpin`, then
236226
/// `x` will be pinned in memory and unable to be moved.
237-
#[stable(feature = "pin", since = "1.33.0")]
238227
#[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> {
240230
(box x).into()
241231
}
242232
}
@@ -366,9 +356,10 @@ impl<T> Box<[T]> {
366356
///
367357
/// assert_eq!(*values, [1, 2, 3])
368358
/// ```
359+
#[inline]
369360
#[unstable(feature = "new_uninit", issue = "63291")]
370361
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)
372363
}
373364
}
374365

@@ -751,19 +742,19 @@ unsafe impl<#[may_dangle] T: ?Sized, A: AllocRef> Drop for Box<T, A> {
751742
impl<T: Default, A: AllocRef + Default> Default for Box<T, A> {
752743
/// Creates a `Box<T>`, with the `Default` value for T.
753744
fn default() -> Self {
754-
Self::new_in(T::default(), A::default())
745+
box T::default()
755746
}
756747
}
757748

758749
#[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]> {
760751
fn default() -> Self {
761-
unsafe { Self::new_uninit_slice_in(0, A::default()).assume_init() }
752+
Box::<[T; 0]>::new([])
762753
}
763754
}
764755

765756
#[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> {
767758
fn default() -> Self {
768759
unsafe { from_boxed_utf8_unchecked(Box::default()) }
769760
}
@@ -970,7 +961,7 @@ impl<T: Copy, A: AllocRef + Default> From<&[T]> for Box<[T], A> {
970961
/// ```
971962
fn from(slice: &[T]) -> Self {
972963
let len = slice.len();
973-
let buf = RawVec::with_capacity_in(len, A::default());
964+
let buf = RawVec::with_capacity_in(len, Global);
974965
unsafe {
975966
ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len);
976967
buf.into_box(slice.len()).assume_init()
@@ -1246,9 +1237,9 @@ impl<I> FromIterator<I> for Box<[I]> {
12461237
}
12471238

12481239
#[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> {
12501241
fn clone(&self) -> Self {
1251-
self.to_vec().into_boxed_slice()
1242+
self.to_vec_in(self.1.clone()).into_boxed_slice()
12521243
}
12531244
}
12541245

0 commit comments

Comments
 (0)