about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJames Wainwright <james.wainwright@lowrisc.org>2025-03-25 21:43:19 +0000
committerJames Wainwright <james.wainwright@lowrisc.org>2025-03-26 21:41:11 +0000
commit78e962139020df03174f03da6af91acc5cbb37e3 (patch)
tree40ebedad64554c4440f523b6203965d452531046
parentd872845eae7f9988995fb8ca35f86d08de999409 (diff)
downloadrust-78e962139020df03174f03da6af91acc5cbb37e3.tar.gz
rust-78e962139020df03174f03da6af91acc5cbb37e3.zip
Pass `Alignment` for `RawVecInner::new_in`
Encodes the safety constraint that `Unique`'s pointer must be non-zero
into the API.
-rw-r--r--library/alloc/src/lib.rs1
-rw-r--r--library/alloc/src/raw_vec/mod.rs9
-rw-r--r--library/alloctests/lib.rs1
-rw-r--r--library/core/src/alloc/layout.rs8
4 files changed, 15 insertions, 4 deletions
diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs
index f0cdb1e4e0f..04858667230 100644
--- a/library/alloc/src/lib.rs
+++ b/library/alloc/src/lib.rs
@@ -135,6 +135,7 @@
 #![feature(pattern)]
 #![feature(pin_coerce_unsized_trait)]
 #![feature(pointer_like_trait)]
+#![feature(ptr_alignment_type)]
 #![feature(ptr_internals)]
 #![feature(ptr_metadata)]
 #![feature(set_ptr_value)]
diff --git a/library/alloc/src/raw_vec/mod.rs b/library/alloc/src/raw_vec/mod.rs
index 99ebc5c4bfc..83facd9d932 100644
--- a/library/alloc/src/raw_vec/mod.rs
+++ b/library/alloc/src/raw_vec/mod.rs
@@ -6,7 +6,7 @@
 
 use core::marker::PhantomData;
 use core::mem::{ManuallyDrop, MaybeUninit, SizedTypeProperties};
-use core::ptr::{self, NonNull, Unique};
+use core::ptr::{self, Alignment, NonNull, Unique};
 use core::{cmp, hint};
 
 #[cfg(not(no_global_oom_handling))]
@@ -177,7 +177,7 @@ impl<T, A: Allocator> RawVec<T, A> {
     /// the returned `RawVec`.
     #[inline]
     pub(crate) const fn new_in(alloc: A) -> Self {
-        Self { inner: RawVecInner::new_in(alloc, align_of::<T>()), _marker: PhantomData }
+        Self { inner: RawVecInner::new_in(alloc, Alignment::of::<T>()), _marker: PhantomData }
     }
 
     /// Like `with_capacity`, but parameterized over the choice of
@@ -409,7 +409,8 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> {
 
 impl<A: Allocator> RawVecInner<A> {
     #[inline]
-    const fn new_in(alloc: A, align: usize) -> Self {
+    const fn new_in(alloc: A, align: Alignment) -> Self {
+        // SAFETY: `Alignment` is non-zero.
         let ptr = unsafe { core::mem::transmute(align) };
         // `cap: 0` means "unallocated". zero-sized types are ignored.
         Self { ptr, cap: ZERO_CAP, alloc }
@@ -465,7 +466,7 @@ impl<A: Allocator> RawVecInner<A> {
 
         // Don't allocate here because `Drop` will not deallocate when `capacity` is 0.
         if layout.size() == 0 {
-            return Ok(Self::new_in(alloc, elem_layout.align()));
+            return Ok(Self::new_in(alloc, elem_layout.alignment()));
         }
 
         if let Err(err) = alloc_guard(layout.size()) {
diff --git a/library/alloctests/lib.rs b/library/alloctests/lib.rs
index 6ce8a6d9ca1..3241b4b0045 100644
--- a/library/alloctests/lib.rs
+++ b/library/alloctests/lib.rs
@@ -28,6 +28,7 @@
 #![feature(iter_next_chunk)]
 #![feature(maybe_uninit_slice)]
 #![feature(maybe_uninit_uninit_array_transpose)]
+#![feature(ptr_alignment_type)]
 #![feature(ptr_internals)]
 #![feature(sized_type_properties)]
 #![feature(slice_iter_mut_as_mut_slice)]
diff --git a/library/core/src/alloc/layout.rs b/library/core/src/alloc/layout.rs
index 1595a3af883..e8a03aadc33 100644
--- a/library/core/src/alloc/layout.rs
+++ b/library/core/src/alloc/layout.rs
@@ -520,6 +520,14 @@ impl Layout {
             unsafe { Ok(Layout::from_size_align_unchecked(array_size, align.as_usize())) }
         }
     }
+
+    /// Perma-unstable access to `align` as `Alignment` type.
+    #[unstable(issue = "none", feature = "std_internals")]
+    #[doc(hidden)]
+    #[inline]
+    pub const fn alignment(&self) -> Alignment {
+        self.align
+    }
 }
 
 #[stable(feature = "alloc_layout", since = "1.28.0")]