Skip to content

Commit

Permalink
Remove type safe MaybeUninit ptr conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
  • Loading branch information
SUPERCILEX committed Jun 30, 2023
1 parent cc18694 commit b602bbf
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 32 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_serialize/src/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl FileEncoder {
// room to write the input to the buffer.
unsafe {
let src = buf.as_ptr();
let dst = self.buf.as_mut_ptr().add(buffered).into_inner();
let dst = self.buf.as_mut_ptr().add(buffered).cast::<u8>();
ptr::copy_nonoverlapping(src, dst, buf_len);
}

Expand Down
14 changes: 7 additions & 7 deletions library/core/src/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ unsafe trait GenericRadix: Sized {
// SAFETY: The only chars in `buf` are created by `Self::digit` which are assumed to be
// valid UTF-8
let buf = unsafe {
str::from_utf8_unchecked(slice::from_raw_parts(buf.as_ptr().into_inner(), buf.len()))
str::from_utf8_unchecked(slice::from_raw_parts(buf.as_ptr().cast::<u8>(), buf.len()))
};
f.pad_integral(is_nonnegative, Self::PREFIX, buf)
}
Expand Down Expand Up @@ -213,7 +213,7 @@ macro_rules! impl_Display {
// 2^128 is about 3*10^38, so 39 gives an extra byte of space
let mut buf = [MaybeUninit::<u8>::uninit(); 39];
let mut curr = buf.len();
let buf_ptr = buf.as_mut_ptr().into_inner();
let buf_ptr = buf.as_mut_ptr().cast::<u8>();
let lut_ptr = DEC_DIGITS_LUT.as_ptr();

// SAFETY: Since `d1` and `d2` are always less than or equal to `198`, we
Expand Down Expand Up @@ -341,7 +341,7 @@ macro_rules! impl_Exp {
// that `curr >= 0`.
let mut buf = [MaybeUninit::<u8>::uninit(); 40];
let mut curr = buf.len(); //index for buf
let buf_ptr = buf.as_mut_ptr().into_inner();
let buf_ptr = buf.as_mut_ptr().cast::<u8>();
let lut_ptr = DEC_DIGITS_LUT.as_ptr();

// decode 2 chars at a time
Expand Down Expand Up @@ -397,11 +397,11 @@ macro_rules! impl_Exp {
} else {
let off = exponent << 1;
// SAFETY: 1 + 2 <= 3
unsafe { ptr::copy_nonoverlapping(lut_ptr.add(off), exp_buf.as_mut_ptr().into_inner().add(1), 2); }
unsafe { ptr::copy_nonoverlapping(lut_ptr.add(off), exp_buf.as_mut_ptr().add(1).cast::<u8>(), 2); }
3
};
// SAFETY: max(2, 3) <= 3
unsafe { slice::from_raw_parts(exp_buf.as_mut_ptr().into_inner(), len) }
unsafe { slice::from_raw_parts(exp_buf.as_mut_ptr().cast::<u8>(), len) }
};

let parts = &[
Expand Down Expand Up @@ -481,7 +481,7 @@ impl_Exp!(i128, u128 as u128 via to_u128 named exp_u128);

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

Expand Down Expand Up @@ -628,7 +628,7 @@ fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::R
// UTF-8 since `DEC_DIGITS_LUT` is
let buf_slice = unsafe {
str::from_utf8_unchecked(slice::from_raw_parts(
buf.as_mut_ptr().add(curr).into_inner(),
buf.as_mut_ptr().add(curr).cast::<u8>(),
buf.len() - curr,
))
};
Expand Down
22 changes: 0 additions & 22 deletions library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,25 +1294,3 @@ impl<T, const N: usize> [MaybeUninit<T>; N] {
unsafe { intrinsics::transmute_unchecked(self) }
}
}

impl<T> *const MaybeUninit<T> {
/// Converts a MaybeUninit pointer to its underlying type.
///
/// See [`MaybeUninit::as_ptr`] for caveats.
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
#[rustc_const_unstable(feature = "maybe_uninit_slice", issue = "63569")]
pub const fn into_inner(self) -> *const T {
self.cast()
}
}

impl<T> *mut MaybeUninit<T> {
/// Converts mutable a MaybeUninit pointer to its underlying type.
///
/// See [`MaybeUninit::as_mut_ptr`] for caveats.
#[unstable(feature = "maybe_uninit_slice", issue = "63569")]
#[rustc_const_unstable(feature = "maybe_uninit_slice", issue = "63569")]
pub const fn into_inner(self) -> *mut T {
self.cast()
}
}
4 changes: 2 additions & 2 deletions library/core/src/slice/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ where

if start_l == end_l {
// Trace `block_l` elements from the left side.
start_l = offsets_l.as_mut_ptr().into_inner();
start_l = offsets_l.as_mut_ptr().cast::<u8>();
end_l = start_l;
let mut elem = l;

Expand All @@ -402,7 +402,7 @@ where

if start_r == end_r {
// Trace `block_r` elements from the right side.
start_r = offsets_r.as_mut_ptr().into_inner();
start_r = offsets_r.as_mut_ptr().cast::<u8>();
end_r = start_r;
let mut elem = r;

Expand Down

0 comments on commit b602bbf

Please sign in to comment.