Skip to content

Commit f2e9b40

Browse files
committed
Nuke slice_as{,_mut}_ptr methods
Signed-off-by: Alex Saveau <[email protected]>
1 parent e1c29d1 commit f2e9b40

File tree

4 files changed

+42
-47
lines changed

4 files changed

+42
-47
lines changed

compiler/rustc_serialize/src/opaque.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl FileEncoder {
160160
// SAFETY: The above check and `flush` ensures that there is enough
161161
// room to write the input to the buffer.
162162
unsafe {
163-
*MaybeUninit::slice_as_mut_ptr(&mut self.buf).add(buffered) = value;
163+
self.buf.get_unchecked_mut(buffered).write(value);
164164
}
165165

166166
self.buffered = buffered + 1;
@@ -182,7 +182,7 @@ impl FileEncoder {
182182
// room to write the input to the buffer.
183183
unsafe {
184184
let src = buf.as_ptr();
185-
let dst = MaybeUninit::slice_as_mut_ptr(&mut self.buf).add(buffered);
185+
let dst = self.buf.get_unchecked_mut(buffered).as_mut_ptr();
186186
ptr::copy_nonoverlapping(src, dst, buf_len);
187187
}
188188

library/core/src/fmt/num.rs

+16-27
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,7 @@ unsafe trait GenericRadix: Sized {
106106
// SAFETY: The only chars in `buf` are created by `Self::digit` which are assumed to be
107107
// valid UTF-8
108108
let buf = unsafe {
109-
str::from_utf8_unchecked(slice::from_raw_parts(
110-
MaybeUninit::slice_as_ptr(buf),
111-
buf.len(),
112-
))
109+
str::from_utf8_unchecked(slice::from_raw_parts(buf.as_ptr().into_inner(), buf.len()))
113110
};
114111
f.pad_integral(is_nonnegative, Self::PREFIX, buf)
115112
}
@@ -216,7 +213,7 @@ macro_rules! impl_Display {
216213
// 2^128 is about 3*10^38, so 39 gives an extra byte of space
217214
let mut buf = [MaybeUninit::<u8>::uninit(); 39];
218215
let mut curr = buf.len();
219-
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
216+
let buf_ptr = buf.as_mut_ptr().into_inner();
220217
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
221218

222219
// SAFETY: Since `d1` and `d2` are always less than or equal to `198`, we
@@ -344,7 +341,7 @@ macro_rules! impl_Exp {
344341
// that `curr >= 0`.
345342
let mut buf = [MaybeUninit::<u8>::uninit(); 40];
346343
let mut curr = buf.len(); //index for buf
347-
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
344+
let buf_ptr = buf.as_mut_ptr().into_inner();
348345
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
349346

350347
// decode 2 chars at a time
@@ -392,20 +389,19 @@ macro_rules! impl_Exp {
392389

393390
// stores 'e' (or 'E') and the up to 2-digit exponent
394391
let mut exp_buf = [MaybeUninit::<u8>::uninit(); 3];
395-
let exp_ptr = MaybeUninit::slice_as_mut_ptr(&mut exp_buf);
396-
// SAFETY: In either case, `exp_buf` is written within bounds and `exp_ptr[..len]`
397-
// is contained within `exp_buf` since `len <= 3`.
398-
let exp_slice = unsafe {
399-
*exp_ptr.add(0) = if upper { b'E' } else { b'e' };
392+
exp_buf[0].write(if upper { b'E' } else { b'e' });
393+
let exp_slice = {
400394
let len = if exponent < 10 {
401-
*exp_ptr.add(1) = (exponent as u8) + b'0';
395+
exp_buf[1].write((exponent as u8) + b'0');
402396
2
403397
} else {
404398
let off = exponent << 1;
405-
ptr::copy_nonoverlapping(lut_ptr.add(off), exp_ptr.add(1), 2);
399+
// SAFETY: 1 + 2 <= 3
400+
unsafe { ptr::copy_nonoverlapping(lut_ptr.add(off), exp_buf[1].as_mut_ptr(), 2); }
406401
3
407402
};
408-
slice::from_raw_parts(exp_ptr, len)
403+
// SAFETY: max(2, 3) <= 3
404+
unsafe { slice::from_raw_parts(exp_buf.as_mut_ptr().into_inner(), len) }
409405
};
410406

411407
let parts = &[
@@ -485,7 +481,7 @@ impl_Exp!(i128, u128 as u128 via to_u128 named exp_u128);
485481

486482
/// Helper function for writing a u64 into `buf` going from last to first, with `curr`.
487483
fn parse_u64_into<const N: usize>(mut n: u64, buf: &mut [MaybeUninit<u8>; N], curr: &mut usize) {
488-
let buf_ptr = MaybeUninit::slice_as_mut_ptr(buf);
484+
let buf_ptr = buf.as_mut_ptr().into_inner();
489485
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
490486
assert!(*curr > 19);
491487

@@ -609,11 +605,7 @@ fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::R
609605
// SAFETY: Guaranteed that we wrote at most 19 bytes, and there must be space
610606
// remaining since it has length 39
611607
unsafe {
612-
ptr::write_bytes(
613-
MaybeUninit::slice_as_mut_ptr(&mut buf).add(target),
614-
b'0',
615-
curr - target,
616-
);
608+
ptr::write_bytes(buf[target].as_mut_ptr(), b'0', curr - target);
617609
}
618610
curr = target;
619611

@@ -622,24 +614,21 @@ fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::R
622614
// Should this following branch be annotated with unlikely?
623615
if n != 0 {
624616
let target = buf.len() - 38;
625-
// The raw `buf_ptr` pointer is only valid until `buf` is used the next time,
626-
// buf `buf` is not used in this scope so we are good.
627-
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
628617
// SAFETY: At this point we wrote at most 38 bytes, pad up to that point,
629618
// There can only be at most 1 digit remaining.
630619
unsafe {
631-
ptr::write_bytes(buf_ptr.add(target), b'0', curr - target);
632-
curr = target - 1;
633-
*buf_ptr.add(curr) = (n as u8) + b'0';
620+
ptr::write_bytes(buf[target].as_mut_ptr(), b'0', curr - target);
634621
}
622+
curr = target - 1;
623+
buf[curr].write((n as u8) + b'0');
635624
}
636625
}
637626

638627
// SAFETY: `curr` > 0 (since we made `buf` large enough), and all the chars are valid
639628
// UTF-8 since `DEC_DIGITS_LUT` is
640629
let buf_slice = unsafe {
641630
str::from_utf8_unchecked(slice::from_raw_parts(
642-
MaybeUninit::slice_as_mut_ptr(&mut buf).add(curr),
631+
buf.get_unchecked_mut(curr).as_mut_ptr(),
643632
buf.len() - curr,
644633
))
645634
};

library/core/src/mem/maybe_uninit.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -993,22 +993,6 @@ impl<T> MaybeUninit<T> {
993993
unsafe { &mut *(slice as *mut [Self] as *mut [T]) }
994994
}
995995

996-
/// Gets a pointer to the first element of the array.
997-
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
998-
#[rustc_const_unstable(feature = "maybe_uninit_slice", issue = "63569")]
999-
#[inline(always)]
1000-
pub const fn slice_as_ptr(this: &[MaybeUninit<T>]) -> *const T {
1001-
this.as_ptr() as *const T
1002-
}
1003-
1004-
/// Gets a mutable pointer to the first element of the array.
1005-
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
1006-
#[rustc_const_unstable(feature = "maybe_uninit_slice", issue = "63569")]
1007-
#[inline(always)]
1008-
pub const fn slice_as_mut_ptr(this: &mut [MaybeUninit<T>]) -> *mut T {
1009-
this.as_mut_ptr() as *mut T
1010-
}
1011-
1012996
/// Copies the elements from `src` to `this`, returning a mutable reference to the now initialized contents of `this`.
1013997
///
1014998
/// If `T` does not implement `Copy`, use [`write_slice_cloned`]
@@ -1310,3 +1294,25 @@ impl<T, const N: usize> [MaybeUninit<T>; N] {
13101294
unsafe { intrinsics::transmute_unchecked(self) }
13111295
}
13121296
}
1297+
1298+
impl<T> *const MaybeUninit<T> {
1299+
/// Converts a MaybeUninit pointer to its underlying type.
1300+
///
1301+
/// See [`MaybeUninit::as_ptr`] for caveats.
1302+
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
1303+
#[rustc_const_unstable(feature = "maybe_uninit_slice", issue = "63569")]
1304+
pub const fn into_inner(self) -> *const T {
1305+
self.cast()
1306+
}
1307+
}
1308+
1309+
impl<T> *mut MaybeUninit<T> {
1310+
/// Converts mutable a MaybeUninit pointer to its underlying type.
1311+
///
1312+
/// See [`MaybeUninit::as_mut_ptr`] for caveats.
1313+
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
1314+
#[rustc_const_unstable(feature = "maybe_uninit_slice", issue = "63569")]
1315+
pub const fn into_inner(self) -> *mut T {
1316+
self.cast()
1317+
}
1318+
}

library/core/src/slice/sort.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ where
376376

377377
if start_l == end_l {
378378
// Trace `block_l` elements from the left side.
379-
start_l = MaybeUninit::slice_as_mut_ptr(&mut offsets_l);
379+
start_l = offsets_l.as_mut_ptr().into_inner();
380380
end_l = start_l;
381381
let mut elem = l;
382382

@@ -402,7 +402,7 @@ where
402402

403403
if start_r == end_r {
404404
// Trace `block_r` elements from the right side.
405-
start_r = MaybeUninit::slice_as_mut_ptr(&mut offsets_r);
405+
start_r = offsets_r.as_mut_ptr().into_inner();
406406
end_r = start_r;
407407
let mut elem = r;
408408

0 commit comments

Comments
 (0)