diff options
| author | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2017-06-23 17:48:29 +0300 |
|---|---|---|
| committer | Eduard-Mihai Burtescu <edy.burt@gmail.com> | 2017-07-19 14:46:54 +0300 |
| commit | 148718b4f32b46a96e339be92102697ad58b170b (patch) | |
| tree | 721d2a8ecf52cf7ac77f4b1084d4c47fedfa23e6 /src/libcore | |
| parent | 2e6334062e2be142125e99d63867711da505cc9e (diff) | |
Implement const fn {size,align}_of.
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. |
