From 1042b5df9bbfa29e87c09edb54aaeb667336d1b4 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Fri, 7 Apr 2023 01:32:12 -0700 Subject: [PATCH] Avoid some manual slice length calculation No need for us to write the multiplication when `size_of_val` does exactly what we need. --- library/core/src/mem/maybe_uninit.rs | 16 ++++------------ 1 file 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 @@ pub fn as_bytes_mut(&mut self) -> &mut [MaybeUninit] { /// ``` #[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")] pub fn slice_as_bytes(this: &[MaybeUninit]) -> &[MaybeUninit] { + let bytes = mem::size_of_val(this); // SAFETY: MaybeUninit is always valid, even for padding bytes - unsafe { - slice::from_raw_parts( - this.as_ptr() as *const MaybeUninit, - this.len() * mem::size_of::(), - ) - } + unsafe { slice::from_raw_parts(this.as_ptr() as *const MaybeUninit, bytes) } } /// Returns the contents of this mutable slice of `MaybeUninit` as a mutable slice of @@ -1274,13 +1270,9 @@ pub fn slice_as_bytes(this: &[MaybeUninit]) -> &[MaybeUninit] { /// ``` #[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")] pub fn slice_as_bytes_mut(this: &mut [MaybeUninit]) -> &mut [MaybeUninit] { + let bytes = mem::size_of_val(this); // SAFETY: MaybeUninit is always valid, even for padding bytes - unsafe { - slice::from_raw_parts_mut( - this.as_mut_ptr() as *mut MaybeUninit, - this.len() * mem::size_of::(), - ) - } + unsafe { slice::from_raw_parts_mut(this.as_mut_ptr() as *mut MaybeUninit, bytes) } } }