Skip to content

Commit dddc5ff

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 e88e908 commit dddc5ff

File tree

6 files changed

+44
-28
lines changed

6 files changed

+44
-28
lines changed

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

+13-9
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

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

library/core/src/slice/sort.rs

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