about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <huyuumi.dev@gmail.com>2020-01-10 04:18:29 +0900
committerGitHub <noreply@github.com>2020-01-10 04:18:29 +0900
commit92bd267ba8d8d12cba15490c618628c1f3ea5ac5 (patch)
tree8582ecf268d798cb4219229716a51e417a5e837c
parentc404ce689f7692f2c9b1df51803faf88ba11d2e8 (diff)
parentd860def8e2e2c807c37907c764b937ec35d4f2e6 (diff)
downloadrust-92bd267ba8d8d12cba15490c618628c1f3ea5ac5.tar.gz
rust-92bd267ba8d8d12cba15490c618628c1f3ea5ac5.zip
Rollup merge of #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.
-rw-r--r--src/libcore/alloc.rs6
1 files changed, 3 insertions, 3 deletions
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 @@ use crate::usize;
 #[derive(Debug)]
 pub struct Excess(pub NonNull<u8>, pub usize);
 
-fn size_align<T>() -> (usize, usize) {
+const fn size_align<T>() -> (usize, usize) {
     (mem::size_of::<T>(), mem::align_of::<T>())
 }
 
@@ -120,14 +120,14 @@ 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 fn new<T>() -> Self {
+    pub const fn new<T>() -> Self {
         let (size, align) = size_align::<T>();
         // 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) }
     }