Rollup merge of #110044 - scottmcm:more-size-of-val, r=ChrisDenton

Avoid some manual slice length calculation

No need for us to write the multiplication when `size_of_val` does exactly what we need.

(https://github.com/rust-lang/rust-clippy/pull/10601)
This commit is contained in:
Dylan DPC 2023-04-10 14:13:14 +05:30 committed by GitHub
commit b6633ffaf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1241,13 +1241,9 @@ impl<T> MaybeUninit<T> {
/// ``` /// ```
#[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")] #[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")]
pub fn slice_as_bytes(this: &[MaybeUninit<T>]) -> &[MaybeUninit<u8>] { pub fn slice_as_bytes(this: &[MaybeUninit<T>]) -> &[MaybeUninit<u8>] {
let bytes = mem::size_of_val(this);
// SAFETY: MaybeUninit<u8> is always valid, even for padding bytes // SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
unsafe { unsafe { slice::from_raw_parts(this.as_ptr() as *const MaybeUninit<u8>, bytes) }
slice::from_raw_parts(
this.as_ptr() as *const MaybeUninit<u8>,
this.len() * mem::size_of::<T>(),
)
}
} }
/// Returns the contents of this mutable slice of `MaybeUninit` as a mutable slice of /// Returns the contents of this mutable slice of `MaybeUninit` as a mutable slice of
@ -1274,13 +1270,9 @@ impl<T> MaybeUninit<T> {
/// ``` /// ```
#[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")] #[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")]
pub fn slice_as_bytes_mut(this: &mut [MaybeUninit<T>]) -> &mut [MaybeUninit<u8>] { pub fn slice_as_bytes_mut(this: &mut [MaybeUninit<T>]) -> &mut [MaybeUninit<u8>] {
let bytes = mem::size_of_val(this);
// SAFETY: MaybeUninit<u8> is always valid, even for padding bytes // SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
unsafe { unsafe { slice::from_raw_parts_mut(this.as_mut_ptr() as *mut MaybeUninit<u8>, bytes) }
slice::from_raw_parts_mut(
this.as_mut_ptr() as *mut MaybeUninit<u8>,
this.len() * mem::size_of::<T>(),
)
}
} }
} }