about summary refs log tree commit diff
path: root/src/libcore/alloc.rs
diff options
context:
space:
mode:
authorTim Diekmann <tim.diekmann@3dvision.de>2020-03-07 02:40:54 +0100
committerTim Diekmann <tim.diekmann@3dvision.de>2020-03-07 02:40:54 +0100
commit545ef9d83ab383bdc99376a2fe4164e46f4a3f6e (patch)
treeb6ac4c86c0c531b21778c34469dfc0cad9eb6539 /src/libcore/alloc.rs
parent2890b37b861247de3b8c6ba2ecbcd00048c728a1 (diff)
downloadrust-545ef9d83ab383bdc99376a2fe4164e46f4a3f6e.tar.gz
rust-545ef9d83ab383bdc99376a2fe4164e46f4a3f6e.zip
Add `Layout::dangling()` to return a well-aligned `NonNull<u8>`
Diffstat (limited to 'src/libcore/alloc.rs')
-rw-r--r--src/libcore/alloc.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs
index f3a2b73f2b8..4f401c75104 100644
--- a/src/libcore/alloc.rs
+++ b/src/libcore/alloc.rs
@@ -140,6 +140,18 @@ impl Layout {
         unsafe { Layout::from_size_align_unchecked(size, align) }
     }
 
+    /// Creates a `NonNull` that is dangling, but well-aligned for this Layout.
+    ///
+    /// Note that the pointer value may potentially represent a valid pointer to
+    /// a `T`, which means this must not be used as a "not yet initialized"
+    /// sentinel value. Types that lazily allocate must track initialization by
+    /// some other means.
+    #[unstable(feature = "alloc_layout_extra", issue = "55724")]
+    pub const fn dangling(&self) -> NonNull<u8> {
+        // align is non-zero and a power of two
+        unsafe { NonNull::new_unchecked(self.align() as *mut u8) }
+    }
+
     /// Creates a layout describing the record that can hold a value
     /// of the same layout as `self`, but that also is aligned to
     /// alignment `align` (measured in bytes).