Skip to content

Commit

Permalink
Rollup merge of #110044 - scottmcm:more-size-of-val, r=ChrisDenton
Browse files Browse the repository at this point in the history
Avoid some manual slice length calculation

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

(rust-lang/rust-clippy#10601)
  • Loading branch information
Dylan-DPC authored Apr 10, 2023
2 parents 5df3883 + 37423d6 commit da45a8b
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) }
}
}

Expand Down

0 comments on commit da45a8b

Please sign in to comment.