diff options
| author | Scott McMurray <scottmcm@users.noreply.github.com> | 2023-04-07 01:32:12 -0700 |
|---|---|---|
| committer | Scott McMurray <scottmcm@users.noreply.github.com> | 2023-04-07 01:32:12 -0700 |
| commit | 1042b5df9bbfa29e87c09edb54aaeb667336d1b4 (patch) | |
| tree | f23f8e923fdd098bac0163a295cce9d7e641dd9f | |
| parent | 7f6edd3f15f75f0df70027edee2a520820d14217 (diff) | |
| download | rust-1042b5df9bbfa29e87c09edb54aaeb667336d1b4.tar.gz rust-1042b5df9bbfa29e87c09edb54aaeb667336d1b4.zip | |
Avoid some manual slice length calculation
No need for us to write the multiplication when `size_of_val` does exactly what we need.
| -rw-r--r-- | library/core/src/mem/maybe_uninit.rs | 16 |
1 files changed, 4 insertions, 12 deletions
diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index 3f491836551..9c6d48675a6 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -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) } } } |
