diff options
| author | bors <bors@rust-lang.org> | 2017-07-19 16:58:02 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-07-19 16:58:02 +0000 |
| commit | 9bbbd29e82fb7fc9f79736653d42100979899832 (patch) | |
| tree | 8f0ddaea23d5ce961f8107ee2f256f0117ac27ab /src/libcore | |
| parent | 344f01cf1322d61bad109b503d0e6a1b55d6ed31 (diff) | |
| parent | 148718b4f32b46a96e339be92102697ad58b170b (diff) | |
| download | rust-9bbbd29e82fb7fc9f79736653d42100979899832.tar.gz rust-9bbbd29e82fb7fc9f79736653d42100979899832.zip | |
Auto merge of #42859 - eddyb:const-size-and-align-of, r=nikomatsakis
Implement const fn {size,align}_of.
Fixes #34078.
r? @nikomatsakis
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/mem.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 6c1e8e8960f..86e5afa4c33 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -188,10 +188,30 @@ pub fn forget<T>(t: T) { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] +#[cfg(stage0)] pub fn size_of<T>() -> usize { unsafe { intrinsics::size_of::<T>() } } +/// Returns the size of a type in bytes. +/// +/// More specifically, this is the offset in bytes between successive +/// items of the same type, including alignment padding. +/// +/// # Examples +/// +/// ``` +/// use std::mem; +/// +/// assert_eq!(4, mem::size_of::<i32>()); +/// ``` +#[inline] +#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(not(stage0))] +pub const fn size_of<T>() -> usize { + unsafe { intrinsics::size_of::<T>() } +} + /// Returns the size of the pointed-to value in bytes. /// /// This is usually the same as `size_of::<T>()`. However, when `T` *has* no @@ -279,10 +299,33 @@ pub fn min_align_of_val<T: ?Sized>(val: &T) -> usize { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] +#[cfg(stage0)] pub fn align_of<T>() -> usize { unsafe { intrinsics::min_align_of::<T>() } } +/// Returns the [ABI]-required minimum alignment of a type. +/// +/// Every reference to a value of the type `T` must be a multiple of this number. +/// +/// This is the alignment used for struct fields. It may be smaller than the preferred alignment. +/// +/// [ABI]: https://en.wikipedia.org/wiki/Application_binary_interface +/// +/// # Examples +/// +/// ``` +/// use std::mem; +/// +/// assert_eq!(4, mem::align_of::<i32>()); +/// ``` +#[inline] +#[stable(feature = "rust1", since = "1.0.0")] +#[cfg(not(stage0))] +pub const fn align_of<T>() -> usize { + unsafe { intrinsics::min_align_of::<T>() } +} + /// Returns the [ABI]-required minimum alignment of the type of the value that `val` points to. /// /// Every reference to a value of the type `T` must be a multiple of this number. |
