Skip to content

Commit

Permalink
Rollup merge of rust-lang#123168 - joshtriplett:size-of-prelude, r=Am…
Browse files Browse the repository at this point in the history
…anieu

Add `size_of` and `size_of_val` and `align_of` and `align_of_val` to the prelude

(Note: need to update the PR to add `align_of` and `align_of_val`, and remove the second commit with the myriad changes to appease the lint.)

Many, many projects use `size_of` to get the size of a type. However,
it's also often equally easy to hardcode a size (e.g. `8` instead of
`size_of::<u64>()`). Minimizing friction in the use of `size_of` helps
ensure that people use it and make code more self-documenting.

The name `size_of` is unambiguous: the name alone, without any prefix or
path, is self-explanatory and unmistakeable for any other functionality.
Adding it to the prelude cannot produce any name conflicts, as any local
definition will silently shadow the one from the prelude. Thus, we don't
need to wait for a new edition prelude to add it.
  • Loading branch information
workingjubilee authored Jun 5, 2024
2 parents 435f706 + 3927f4a commit 4da8687
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/core_simd/src/simd/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ where
fn cast<U>(self) -> Self::CastPtr<U> {
// SimdElement currently requires zero-sized metadata, so this should never fail.
// If this ever changes, `simd_cast_ptr` should produce a post-mono error.
use core::{mem::size_of, ptr::Pointee};
use core::ptr::Pointee;
assert_eq!(size_of::<<T as Pointee>::Metadata>(), 0);
assert_eq!(size_of::<<U as Pointee>::Metadata>(), 0);

Expand Down
2 changes: 1 addition & 1 deletion crates/core_simd/src/simd/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ where
fn cast<U>(self) -> Self::CastPtr<U> {
// SimdElement currently requires zero-sized metadata, so this should never fail.
// If this ever changes, `simd_cast_ptr` should produce a post-mono error.
use core::{mem::size_of, ptr::Pointee};
use core::ptr::Pointee;
assert_eq!(size_of::<<T as Pointee>::Metadata>(), 0);
assert_eq!(size_of::<<U as Pointee>::Metadata>(), 0);

Expand Down

0 comments on commit 4da8687

Please sign in to comment.