Skip to content

Commit 94cb34e

Browse files
authored
Rollup merge of rust-lang#66254 - CAD97:patch-1, r=KodrAus
Make Layout::new const This seems like a reasonable change to make. If we don't provide `Layout::new::<T>` as `const`, then users can just instead do the more error prone `Layout::from_size_align_unchecked(mem::size_of::<T>(), mem::align_of::<T>())` for the same effect and an extra `unsafe { }` incantation.
2 parents f0d4b57 + e8c53a6 commit 94cb34e

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

src/libcore/alloc.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::num::NonZeroUsize;
1717
#[derive(Debug)]
1818
pub struct Excess(pub NonNull<u8>, pub usize);
1919

20-
fn size_align<T>() -> (usize, usize) {
20+
const fn size_align<T>() -> (usize, usize) {
2121
(mem::size_of::<T>(), mem::align_of::<T>())
2222
}
2323

@@ -119,13 +119,12 @@ impl Layout {
119119
/// Constructs a `Layout` suitable for holding a value of type `T`.
120120
#[stable(feature = "alloc_layout", since = "1.28.0")]
121121
#[inline]
122-
pub fn new<T>() -> Self {
122+
pub const fn new<T>() -> Self {
123123
let (size, align) = size_align::<T>();
124124
// Note that the align is guaranteed by rustc to be a power of two and
125125
// the size+align combo is guaranteed to fit in our address space. As a
126126
// result use the unchecked constructor here to avoid inserting code
127127
// that panics if it isn't optimized well enough.
128-
debug_assert!(Layout::from_size_align(size, align).is_ok());
129128
unsafe {
130129
Layout::from_size_align_unchecked(size, align)
131130
}

0 commit comments

Comments
 (0)