Skip to content

Commit 981936b

Browse files
committed
Add inherent versions of MaybeUninit methods for slices
1 parent dd84b7d commit 981936b

File tree

18 files changed

+404
-259
lines changed

18 files changed

+404
-259
lines changed

compiler/rustc_arena/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<T> ArenaChunk<T> {
7878
// been initialized.
7979
unsafe {
8080
let slice = self.storage.as_mut();
81-
ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(&mut slice[..len]));
81+
slice[..len].assume_init_drop();
8282
}
8383
}
8484
}

library/alloc/src/collections/btree/node.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -383,9 +383,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
383383
/// Borrows a view into the keys stored in the node.
384384
pub fn keys(&self) -> &[K] {
385385
let leaf = self.into_leaf();
386-
unsafe {
387-
MaybeUninit::slice_assume_init_ref(leaf.keys.get_unchecked(..usize::from(leaf.len)))
388-
}
386+
unsafe { leaf.keys.get_unchecked(..usize::from(leaf.len)).assume_init_ref() }
389387
}
390388
}
391389

library/core/src/array/iter.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl<T, const N: usize> IntoIter<T, N> {
214214
// SAFETY: We know that all elements within `alive` are properly initialized.
215215
unsafe {
216216
let slice = self.data.get_unchecked(self.alive.clone());
217-
MaybeUninit::slice_assume_init_ref(slice)
217+
slice.assume_init_ref()
218218
}
219219
}
220220

@@ -224,7 +224,7 @@ impl<T, const N: usize> IntoIter<T, N> {
224224
// SAFETY: We know that all elements within `alive` are properly initialized.
225225
unsafe {
226226
let slice = self.data.get_unchecked_mut(self.alive.clone());
227-
MaybeUninit::slice_assume_init_mut(slice)
227+
slice.assume_init_mut()
228228
}
229229
}
230230
}
@@ -285,7 +285,7 @@ impl<T, const N: usize> Iterator for IntoIter<T, N> {
285285
// SAFETY: These elements are currently initialized, so it's fine to drop them.
286286
unsafe {
287287
let slice = self.data.get_unchecked_mut(range_to_drop);
288-
ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(slice));
288+
slice.assume_init_drop();
289289
}
290290

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

346346
NonZero::new(remaining).map_or(Ok(()), Err)

library/core/src/array/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -911,9 +911,7 @@ impl<T> Drop for Guard<'_, T> {
911911

912912
// SAFETY: this slice will contain only initialized objects.
913913
unsafe {
914-
crate::ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(
915-
self.array_mut.get_unchecked_mut(..self.initialized),
916-
));
914+
self.array_mut.get_unchecked_mut(..self.initialized).assume_init_drop();
917915
}
918916
}
919917
}

library/core/src/io/borrowed_buf.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl<'data> BorrowedBuf<'data> {
9494
// SAFETY: We only slice the filled part of the buffer, which is always valid
9595
unsafe {
9696
let buf = self.buf.get_unchecked(..self.filled);
97-
MaybeUninit::slice_assume_init_ref(buf)
97+
buf.assume_init_ref()
9898
}
9999
}
100100

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

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

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

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

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

@@ -344,7 +344,7 @@ impl<'a> BorrowedCursor<'a> {
344344

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

350350
// SAFETY: We just added the entire contents of buf to the filled section.

library/core/src/iter/adapters/filter_map.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ where
8181
if const { crate::mem::needs_drop::<T>() } {
8282
// SAFETY: self.initialized is always <= N, which also is the length of the array.
8383
unsafe {
84-
core::ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(
85-
self.array.get_unchecked_mut(..self.initialized),
86-
));
84+
self.array.get_unchecked_mut(..self.initialized).assume_init_drop();
8785
}
8886
}
8987
}

0 commit comments

Comments
 (0)