diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index b2d4b1b5fb9..163f9170b8b 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -17,7 +17,7 @@ #[derive(Debug)] pub struct Excess(pub NonNull, pub usize); -fn size_align() -> (usize, usize) { +const fn size_align() -> (usize, usize) { (mem::size_of::(), mem::align_of::()) } @@ -120,14 +120,14 @@ pub fn align(&self) -> usize { /// Constructs a `Layout` suitable for holding a value of type `T`. #[stable(feature = "alloc_layout", since = "1.28.0")] + #[rustc_const_stable(feature = "alloc_layout_const_new", since = "1.42.0")] #[inline] - pub fn new() -> Self { + pub const fn new() -> Self { let (size, align) = size_align::(); // Note that the align is guaranteed by rustc to be a power of two and // the size+align combo is guaranteed to fit in our address space. As a // result use the unchecked constructor here to avoid inserting code // that panics if it isn't optimized well enough. - debug_assert!(Layout::from_size_align(size, align).is_ok()); unsafe { Layout::from_size_align_unchecked(size, align) } }