about summary refs log tree commit diff
path: root/library/alloc/src/raw_vec
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 /library/alloc/src/raw_vec
parentd872845eae7f9988995fb8ca35f86d08de999409 (diff)
Pass `Alignment` for `RawVecInner::new_in`
Encodes the safety constraint that `Unique`'s pointer must be non-zero
into the API.
Diffstat (limited to 'library/alloc/src/raw_vec')
-rw-r--r--library/alloc/src/raw_vec/mod.rs9
1 files changed, 5 insertions, 4 deletions
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()) {