From 840454ebd5104d2ebf65c1fd50f69a40e3a1945f Mon Sep 17 00:00:00 2001 From: Kisaragi Marine Date: Sun, 8 Dec 2024 14:36:07 +0900 Subject: [PATCH] refactor: replace `assume_every_elements_are_initialized` with `Box::<[T]>::assume_init` --- package/origlang-slice-in-box/src/lib.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/package/origlang-slice-in-box/src/lib.rs b/package/origlang-slice-in-box/src/lib.rs index 82d7036..db5a6ba 100644 --- a/package/origlang-slice-in-box/src/lib.rs +++ b/package/origlang-slice-in-box/src/lib.rs @@ -9,7 +9,7 @@ pub fn create_initialized_boxed_slice(len: usize, mut create_element: impl Fn e.write(create_element(i)); } - unsafe { assume_every_elements_are_initialized(slice) } + unsafe { slice.assume_init() } } pub fn try_create_initialized_boxed_slice(len: usize, mut try_create_element: impl FnMut(usize) -> Result) -> Result, E> { @@ -18,13 +18,5 @@ pub fn try_create_initialized_boxed_slice(len: usize, mut try_create_eleme e.write(try_create_element(i)?); } - Ok(unsafe { assume_every_elements_are_initialized(slice) }) -} - -unsafe fn assume_every_elements_are_initialized(half_baked_slice_in_box: BoxedSlice>) -> BoxedSlice { - let ptr = Box::into_raw(half_baked_slice_in_box); - // SAFETY: caller must initialize elements in the fed MaybeUninit-slice. - let slice = ptr as *mut [I]; - - unsafe { Box::from_raw(slice) } + Ok(unsafe { slice.assume_init() }) }