Skip to content

Commit cdc8f06

Browse files
committed
Auto 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 de921ab + 3506832 commit cdc8f06

File tree

9 files changed

+119
-79
lines changed

9 files changed

+119
-79
lines changed

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

+15-11
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,15 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
474474

475475
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
476476
fn into_key_slice(self) -> &'a [K] {
477-
unsafe { slice::from_raw_parts(MaybeUninit::first_ptr(&self.as_leaf().keys), self.len()) }
477+
unsafe {
478+
slice::from_raw_parts(MaybeUninit::slice_as_ptr(&self.as_leaf().keys), self.len())
479+
}
478480
}
479481

480482
fn into_val_slice(self) -> &'a [V] {
481-
unsafe { slice::from_raw_parts(MaybeUninit::first_ptr(&self.as_leaf().vals), self.len()) }
483+
unsafe {
484+
slice::from_raw_parts(MaybeUninit::slice_as_ptr(&self.as_leaf().vals), self.len())
485+
}
482486
}
483487
}
484488

@@ -493,7 +497,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
493497
// SAFETY: The keys of a node must always be initialized up to length.
494498
unsafe {
495499
slice::from_raw_parts_mut(
496-
MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).keys),
500+
MaybeUninit::slice_as_mut_ptr(&mut (*self.as_leaf_mut()).keys),
497501
self.len(),
498502
)
499503
}
@@ -503,7 +507,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
503507
// SAFETY: The values of a node must always be initialized up to length.
504508
unsafe {
505509
slice::from_raw_parts_mut(
506-
MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).vals),
510+
MaybeUninit::slice_as_mut_ptr(&mut (*self.as_leaf_mut()).vals),
507511
self.len(),
508512
)
509513
}
@@ -519,10 +523,10 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
519523
let leaf = self.as_leaf_mut();
520524
// SAFETY: The keys and values of a node must always be initialized up to length.
521525
let keys = unsafe {
522-
slice::from_raw_parts_mut(MaybeUninit::first_ptr_mut(&mut (*leaf).keys), len)
526+
slice::from_raw_parts_mut(MaybeUninit::slice_as_mut_ptr(&mut (*leaf).keys), len)
523527
};
524528
let vals = unsafe {
525-
slice::from_raw_parts_mut(MaybeUninit::first_ptr_mut(&mut (*leaf).vals), len)
529+
slice::from_raw_parts_mut(MaybeUninit::slice_as_mut_ptr(&mut (*leaf).vals), len)
526530
};
527531
(keys, vals)
528532
}
@@ -536,9 +540,9 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::ValMut<'a>, K, V, Type> {
536540
let len = self.len();
537541
let leaf = self.node.as_ptr();
538542
// SAFETY: The keys and values of a node must always be initialized up to length.
539-
let keys = unsafe { slice::from_raw_parts(MaybeUninit::first_ptr(&(*leaf).keys), len) };
543+
let keys = unsafe { slice::from_raw_parts(MaybeUninit::slice_as_ptr(&(*leaf).keys), len) };
540544
let vals = unsafe {
541-
slice::from_raw_parts_mut(MaybeUninit::first_ptr_mut(&mut (*leaf).vals), len)
545+
slice::from_raw_parts_mut(MaybeUninit::slice_as_mut_ptr(&mut (*leaf).vals), len)
542546
};
543547
(keys, vals)
544548
}
@@ -617,7 +621,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
617621
slice_insert(self.vals_mut(), 0, val);
618622
slice_insert(
619623
slice::from_raw_parts_mut(
620-
MaybeUninit::first_ptr_mut(&mut self.as_internal_mut().edges),
624+
MaybeUninit::slice_as_mut_ptr(&mut self.as_internal_mut().edges),
621625
self.len() + 1,
622626
),
623627
0,
@@ -675,7 +679,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
675679
ForceResult::Internal(mut internal) => {
676680
let edge = slice_remove(
677681
slice::from_raw_parts_mut(
678-
MaybeUninit::first_ptr_mut(&mut internal.as_internal_mut().edges),
682+
MaybeUninit::slice_as_mut_ptr(&mut internal.as_internal_mut().edges),
679683
old_len + 1,
680684
),
681685
0,
@@ -962,7 +966,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
962966

963967
slice_insert(
964968
slice::from_raw_parts_mut(
965-
MaybeUninit::first_ptr_mut(&mut self.node.as_internal_mut().edges),
969+
MaybeUninit::slice_as_mut_ptr(&mut self.node.as_internal_mut().edges),
966970
self.node.len(),
967971
),
968972
self.idx + 1,

library/core/src/array/iter.rs

+2-2
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

+1-1
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

+7-4
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

+21-14
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
}

0 commit comments

Comments
 (0)