From 6bac6939e804c55464eaed7cfa047adb4674c267 Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Sat, 9 Nov 2019 13:43:11 -0500 Subject: [PATCH 1/2] Make Layout::new const --- src/libcore/alloc.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 71517ffb0065e..b049a446cdcd0 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -17,7 +17,7 @@ use crate::num::NonZeroUsize; #[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::()) } @@ -119,13 +119,12 @@ impl Layout { /// Constructs a `Layout` suitable for holding a value of type `T`. #[stable(feature = "alloc_layout", since = "1.28.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) } From 878d3b3171d98f5c68c826d99d30986d60f4f7bb Mon Sep 17 00:00:00 2001 From: CAD97 Date: Tue, 17 Dec 2019 14:55:46 -0500 Subject: [PATCH 2/2] Mark Layout::new as const stable --- src/libcore/alloc.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index b049a446cdcd0..ceb4a73db61cd 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -118,6 +118,7 @@ impl Layout { /// 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 const fn new() -> Self { let (size, align) = size_align::();