Skip to content

Commit 589aa8e

Browse files
committed
Reuse Box::try_new_*_in() in Box::new_*_in()
1 parent bb15fa1 commit 589aa8e

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

library/alloc/src/boxed.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,7 @@ impl<T, A: Allocator> Box<T, A> {
387387
// #[unstable(feature = "new_uninit", issue = "63291")]
388388
pub fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> {
389389
let layout = Layout::new::<mem::MaybeUninit<T>>();
390-
let ptr = alloc.allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout)).cast();
391-
unsafe { Box::from_raw_in(ptr.as_ptr(), alloc) }
390+
Box::try_new_uninit_in(alloc).unwrap_or_else(|_| handle_alloc_error(layout))
392391
}
393392

394393
/// Constructs a new box with uninitialized contents in the provided allocator,
@@ -445,9 +444,7 @@ impl<T, A: Allocator> Box<T, A> {
445444
// #[unstable(feature = "new_uninit", issue = "63291")]
446445
pub fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A> {
447446
let layout = Layout::new::<mem::MaybeUninit<T>>();
448-
let ptr =
449-
alloc.allocate_zeroed(layout).unwrap_or_else(|_| handle_alloc_error(layout)).cast();
450-
unsafe { Box::from_raw_in(ptr.as_ptr(), alloc) }
447+
Box::try_new_zeroed_in(alloc).unwrap_or_else(|_| handle_alloc_error(layout))
451448
}
452449

453450
/// Constructs a new `Box` with uninitialized contents, with the memory

0 commit comments

Comments
 (0)