From 589aa8e29c8b21a450ea6a78a387f6c0ac060fa4 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 31 Dec 2020 08:43:30 +0000 Subject: [PATCH] Reuse Box::try_new_*_in() in Box::new_*_in() --- library/alloc/src/boxed.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index eb00d1a8b47..4391176c8be 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -387,8 +387,7 @@ impl Box { // #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit_in(alloc: A) -> Box, A> { let layout = Layout::new::>(); - let ptr = alloc.allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout)).cast(); - unsafe { Box::from_raw_in(ptr.as_ptr(), alloc) } + Box::try_new_uninit_in(alloc).unwrap_or_else(|_| handle_alloc_error(layout)) } /// Constructs a new box with uninitialized contents in the provided allocator, @@ -445,9 +444,7 @@ impl Box { // #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_zeroed_in(alloc: A) -> Box, A> { let layout = Layout::new::>(); - let ptr = - alloc.allocate_zeroed(layout).unwrap_or_else(|_| handle_alloc_error(layout)).cast(); - unsafe { Box::from_raw_in(ptr.as_ptr(), alloc) } + Box::try_new_zeroed_in(alloc).unwrap_or_else(|_| handle_alloc_error(layout)) } /// Constructs a new `Box` with uninitialized contents, with the memory