Skip to content

Commit

Permalink
refactor: replace assume_every_elements_are_initialized with `Box::…
Browse files Browse the repository at this point in the history
…<[T]>::assume_init`
  • Loading branch information
KisaragiEffective committed Dec 8, 2024
1 parent 897f08f commit 840454e
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions package/origlang-slice-in-box/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn create_initialized_boxed_slice<I>(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<I, E>(len: usize, mut try_create_element: impl FnMut(usize) -> Result<I, E>) -> Result<BoxedSlice<I>, E> {
Expand All @@ -18,13 +18,5 @@ pub fn try_create_initialized_boxed_slice<I, E>(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<I>(half_baked_slice_in_box: BoxedSlice<MaybeUninit<I>>) -> BoxedSlice<I> {
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() })
}

0 comments on commit 840454e

Please sign in to comment.