diff options
| author | bors <bors@rust-lang.org> | 2020-02-12 10:10:15 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-02-12 10:10:15 +0000 |
| commit | cd5441faf4e56d136d7c05d5eb55b4a41396edaf (patch) | |
| tree | 27e27611e789f2cf88d5efe83c1fa543dee6590f /src/liballoc/boxed.rs | |
| parent | 7cba853b4f6080bf7831169fe5632ec9b6833242 (diff) | |
| parent | 486856f75fd8c681f728ed3445e285666dbe19b9 (diff) | |
| download | rust-cd5441faf4e56d136d7c05d5eb55b4a41396edaf.tar.gz rust-cd5441faf4e56d136d7c05d5eb55b4a41396edaf.zip | |
Auto merge of #69088 - JohnTitor:rollup-x7bk7h7, r=JohnTitor
Rollup of 11 pull requests Successful merges: - #67695 (Added dyn and true keyword docs) - #68487 ([experiment] Support linking from a .rlink file) - #68554 (Split lang_items to crates `rustc_hir` and `rustc_passes`.) - #68937 (Test failure of unchecked arithmetic intrinsics in const eval) - #68947 (Python script PEP8 style guide space formatting and minor Python source cleanup) - #68999 (remove dependency on itertools) - #69026 (Remove common usage pattern from `AllocRef`) - #69027 (Add missing `_zeroed` varants to `AllocRef`) - #69058 (Preparation for allocator aware `Box`) - #69070 (Add self to .mailmap) - #69077 (Fix outdated doc comment.) Failed merges: r? @ghost
Diffstat (limited to 'src/liballoc/boxed.rs')
| -rw-r--r-- | src/liballoc/boxed.rs | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index d65aee09232..3ac4bd82a3a 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -196,12 +196,14 @@ impl<T> Box<T> { #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit() -> Box<mem::MaybeUninit<T>> { let layout = alloc::Layout::new::<mem::MaybeUninit<T>>(); - if layout.size() == 0 { - return Box(NonNull::dangling().into()); + unsafe { + let ptr = if layout.size() == 0 { + NonNull::dangling() + } else { + Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).cast() + }; + Box::from_raw(ptr.as_ptr()) } - let ptr = - unsafe { Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)) }; - Box(ptr.cast().into()) } /// Constructs a new `Box` with uninitialized contents, with the memory @@ -264,15 +266,14 @@ impl<T> Box<[T]> { #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> { let layout = alloc::Layout::array::<mem::MaybeUninit<T>>(len).unwrap(); - let ptr = if layout.size() == 0 { - NonNull::dangling() - } else { - unsafe { + unsafe { + let ptr = if layout.size() == 0 { + NonNull::dangling() + } else { Global.alloc(layout).unwrap_or_else(|_| alloc::handle_alloc_error(layout)).cast() - } - }; - let slice = unsafe { slice::from_raw_parts_mut(ptr.as_ptr(), len) }; - Box(Unique::from(slice)) + }; + Box::from_raw(slice::from_raw_parts_mut(ptr.as_ptr(), len)) + } } } @@ -308,7 +309,7 @@ impl<T> Box<mem::MaybeUninit<T>> { #[unstable(feature = "new_uninit", issue = "63291")] #[inline] pub unsafe fn assume_init(self) -> Box<T> { - Box(Box::into_unique(self).cast()) + Box::from_raw(Box::into_raw(self) as *mut T) } } @@ -346,7 +347,7 @@ impl<T> Box<[mem::MaybeUninit<T>]> { #[unstable(feature = "new_uninit", issue = "63291")] #[inline] pub unsafe fn assume_init(self) -> Box<[T]> { - Box(Unique::new_unchecked(Box::into_raw(self) as _)) + Box::from_raw(Box::into_raw(self) as *mut [T]) } } |
