Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 80cee25

Browse files
authoredJun 5, 2024
Rollup merge of rust-lang#123168 - joshtriplett:size-of-prelude, r=Amanieu
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.
2 parents f8ee355 + 88338ff commit 80cee25

File tree

15 files changed

+15
-16
lines changed

15 files changed

+15
-16
lines changed
 

‎alloc/src/rc.rs

-2
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,6 @@ use core::intrinsics::abort;
257257
#[cfg(not(no_global_oom_handling))]
258258
use core::iter;
259259
use core::marker::{PhantomData, Unsize};
260-
#[cfg(not(no_global_oom_handling))]
261-
use core::mem::size_of_val;
262260
use core::mem::{self, align_of_val_raw, forget, ManuallyDrop};
263261
use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver};
264262
use core::panic::{RefUnwindSafe, UnwindSafe};

‎alloc/src/sync.rs

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ use core::intrinsics::abort;
1818
#[cfg(not(no_global_oom_handling))]
1919
use core::iter;
2020
use core::marker::{PhantomData, Unsize};
21-
#[cfg(not(no_global_oom_handling))]
22-
use core::mem::size_of_val;
2321
use core::mem::{self, align_of_val_raw};
2422
use core::ops::{CoerceUnsized, Deref, DerefPure, DispatchFromDyn, Receiver};
2523
use core::panic::{RefUnwindSafe, UnwindSafe};

‎core/src/cell.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@
253253
use crate::cmp::Ordering;
254254
use crate::fmt::{self, Debug, Display};
255255
use crate::marker::{PhantomData, Unsize};
256-
use crate::mem::{self, size_of};
256+
use crate::mem;
257257
use crate::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn};
258258
use crate::ptr::{self, NonNull};
259259

‎core/src/intrinsics.rs

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565

6666
use crate::marker::DiscriminantKind;
6767
use crate::marker::Tuple;
68-
use crate::mem::align_of;
6968
use crate::ptr;
7069
use crate::ub_checks;
7170

‎core/src/prelude/common.rs

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ pub use crate::ops::{Drop, Fn, FnMut, FnOnce};
1414
#[stable(feature = "core_prelude", since = "1.4.0")]
1515
#[doc(no_inline)]
1616
pub use crate::mem::drop;
17+
#[stable(feature = "size_of_prelude", since = "CURRENT_RUSTC_VERSION")]
18+
#[doc(no_inline)]
19+
pub use crate::mem::{align_of, align_of_val, size_of, size_of_val};
1720

1821
// Re-exported types and traits
1922
#[stable(feature = "core_prelude", since = "1.4.0")]

‎core/src/ptr/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ use crate::intrinsics;
415415
use crate::marker::FnPtr;
416416
use crate::ub_checks;
417417

418-
use crate::mem::{self, align_of, size_of, MaybeUninit};
418+
use crate::mem::{self, MaybeUninit};
419419

420420
mod alignment;
421421
#[unstable(feature = "ptr_alignment_type", issue = "102070")]

‎core/src/slice/raw.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Free functions to create `&[T]` and `&mut [T]`.
22
33
use crate::array;
4-
use crate::mem::{align_of, size_of};
54
use crate::ops::Range;
65
use crate::ptr;
76
use crate::ub_checks;

‎core/src/sync/atomic.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,6 @@ impl<T> AtomicPtr<T> {
12961296
#[cfg(target_has_atomic_equal_alignment = "ptr")]
12971297
#[unstable(feature = "atomic_from_mut", issue = "76314")]
12981298
pub fn from_mut(v: &mut *mut T) -> &mut Self {
1299-
use crate::mem::align_of;
13001299
let [] = [(); align_of::<AtomicPtr<()>>() - align_of::<*mut ()>()];
13011300
// SAFETY:
13021301
// - the mutable reference guarantees unique ownership.
@@ -2286,7 +2285,6 @@ macro_rules! atomic_int {
22862285
#[$cfg_align]
22872286
#[unstable(feature = "atomic_from_mut", issue = "76314")]
22882287
pub fn from_mut(v: &mut $int_type) -> &mut Self {
2289-
use crate::mem::align_of;
22902288
let [] = [(); align_of::<Self>() - align_of::<$int_type>()];
22912289
// SAFETY:
22922290
// - the mutable reference guarantees unique ownership.
@@ -2354,7 +2352,6 @@ macro_rules! atomic_int {
23542352
#[$cfg_align]
23552353
#[unstable(feature = "atomic_from_mut", issue = "76314")]
23562354
pub fn from_mut_slice(v: &mut [$int_type]) -> &mut [Self] {
2357-
use crate::mem::align_of;
23582355
let [] = [(); align_of::<Self>() - align_of::<$int_type>()];
23592356
// SAFETY:
23602357
// - the mutable reference guarantees unique ownership.

‎portable-simd/crates/core_simd/src/simd/ptr/const_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ where
9696
fn cast<U>(self) -> Self::CastPtr<U> {
9797
// SimdElement currently requires zero-sized metadata, so this should never fail.
9898
// If this ever changes, `simd_cast_ptr` should produce a post-mono error.
99-
use core::{mem::size_of, ptr::Pointee};
99+
use core::ptr::Pointee;
100100
assert_eq!(size_of::<<T as Pointee>::Metadata>(), 0);
101101
assert_eq!(size_of::<<U as Pointee>::Metadata>(), 0);
102102

‎portable-simd/crates/core_simd/src/simd/ptr/mut_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ where
9393
fn cast<U>(self) -> Self::CastPtr<U> {
9494
// SimdElement currently requires zero-sized metadata, so this should never fail.
9595
// If this ever changes, `simd_cast_ptr` should produce a post-mono error.
96-
use core::{mem::size_of, ptr::Pointee};
96+
use core::ptr::Pointee;
9797
assert_eq!(size_of::<<T as Pointee>::Metadata>(), 0);
9898
assert_eq!(size_of::<<U as Pointee>::Metadata>(), 0);
9999

‎proc_macro/src/bridge/fxhash.rs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use std::collections::HashMap;
88
use std::hash::BuildHasherDefault;
99
use std::hash::Hasher;
10-
use std::mem::size_of;
1110
use std::ops::BitXor;
1211

1312
/// Type alias for a hashmap using the `fx` hash algorithm.

‎std/src/io/error/repr_bitpacked.rs

-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
105105
use super::{Custom, ErrorData, ErrorKind, RawOsError, SimpleMessage};
106106
use core::marker::PhantomData;
107-
use core::mem::{align_of, size_of};
108107
use core::ptr::{self, NonNull};
109108

110109
// The 2 least-significant bits are used as tag.

‎std/src/os/unix/net/ancillary.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use super::{sockaddr_un, SocketAddr};
44
use crate::io::{self, IoSlice, IoSliceMut};
55
use crate::marker::PhantomData;
6-
use crate::mem::{size_of, zeroed};
6+
use crate::mem::zeroed;
77
use crate::os::unix::io::RawFd;
88
use crate::path::Path;
99
use crate::ptr::{eq, read_unaligned};

‎std/src/prelude/common.rs

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ pub use crate::ops::{Drop, Fn, FnMut, FnOnce};
1414
#[stable(feature = "rust1", since = "1.0.0")]
1515
#[doc(no_inline)]
1616
pub use crate::mem::drop;
17+
#[stable(feature = "size_of_prelude", since = "CURRENT_RUSTC_VERSION")]
18+
#[doc(no_inline)]
19+
pub use crate::mem::{align_of, align_of_val, size_of, size_of_val};
1720

1821
// Re-exported types and traits
1922
#[stable(feature = "rust1", since = "1.0.0")]

‎std/src/prelude/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
//! operations for both destructors and overloading `()`.
3737
//! * <code>[std::mem]::[drop]</code>, a convenience function for explicitly
3838
//! dropping a value.
39+
//! * <code>[std::mem]::{[size_of], [size_of_val]}</code>, to get the size of
40+
//! a type or value.
41+
//! * <code>[std::mem]::{[align_of], [align_of_val]}</code>, to get the
42+
//! alignment of a type or value.
3943
//! * <code>[std::boxed]::[Box]</code>, a way to allocate values on the heap.
4044
//! * <code>[std::borrow]::[ToOwned]</code>, the conversion trait that defines
4145
//! [`to_owned`], the generic method for creating an owned type from a

0 commit comments

Comments
 (0)
Failed to load comments.