Skip to content

Commit b6633ff

Browse files
authored
Rollup merge of rust-lang#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. (rust-lang/rust-clippy#10601)
2 parents b872552 + 1042b5d commit b6633ff

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

library/core/src/mem/maybe_uninit.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -1241,13 +1241,9 @@ impl<T> MaybeUninit<T> {
12411241
/// ```
12421242
#[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")]
12431243
pub fn slice_as_bytes(this: &[MaybeUninit<T>]) -> &[MaybeUninit<u8>] {
1244+
let bytes = mem::size_of_val(this);
12441245
// SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
1245-
unsafe {
1246-
slice::from_raw_parts(
1247-
this.as_ptr() as *const MaybeUninit<u8>,
1248-
this.len() * mem::size_of::<T>(),
1249-
)
1250-
}
1246+
unsafe { slice::from_raw_parts(this.as_ptr() as *const MaybeUninit<u8>, bytes) }
12511247
}
12521248

12531249
/// Returns the contents of this mutable slice of `MaybeUninit` as a mutable slice of
@@ -1274,13 +1270,9 @@ impl<T> MaybeUninit<T> {
12741270
/// ```
12751271
#[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")]
12761272
pub fn slice_as_bytes_mut(this: &mut [MaybeUninit<T>]) -> &mut [MaybeUninit<u8>] {
1273+
let bytes = mem::size_of_val(this);
12771274
// SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
1278-
unsafe {
1279-
slice::from_raw_parts_mut(
1280-
this.as_mut_ptr() as *mut MaybeUninit<u8>,
1281-
this.len() * mem::size_of::<T>(),
1282-
)
1283-
}
1275+
unsafe { slice::from_raw_parts_mut(this.as_mut_ptr() as *mut MaybeUninit<u8>, bytes) }
12841276
}
12851277
}
12861278

0 commit comments

Comments
 (0)