Skip to content

Commit 032b547

Browse files
Auto merge of #149109 - scottmcm:align-always-alignment, r=<try>
See if this is the time we can remove `layout::size_align`
2 parents 07bdbae + 2bf4c01 commit 032b547

File tree

2 files changed

+7
-16
lines changed

2 files changed

+7
-16
lines changed

library/core/src/alloc/layout.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ use crate::mem::SizedTypeProperties;
1010
use crate::ptr::{Alignment, NonNull};
1111
use crate::{assert_unsafe_precondition, fmt, mem};
1212

13-
// While this function is used in one place and its implementation
14-
// could be inlined, the previous attempts to do so made rustc
15-
// slower:
16-
//
17-
// * https://github.com/rust-lang/rust/pull/72189
18-
// * https://github.com/rust-lang/rust/pull/79827
19-
const fn size_align<T>() -> (usize, usize) {
20-
(size_of::<T>(), align_of::<T>())
21-
}
22-
2313
/// Layout of a block of memory.
2414
///
2515
/// An instance of `Layout` describes a particular layout of memory.
@@ -168,11 +158,7 @@ impl Layout {
168158
#[must_use]
169159
#[inline]
170160
pub const fn new<T>() -> Self {
171-
let (size, align) = size_align::<T>();
172-
// SAFETY: if the type is instantiated, rustc already ensures that its
173-
// layout is valid. Use the unchecked constructor to avoid inserting a
174-
// panicking codepath that needs to be optimized out.
175-
unsafe { Layout::from_size_align_unchecked(size, align) }
161+
<T as SizedTypeProperties>::LAYOUT
176162
}
177163

178164
/// Produces layout describing a record that could be used to

library/core/src/mem/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,12 @@ pub trait SizedTypeProperties: Sized {
12831283

12841284
#[doc(hidden)]
12851285
#[unstable(feature = "sized_type_properties", issue = "none")]
1286-
const LAYOUT: Layout = Layout::new::<Self>();
1286+
const LAYOUT: Layout = {
1287+
// SAFETY: if the type is instantiated, rustc already ensures that its
1288+
// layout is valid. Use the unchecked constructor to avoid inserting a
1289+
// panicking codepath that needs to be optimized out.
1290+
unsafe { Layout::from_size_align_unchecked(Self::SIZE, Self::ALIGN) }
1291+
};
12871292

12881293
/// The largest safe length for a `[Self]`.
12891294
///

0 commit comments

Comments
 (0)