Skip to content

Commit cff5f56

Browse files
committed
rename MaybeUninit slice methods
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
1 parent 81a769f commit cff5f56

File tree

9 files changed

+115
-73
lines changed

9 files changed

+115
-73
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

+17-8
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ impl<T> MaybeUninit<T> {
281281
/// # Examples
282282
///
283283
/// ```no_run
284-
/// #![feature(maybe_uninit_uninit_array, maybe_uninit_extra, maybe_uninit_slice_assume_init)]
284+
/// #![feature(maybe_uninit_uninit_array, maybe_uninit_extra, maybe_uninit_slice)]
285285
///
286286
/// use std::mem::MaybeUninit;
287287
///
@@ -293,7 +293,7 @@ impl<T> MaybeUninit<T> {
293293
/// fn read(buf: &mut [MaybeUninit<u8>]) -> &[u8] {
294294
/// unsafe {
295295
/// let len = read_into_buffer(buf.as_mut_ptr() as *mut u8, buf.len());
296-
/// MaybeUninit::slice_get_ref(&buf[..len])
296+
/// MaybeUninit::slice_assume_init_ref(&buf[..len])
297297
/// }
298298
/// }
299299
///
@@ -303,6 +303,7 @@ impl<T> MaybeUninit<T> {
303303
#[unstable(feature = "maybe_uninit_uninit_array", issue = "none")]
304304
#[inline(always)]
305305
pub fn uninit_array<const LEN: usize>() -> [Self; LEN] {
306+
// SAFETY: An uninitialized `[MaybeUninit<_>; LEN]` is valid.
306307
unsafe { MaybeUninit::<[MaybeUninit<T>; LEN]>::uninit().assume_init() }
307308
}
308309

@@ -769,9 +770,13 @@ impl<T> MaybeUninit<T> {
769770
/// It is up to the caller to guarantee that the `MaybeUninit<T>` elements
770771
/// really are in an initialized state.
771772
/// Calling this when the content is not yet fully initialized causes undefined behavior.
772-
#[unstable(feature = "maybe_uninit_slice_assume_init", issue = "none")]
773+
///
774+
/// See [`assume_init_ref`] for more details and examples.
775+
///
776+
/// [`assume_init_ref`]: MaybeUninit::assume_init_ref
777+
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
773778
#[inline(always)]
774-
pub unsafe fn slice_get_ref(slice: &[Self]) -> &[T] {
779+
pub unsafe fn slice_assume_init_ref(slice: &[Self]) -> &[T] {
775780
// SAFETY: casting slice to a `*const [T]` is safe since the caller guarantees that
776781
// `slice` is initialized, and`MaybeUninit` is guaranteed to have the same layout as `T`.
777782
// The pointer obtained is valid since it refers to memory owned by `slice` which is a
@@ -786,9 +791,13 @@ impl<T> MaybeUninit<T> {
786791
/// It is up to the caller to guarantee that the `MaybeUninit<T>` elements
787792
/// really are in an initialized state.
788793
/// Calling this when the content is not yet fully initialized causes undefined behavior.
789-
#[unstable(feature = "maybe_uninit_slice_assume_init", issue = "none")]
794+
///
795+
/// See [`assume_init_mut`] for more details and examples.
796+
///
797+
/// [`assume_init_mut`]: MaybeUninit::assume_init_mut
798+
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
790799
#[inline(always)]
791-
pub unsafe fn slice_get_mut(slice: &mut [Self]) -> &mut [T] {
800+
pub unsafe fn slice_assume_init_mut(slice: &mut [Self]) -> &mut [T] {
792801
// SAFETY: similar to safety notes for `slice_get_ref`, but we have a
793802
// mutable reference which is also guaranteed to be valid for writes.
794803
unsafe { &mut *(slice as *mut [Self] as *mut [T]) }
@@ -797,14 +806,14 @@ impl<T> MaybeUninit<T> {
797806
/// Gets a pointer to the first element of the array.
798807
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
799808
#[inline(always)]
800-
pub fn first_ptr(this: &[MaybeUninit<T>]) -> *const T {
809+
pub fn slice_as_ptr(this: &[MaybeUninit<T>]) -> *const T {
801810
this as *const [MaybeUninit<T>] as *const T
802811
}
803812

804813
/// Gets a mutable pointer to the first element of the array.
805814
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
806815
#[inline(always)]
807-
pub fn first_ptr_mut(this: &mut [MaybeUninit<T>]) -> *mut T {
816+
pub fn slice_as_mut_ptr(this: &mut [MaybeUninit<T>]) -> *mut T {
808817
this as *mut [MaybeUninit<T>] as *mut T
809818
}
810819
}

0 commit comments

Comments
 (0)