Skip to content

Commit

Permalink
Add inherent versions of MaybeUninit methods for slices
Browse files Browse the repository at this point in the history
  • Loading branch information
clarfonthey authored and gitbot committed Feb 20, 2025
1 parent d01e0c8 commit f3d4a4e
Show file tree
Hide file tree
Showing 17 changed files with 411 additions and 309 deletions.
4 changes: 1 addition & 3 deletions alloc/src/collections/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
/// Borrows a view into the keys stored in the node.
pub fn keys(&self) -> &[K] {
let leaf = self.into_leaf();
unsafe {
MaybeUninit::slice_assume_init_ref(leaf.keys.get_unchecked(..usize::from(leaf.len)))
}
unsafe { leaf.keys.get_unchecked(..usize::from(leaf.len)).assume_init_ref() }
}
}

Expand Down
8 changes: 4 additions & 4 deletions core/src/array/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl<T, const N: usize> IntoIter<T, N> {
// SAFETY: We know that all elements within `alive` are properly initialized.
unsafe {
let slice = self.data.get_unchecked(self.alive.clone());
MaybeUninit::slice_assume_init_ref(slice)
slice.assume_init_ref()
}
}

Expand All @@ -224,7 +224,7 @@ impl<T, const N: usize> IntoIter<T, N> {
// SAFETY: We know that all elements within `alive` are properly initialized.
unsafe {
let slice = self.data.get_unchecked_mut(self.alive.clone());
MaybeUninit::slice_assume_init_mut(slice)
slice.assume_init_mut()
}
}
}
Expand Down Expand Up @@ -285,7 +285,7 @@ impl<T, const N: usize> Iterator for IntoIter<T, N> {
// SAFETY: These elements are currently initialized, so it's fine to drop them.
unsafe {
let slice = self.data.get_unchecked_mut(range_to_drop);
ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(slice));
slice.assume_init_drop();
}

NonZero::new(remaining).map_or(Ok(()), Err)
Expand Down Expand Up @@ -340,7 +340,7 @@ impl<T, const N: usize> DoubleEndedIterator for IntoIter<T, N> {
// SAFETY: These elements are currently initialized, so it's fine to drop them.
unsafe {
let slice = self.data.get_unchecked_mut(range_to_drop);
ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(slice));
slice.assume_init_drop();
}

NonZero::new(remaining).map_or(Ok(()), Err)
Expand Down
4 changes: 1 addition & 3 deletions core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,7 @@ impl<T> Drop for Guard<'_, T> {

// SAFETY: this slice will contain only initialized objects.
unsafe {
crate::ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(
self.array_mut.get_unchecked_mut(..self.initialized),
));
self.array_mut.get_unchecked_mut(..self.initialized).assume_init_drop();
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions core/src/io/borrowed_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl<'data> BorrowedBuf<'data> {
// SAFETY: We only slice the filled part of the buffer, which is always valid
unsafe {
let buf = self.buf.get_unchecked(..self.filled);
MaybeUninit::slice_assume_init_ref(buf)
buf.assume_init_ref()
}
}

Expand All @@ -104,7 +104,7 @@ impl<'data> BorrowedBuf<'data> {
// SAFETY: We only slice the filled part of the buffer, which is always valid
unsafe {
let buf = self.buf.get_unchecked_mut(..self.filled);
MaybeUninit::slice_assume_init_mut(buf)
buf.assume_init_mut()
}
}

Expand All @@ -114,7 +114,7 @@ impl<'data> BorrowedBuf<'data> {
// SAFETY: We only slice the filled part of the buffer, which is always valid
unsafe {
let buf = self.buf.get_unchecked(..self.filled);
MaybeUninit::slice_assume_init_ref(buf)
buf.assume_init_ref()
}
}

Expand All @@ -124,7 +124,7 @@ impl<'data> BorrowedBuf<'data> {
// SAFETY: We only slice the filled part of the buffer, which is always valid
unsafe {
let buf = self.buf.get_unchecked_mut(..self.filled);
MaybeUninit::slice_assume_init_mut(buf)
buf.assume_init_mut()
}
}

Expand Down Expand Up @@ -233,7 +233,7 @@ impl<'a> BorrowedCursor<'a> {
// SAFETY: We only slice the initialized part of the buffer, which is always valid
unsafe {
let buf = self.buf.buf.get_unchecked(self.buf.filled..self.buf.init);
MaybeUninit::slice_assume_init_ref(buf)
buf.assume_init_ref()
}
}

Expand All @@ -243,7 +243,7 @@ impl<'a> BorrowedCursor<'a> {
// SAFETY: We only slice the initialized part of the buffer, which is always valid
unsafe {
let buf = self.buf.buf.get_unchecked_mut(self.buf.filled..self.buf.init);
MaybeUninit::slice_assume_init_mut(buf)
buf.assume_init_mut()
}
}

Expand Down Expand Up @@ -344,7 +344,7 @@ impl<'a> BorrowedCursor<'a> {

// SAFETY: we do not de-initialize any of the elements of the slice
unsafe {
MaybeUninit::copy_from_slice(&mut self.as_mut()[..buf.len()], buf);
self.as_mut()[..buf.len()].write_copy_of_slice(buf);
}

// SAFETY: We just added the entire contents of buf to the filled section.
Expand Down
4 changes: 1 addition & 3 deletions core/src/iter/adapters/filter_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ where
if const { crate::mem::needs_drop::<T>() } {
// SAFETY: self.initialized is always <= N, which also is the length of the array.
unsafe {
core::ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(
self.array.get_unchecked_mut(..self.initialized),
));
self.array.get_unchecked_mut(..self.initialized).assume_init_drop();
}
}
}
Expand Down
Loading

0 comments on commit f3d4a4e

Please sign in to comment.