mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
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:
commit
b6633ffaf7
@ -1241,13 +1241,9 @@ impl<T> MaybeUninit<T> {
|
||||
/// ```
|
||||
#[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")]
|
||||
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
|
||||
unsafe {
|
||||
slice::from_raw_parts(
|
||||
this.as_ptr() as *const MaybeUninit<u8>,
|
||||
this.len() * mem::size_of::<T>(),
|
||||
)
|
||||
}
|
||||
unsafe { slice::from_raw_parts(this.as_ptr() as *const MaybeUninit<u8>, bytes) }
|
||||
}
|
||||
|
||||
/// 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")]
|
||||
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
|
||||
unsafe {
|
||||
slice::from_raw_parts_mut(
|
||||
this.as_mut_ptr() as *mut MaybeUninit<u8>,
|
||||
this.len() * mem::size_of::<T>(),
|
||||
)
|
||||
}
|
||||
unsafe { slice::from_raw_parts_mut(this.as_mut_ptr() as *mut MaybeUninit<u8>, bytes) }
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user