mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
Rollup merge of #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.
This commit is contained in:
commit
9ccc7b78ec
@ -4,7 +4,7 @@ use crate::traits;
|
||||
use crate::ty::adjustment::CoerceUnsizedInfo;
|
||||
use crate::ty::{self, Ty};
|
||||
use std::intrinsics::transmute_unchecked;
|
||||
use std::mem::{size_of, MaybeUninit};
|
||||
use std::mem::MaybeUninit;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Erased<T: Copy> {
|
||||
|
@ -257,8 +257,6 @@ use core::intrinsics::abort;
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
use core::iter;
|
||||
use core::marker::{PhantomData, Unsize};
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
use core::mem::size_of_val;
|
||||
use core::mem::{self, align_of_val_raw, forget, ManuallyDrop};
|
||||
use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, Receiver};
|
||||
use core::panic::{RefUnwindSafe, UnwindSafe};
|
||||
|
@ -18,8 +18,6 @@ use core::intrinsics::abort;
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
use core::iter;
|
||||
use core::marker::{PhantomData, Unsize};
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
use core::mem::size_of_val;
|
||||
use core::mem::{self, align_of_val_raw};
|
||||
use core::ops::{CoerceUnsized, Deref, DerefPure, DispatchFromDyn, Receiver};
|
||||
use core::panic::{RefUnwindSafe, UnwindSafe};
|
||||
|
@ -253,7 +253,7 @@
|
||||
use crate::cmp::Ordering;
|
||||
use crate::fmt::{self, Debug, Display};
|
||||
use crate::marker::{PhantomData, Unsize};
|
||||
use crate::mem::{self, size_of};
|
||||
use crate::mem;
|
||||
use crate::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn};
|
||||
use crate::ptr::{self, NonNull};
|
||||
|
||||
|
@ -65,7 +65,6 @@
|
||||
|
||||
use crate::marker::DiscriminantKind;
|
||||
use crate::marker::Tuple;
|
||||
use crate::mem::align_of;
|
||||
use crate::ptr;
|
||||
use crate::ub_checks;
|
||||
|
||||
|
@ -14,6 +14,9 @@ pub use crate::ops::{Drop, Fn, FnMut, FnOnce};
|
||||
#[stable(feature = "core_prelude", since = "1.4.0")]
|
||||
#[doc(no_inline)]
|
||||
pub use crate::mem::drop;
|
||||
#[stable(feature = "size_of_prelude", since = "CURRENT_RUSTC_VERSION")]
|
||||
#[doc(no_inline)]
|
||||
pub use crate::mem::{align_of, align_of_val, size_of, size_of_val};
|
||||
|
||||
// Re-exported types and traits
|
||||
#[stable(feature = "core_prelude", since = "1.4.0")]
|
||||
|
@ -415,7 +415,7 @@ use crate::intrinsics;
|
||||
use crate::marker::FnPtr;
|
||||
use crate::ub_checks;
|
||||
|
||||
use crate::mem::{self, align_of, size_of, MaybeUninit};
|
||||
use crate::mem::{self, MaybeUninit};
|
||||
|
||||
mod alignment;
|
||||
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
|
||||
|
@ -1,7 +1,6 @@
|
||||
//! Free functions to create `&[T]` and `&mut [T]`.
|
||||
|
||||
use crate::array;
|
||||
use crate::mem::{align_of, size_of};
|
||||
use crate::ops::Range;
|
||||
use crate::ptr;
|
||||
use crate::ub_checks;
|
||||
|
@ -1296,7 +1296,6 @@ impl<T> AtomicPtr<T> {
|
||||
#[cfg(target_has_atomic_equal_alignment = "ptr")]
|
||||
#[unstable(feature = "atomic_from_mut", issue = "76314")]
|
||||
pub fn from_mut(v: &mut *mut T) -> &mut Self {
|
||||
use crate::mem::align_of;
|
||||
let [] = [(); align_of::<AtomicPtr<()>>() - align_of::<*mut ()>()];
|
||||
// SAFETY:
|
||||
// - the mutable reference guarantees unique ownership.
|
||||
@ -2286,7 +2285,6 @@ macro_rules! atomic_int {
|
||||
#[$cfg_align]
|
||||
#[unstable(feature = "atomic_from_mut", issue = "76314")]
|
||||
pub fn from_mut(v: &mut $int_type) -> &mut Self {
|
||||
use crate::mem::align_of;
|
||||
let [] = [(); align_of::<Self>() - align_of::<$int_type>()];
|
||||
// SAFETY:
|
||||
// - the mutable reference guarantees unique ownership.
|
||||
@ -2354,7 +2352,6 @@ macro_rules! atomic_int {
|
||||
#[$cfg_align]
|
||||
#[unstable(feature = "atomic_from_mut", issue = "76314")]
|
||||
pub fn from_mut_slice(v: &mut [$int_type]) -> &mut [Self] {
|
||||
use crate::mem::align_of;
|
||||
let [] = [(); align_of::<Self>() - align_of::<$int_type>()];
|
||||
// SAFETY:
|
||||
// - the mutable reference guarantees unique ownership.
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -7,7 +7,6 @@
|
||||
use std::collections::HashMap;
|
||||
use std::hash::BuildHasherDefault;
|
||||
use std::hash::Hasher;
|
||||
use std::mem::size_of;
|
||||
use std::ops::BitXor;
|
||||
|
||||
/// Type alias for a hashmap using the `fx` hash algorithm.
|
||||
|
@ -104,7 +104,6 @@
|
||||
|
||||
use super::{Custom, ErrorData, ErrorKind, RawOsError, SimpleMessage};
|
||||
use core::marker::PhantomData;
|
||||
use core::mem::{align_of, size_of};
|
||||
use core::ptr::{self, NonNull};
|
||||
|
||||
// The 2 least-significant bits are used as tag.
|
||||
|
@ -3,7 +3,7 @@
|
||||
use super::{sockaddr_un, SocketAddr};
|
||||
use crate::io::{self, IoSlice, IoSliceMut};
|
||||
use crate::marker::PhantomData;
|
||||
use crate::mem::{size_of, zeroed};
|
||||
use crate::mem::zeroed;
|
||||
use crate::os::unix::io::RawFd;
|
||||
use crate::path::Path;
|
||||
use crate::ptr::{eq, read_unaligned};
|
||||
|
@ -14,6 +14,9 @@ pub use crate::ops::{Drop, Fn, FnMut, FnOnce};
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[doc(no_inline)]
|
||||
pub use crate::mem::drop;
|
||||
#[stable(feature = "size_of_prelude", since = "CURRENT_RUSTC_VERSION")]
|
||||
#[doc(no_inline)]
|
||||
pub use crate::mem::{align_of, align_of_val, size_of, size_of_val};
|
||||
|
||||
// Re-exported types and traits
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
|
@ -36,6 +36,10 @@
|
||||
//! operations for both destructors and overloading `()`.
|
||||
//! * <code>[std::mem]::[drop]</code>, a convenience function for explicitly
|
||||
//! dropping a value.
|
||||
//! * <code>[std::mem]::{[size_of], [size_of_val]}</code>, to get the size of
|
||||
//! a type or value.
|
||||
//! * <code>[std::mem]::{[align_of], [align_of_val]}</code>, to get the
|
||||
//! alignment of a type or value.
|
||||
//! * <code>[std::boxed]::[Box]</code>, a way to allocate values on the heap.
|
||||
//! * <code>[std::borrow]::[ToOwned]</code>, the conversion trait that defines
|
||||
//! [`to_owned`], the generic method for creating an owned type from a
|
||||
|
@ -1,6 +1,6 @@
|
||||
fn main() {
|
||||
// Should suggest only `std::mem::size_of`
|
||||
let _ = size_of::<usize>();
|
||||
// Should suggest only `std::mem::transmute`
|
||||
let _ = transmute::<usize>();
|
||||
//~^ ERROR cannot find
|
||||
|
||||
// Should suggest `std::intrinsics::fabsf64`,
|
||||
|
@ -1,12 +1,12 @@
|
||||
error[E0425]: cannot find function `size_of` in this scope
|
||||
error[E0425]: cannot find function `transmute` in this scope
|
||||
--> $DIR/filter-intrinsics.rs:3:13
|
||||
|
|
||||
LL | let _ = size_of::<usize>();
|
||||
| ^^^^^^^ not found in this scope
|
||||
LL | let _ = transmute::<usize>();
|
||||
| ^^^^^^^^^ not found in this scope
|
||||
|
|
||||
help: consider importing this function
|
||||
|
|
||||
LL + use std::mem::size_of;
|
||||
LL + use std::mem::transmute;
|
||||
|
|
||||
|
||||
error[E0425]: cannot find function `fabsf64` in this scope
|
||||
|
Loading…
Reference in New Issue
Block a user