Skip to content

Commit c191b36

Browse files
authored
Rollup merge of rust-lang#76217 - RalfJung:maybe-uninit-slice, r=KodrAus
rename MaybeUninit slice methods The `first` methods conceptually point to the whole slice, not just its first element, so rename them to be consistent with the raw ptr methods on ref-slices. Also, do the equivalent of rust-lang#76047 for the slice reference getters, and make them part of rust-lang#63569 (so far they somehow had no tracking issue). * first_ptr -> slice_as_ptr * first_ptr_mut -> slice_as_mut_ptr * slice_get_ref -> slice_assume_init_ref * slice_get_mut -> slice_assume_init_mut
2 parents 5734041 + 0e0a47d commit c191b36

File tree

6 files changed

+48
-34
lines changed

6 files changed

+48
-34
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,15 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
461461

462462
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
463463
fn into_key_slice(self) -> &'a [K] {
464-
unsafe { slice::from_raw_parts(MaybeUninit::first_ptr(&self.as_leaf().keys), self.len()) }
464+
unsafe {
465+
slice::from_raw_parts(MaybeUninit::slice_as_ptr(&self.as_leaf().keys), self.len())
466+
}
465467
}
466468

467469
fn into_val_slice(self) -> &'a [V] {
468-
unsafe { slice::from_raw_parts(MaybeUninit::first_ptr(&self.as_leaf().vals), self.len()) }
470+
unsafe {
471+
slice::from_raw_parts(MaybeUninit::slice_as_ptr(&self.as_leaf().vals), self.len())
472+
}
469473
}
470474
}
471475

@@ -480,7 +484,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
480484
// SAFETY: The keys of a node must always be initialized up to length.
481485
unsafe {
482486
slice::from_raw_parts_mut(
483-
MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).keys),
487+
MaybeUninit::slice_as_mut_ptr(&mut (*self.as_leaf_mut()).keys),
484488
self.len(),
485489
)
486490
}
@@ -490,7 +494,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
490494
// SAFETY: The values of a node must always be initialized up to length.
491495
unsafe {
492496
slice::from_raw_parts_mut(
493-
MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).vals),
497+
MaybeUninit::slice_as_mut_ptr(&mut (*self.as_leaf_mut()).vals),
494498
self.len(),
495499
)
496500
}
@@ -506,10 +510,10 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
506510
let leaf = self.as_leaf_mut();
507511
// SAFETY: The keys and values of a node must always be initialized up to length.
508512
let keys = unsafe {
509-
slice::from_raw_parts_mut(MaybeUninit::first_ptr_mut(&mut (*leaf).keys), len)
513+
slice::from_raw_parts_mut(MaybeUninit::slice_as_mut_ptr(&mut (*leaf).keys), len)
510514
};
511515
let vals = unsafe {
512-
slice::from_raw_parts_mut(MaybeUninit::first_ptr_mut(&mut (*leaf).vals), len)
516+
slice::from_raw_parts_mut(MaybeUninit::slice_as_mut_ptr(&mut (*leaf).vals), len)
513517
};
514518
(keys, vals)
515519
}
@@ -588,7 +592,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
588592
slice_insert(self.vals_mut(), 0, val);
589593
slice_insert(
590594
slice::from_raw_parts_mut(
591-
MaybeUninit::first_ptr_mut(&mut self.as_internal_mut().edges),
595+
MaybeUninit::slice_as_mut_ptr(&mut self.as_internal_mut().edges),
592596
self.len() + 1,
593597
),
594598
0,
@@ -646,7 +650,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
646650
ForceResult::Internal(mut internal) => {
647651
let edge = slice_remove(
648652
slice::from_raw_parts_mut(
649-
MaybeUninit::first_ptr_mut(&mut internal.as_internal_mut().edges),
653+
MaybeUninit::slice_as_mut_ptr(&mut internal.as_internal_mut().edges),
650654
old_len + 1,
651655
),
652656
0,
@@ -933,7 +937,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
933937

934938
slice_insert(
935939
slice::from_raw_parts_mut(
936-
MaybeUninit::first_ptr_mut(&mut self.node.as_internal_mut().edges),
940+
MaybeUninit::slice_as_mut_ptr(&mut self.node.as_internal_mut().edges),
937941
self.node.len(),
938942
),
939943
self.idx + 1,

library/core/src/array/iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<T, const N: usize> IntoIter<T, N> {
7373
// SAFETY: We know that all elements within `alive` are properly initialized.
7474
unsafe {
7575
let slice = self.data.get_unchecked(self.alive.clone());
76-
MaybeUninit::slice_get_ref(slice)
76+
MaybeUninit::slice_assume_init_ref(slice)
7777
}
7878
}
7979

@@ -82,7 +82,7 @@ impl<T, const N: usize> IntoIter<T, N> {
8282
// SAFETY: We know that all elements within `alive` are properly initialized.
8383
unsafe {
8484
let slice = self.data.get_unchecked_mut(self.alive.clone());
85-
MaybeUninit::slice_get_mut(slice)
85+
MaybeUninit::slice_assume_init_mut(slice)
8686
}
8787
}
8888
}

library/core/src/array/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ impl<T, const N: usize> [T; N] {
410410
}
411411
let mut dst = MaybeUninit::uninit_array::<N>();
412412
let mut guard: Guard<U, N> =
413-
Guard { dst: MaybeUninit::first_ptr_mut(&mut dst), initialized: 0 };
413+
Guard { dst: MaybeUninit::slice_as_mut_ptr(&mut dst), initialized: 0 };
414414
for (src, dst) in IntoIter::new(self).zip(&mut dst) {
415415
dst.write(f(src));
416416
guard.initialized += 1;

library/core/src/fmt/num.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ trait GenericRadix {
8585
// SAFETY: The only chars in `buf` are created by `Self::digit` which are assumed to be
8686
// valid UTF-8
8787
let buf = unsafe {
88-
str::from_utf8_unchecked(slice::from_raw_parts(MaybeUninit::first_ptr(buf), buf.len()))
88+
str::from_utf8_unchecked(slice::from_raw_parts(
89+
MaybeUninit::slice_as_ptr(buf),
90+
buf.len(),
91+
))
8992
};
9093
f.pad_integral(is_nonnegative, Self::PREFIX, buf)
9194
}
@@ -192,7 +195,7 @@ macro_rules! impl_Display {
192195
// 2^128 is about 3*10^38, so 39 gives an extra byte of space
193196
let mut buf = [MaybeUninit::<u8>::uninit(); 39];
194197
let mut curr = buf.len() as isize;
195-
let buf_ptr = MaybeUninit::first_ptr_mut(&mut buf);
198+
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
196199
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
197200

198201
// SAFETY: Since `d1` and `d2` are always less than or equal to `198`, we
@@ -322,7 +325,7 @@ macro_rules! impl_Exp {
322325
// that `curr >= 0`.
323326
let mut buf = [MaybeUninit::<u8>::uninit(); 40];
324327
let mut curr = buf.len() as isize; //index for buf
325-
let buf_ptr = MaybeUninit::first_ptr_mut(&mut buf);
328+
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
326329
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
327330

328331
// decode 2 chars at a time
@@ -370,7 +373,7 @@ macro_rules! impl_Exp {
370373

371374
// stores 'e' (or 'E') and the up to 2-digit exponent
372375
let mut exp_buf = [MaybeUninit::<u8>::uninit(); 3];
373-
let exp_ptr = MaybeUninit::first_ptr_mut(&mut exp_buf);
376+
let exp_ptr = MaybeUninit::slice_as_mut_ptr(&mut exp_buf);
374377
// SAFETY: In either case, `exp_buf` is written within bounds and `exp_ptr[..len]`
375378
// is contained within `exp_buf` since `len <= 3`.
376379
let exp_slice = unsafe {

library/core/src/mem/maybe_uninit.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use crate::fmt;
33
use crate::intrinsics;
44
use crate::mem::ManuallyDrop;
55

6-
// ignore-tidy-undocumented-unsafe
7-
86
/// A wrapper type to construct uninitialized instances of `T`.
97
///
108
/// # Initialization invariant
@@ -281,7 +279,7 @@ impl<T> MaybeUninit<T> {
281279
/// # Examples
282280
///
283281
/// ```no_run
284-
/// #![feature(maybe_uninit_uninit_array, maybe_uninit_extra, maybe_uninit_slice_assume_init)]
282+
/// #![feature(maybe_uninit_uninit_array, maybe_uninit_extra, maybe_uninit_slice)]
285283
///
286284
/// use std::mem::MaybeUninit;
287285
///
@@ -293,7 +291,7 @@ impl<T> MaybeUninit<T> {
293291
/// fn read(buf: &mut [MaybeUninit<u8>]) -> &[u8] {
294292
/// unsafe {
295293
/// let len = read_into_buffer(buf.as_mut_ptr() as *mut u8, buf.len());
296-
/// MaybeUninit::slice_get_ref(&buf[..len])
294+
/// MaybeUninit::slice_assume_init_ref(&buf[..len])
297295
/// }
298296
/// }
299297
///
@@ -303,6 +301,7 @@ impl<T> MaybeUninit<T> {
303301
#[unstable(feature = "maybe_uninit_uninit_array", issue = "none")]
304302
#[inline(always)]
305303
pub fn uninit_array<const LEN: usize>() -> [Self; LEN] {
304+
// SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid.
306305
unsafe { MaybeUninit::<[MaybeUninit<T>; LEN]>::uninit().assume_init() }
307306
}
308307

@@ -354,6 +353,7 @@ impl<T> MaybeUninit<T> {
354353
#[rustc_diagnostic_item = "maybe_uninit_zeroed"]
355354
pub fn zeroed() -> MaybeUninit<T> {
356355
let mut u = MaybeUninit::<T>::uninit();
356+
// SAFETY: `u.as_mut_ptr()` points to allocated memory.
357357
unsafe {
358358
u.as_mut_ptr().write_bytes(0u8, 1);
359359
}
@@ -367,10 +367,9 @@ impl<T> MaybeUninit<T> {
367367
#[unstable(feature = "maybe_uninit_extra", issue = "63567")]
368368
#[inline(always)]
369369
pub fn write(&mut self, val: T) -> &mut T {
370-
unsafe {
371-
self.value = ManuallyDrop::new(val);
372-
self.assume_init_mut()
373-
}
370+
*self = MaybeUninit::new(val);
371+
// SAFETY: We just initialized this value.
372+
unsafe { self.assume_init_mut() }
374373
}
375374

376375
/// Gets a pointer to the contained value. Reading from this pointer or turning it
@@ -769,9 +768,13 @@ impl<T> MaybeUninit<T> {
769768
/// It is up to the caller to guarantee that the `MaybeUninit<T>` elements
770769
/// really are in an initialized state.
771770
/// Calling this when the content is not yet fully initialized causes undefined behavior.
772-
#[unstable(feature = "maybe_uninit_slice_assume_init", issue = "none")]
771+
///
772+
/// See [`assume_init_ref`] for more details and examples.
773+
///
774+
/// [`assume_init_ref`]: MaybeUninit::assume_init_ref
775+
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
773776
#[inline(always)]
774-
pub unsafe fn slice_get_ref(slice: &[Self]) -> &[T] {
777+
pub unsafe fn slice_assume_init_ref(slice: &[Self]) -> &[T] {
775778
// SAFETY: casting slice to a `*const [T]` is safe since the caller guarantees that
776779
// `slice` is initialized, and`MaybeUninit` is guaranteed to have the same layout as `T`.
777780
// The pointer obtained is valid since it refers to memory owned by `slice` which is a
@@ -786,9 +789,13 @@ impl<T> MaybeUninit<T> {
786789
/// It is up to the caller to guarantee that the `MaybeUninit<T>` elements
787790
/// really are in an initialized state.
788791
/// Calling this when the content is not yet fully initialized causes undefined behavior.
789-
#[unstable(feature = "maybe_uninit_slice_assume_init", issue = "none")]
792+
///
793+
/// See [`assume_init_mut`] for more details and examples.
794+
///
795+
/// [`assume_init_mut`]: MaybeUninit::assume_init_mut
796+
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
790797
#[inline(always)]
791-
pub unsafe fn slice_get_mut(slice: &mut [Self]) -> &mut [T] {
798+
pub unsafe fn slice_assume_init_mut(slice: &mut [Self]) -> &mut [T] {
792799
// SAFETY: similar to safety notes for `slice_get_ref`, but we have a
793800
// mutable reference which is also guaranteed to be valid for writes.
794801
unsafe { &mut *(slice as *mut [Self] as *mut [T]) }
@@ -797,14 +804,14 @@ impl<T> MaybeUninit<T> {
797804
/// Gets a pointer to the first element of the array.
798805
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
799806
#[inline(always)]
800-
pub fn first_ptr(this: &[MaybeUninit<T>]) -> *const T {
807+
pub fn slice_as_ptr(this: &[MaybeUninit<T>]) -> *const T {
801808
this as *const [MaybeUninit<T>] as *const T
802809
}
803810

804811
/// Gets a mutable pointer to the first element of the array.
805812
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
806813
#[inline(always)]
807-
pub fn first_ptr_mut(this: &mut [MaybeUninit<T>]) -> *mut T {
814+
pub fn slice_as_mut_ptr(this: &mut [MaybeUninit<T>]) -> *mut T {
808815
this as *mut [MaybeUninit<T>] as *mut T
809816
}
810817
}

library/core/src/slice/sort.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ where
299299

300300
if start_l == end_l {
301301
// Trace `block_l` elements from the left side.
302-
start_l = MaybeUninit::first_ptr_mut(&mut offsets_l);
303-
end_l = MaybeUninit::first_ptr_mut(&mut offsets_l);
302+
start_l = MaybeUninit::slice_as_mut_ptr(&mut offsets_l);
303+
end_l = MaybeUninit::slice_as_mut_ptr(&mut offsets_l);
304304
let mut elem = l;
305305

306306
for i in 0..block_l {
@@ -325,8 +325,8 @@ where
325325

326326
if start_r == end_r {
327327
// Trace `block_r` elements from the right side.
328-
start_r = MaybeUninit::first_ptr_mut(&mut offsets_r);
329-
end_r = MaybeUninit::first_ptr_mut(&mut offsets_r);
328+
start_r = MaybeUninit::slice_as_mut_ptr(&mut offsets_r);
329+
end_r = MaybeUninit::slice_as_mut_ptr(&mut offsets_r);
330330
let mut elem = r;
331331

332332
for i in 0..block_r {

0 commit comments

Comments
 (0)