Skip to content

Commit 9be56ee

Browse files
authored
Unrolled build for rust-lang#129259
Rollup merge of rust-lang#129259 - clarfonthey:maybe_uninit_slices, r=tgross35 Add inherent versions of MaybeUninit methods for slices This is my attempt to un-stall rust-lang#63569 and rust-lang#79995, by creating methods that mirror the existing `MaybeUninit` API: ```rust impl<T> MaybeUninit<T> { pub fn write(&mut self, value: T) -> &mut T; pub fn as_bytes(&self) -> &[MaybeUninit<u8>]; pub fn as_bytes_mut(&mut self) -> &mut [MaybeUninit<u8>]; pub unsafe fn assume_init_drop(&mut self); pub unsafe fn assume_init_ref(&self) -> &T; pub unsafe fn assume_init_mut(&mut self) -> &mut T; } ``` Adding these APIs: ```rust impl<T> [MaybeUninit<T>] { // replacing copy_from_slice; renamed to avoid conflict pub fn write_copy_of_slice(&mut self, value: &[T]) -> &mut [T] where T: Copy; // replacing clone_from_slice; renamed to avoid conflict pub fn write_clone_of_slice(&mut self, value: &[T]) -> &mut [T] where T: Clone; // identical to non-slice versions; no conflict pub fn as_bytes(&self) -> &[MaybeUninit<u8>]; pub fn as_bytes_mut(&mut self) -> &mut [MaybeUninit<u8>]; pub unsafe fn assume_init_drop(&mut self); pub unsafe fn assume_init_ref(&self) -> &[T]; pub unsafe fn assume_init_mut(&mut self) -> &mut [T]; } ``` Since the `assume_init` methods are identical to those on non-slices, they feel pretty natural. The main issue with the write methods is naming, as discussed in rust-lang#79995 among other places. My rationale: * The term "write" should be in them somewhere, to mirror the other API, and this pretty much automatically makes them not collide with any other inherent slice methods. * I chose `write_clone_of_slice` and `write_copy_of_slice` since `clone` and `copy` are being used as objects here, whereas they're being used as actions in `clone_from_slice` and `copy_from_slice`. The final "weird" thing I've done in this PR is remove a link to `Vec<T>` from `assume_init_drop` (both copies, since they're effectively copied docs), since there's no good way to link to `Vec` for something that can occur both on the page for `std/primitive.slice.html` and `std/vec/struct.Vec.html`, since the code here lives in libcore and can't use intra-doc-linking to mention `Vec`. (see: rust-lang#121436) The reason why this method shows up both on `Vec<T>` and `[T]` is because the `[T]` docs are automatically inlined on `Vec<T>`'s page, since it implements `Deref`. It's unfortunate that rustdoc doesn't have a way of dealing with this at the moment, but it is what it is, and it's a reasonable compromise for now.
2 parents c0f6a1c + e37daf0 commit 9be56ee

File tree

18 files changed

+412
-310
lines changed

18 files changed

+412
-310
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)