Skip to content

Commit 88338ff

Browse files
committed
Add size_of, size_of_val, align_of, and align_of_val to the prelude
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. Add `size_of_val`, `align_of`, and `align_of_val` as well, with similar justification: widely useful, self-explanatory, unmistakeable for anything else, won't produce conflicts.
1 parent 09d9a3b commit 88338ff

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
@@ -238,7 +238,7 @@
238238
use crate::cmp::Ordering;
239239
use crate::fmt::{self, Debug, Display};
240240
use crate::marker::{PhantomData, Unsize};
241-
use crate::mem::{self, size_of};
241+
use crate::mem;
242242
use crate::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn};
243243
use crate::ptr::{self, NonNull};
244244

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
@@ -392,7 +392,7 @@ use crate::intrinsics;
392392
use crate::marker::FnPtr;
393393
use crate::ub_checks;
394394

395-
use crate::mem::{self, align_of, size_of, MaybeUninit};
395+
use crate::mem::{self, MaybeUninit};
396396

397397
mod alignment;
398398
#[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)