Skip to content

Commit 394983d

Browse files
committed
Pass Alignment for RawVecInner::new_in
Encodes the safety constraint that `Unique`'s pointer must be non-zero into the API.
1 parent 48994b1 commit 394983d

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
#![feature(pattern)]
136136
#![feature(pin_coerce_unsized_trait)]
137137
#![feature(pointer_like_trait)]
138+
#![feature(ptr_alignment_type)]
138139
#![feature(ptr_internals)]
139140
#![feature(ptr_metadata)]
140141
#![feature(set_ptr_value)]

library/alloc/src/raw_vec/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use core::marker::PhantomData;
88
use core::mem::{ManuallyDrop, MaybeUninit, SizedTypeProperties};
9-
use core::ptr::{self, NonNull, Unique};
9+
use core::ptr::{self, Alignment, NonNull, Unique};
1010
use core::{cmp, hint};
1111

1212
#[cfg(not(no_global_oom_handling))]
@@ -177,7 +177,7 @@ impl<T, A: Allocator> RawVec<T, A> {
177177
/// the returned `RawVec`.
178178
#[inline]
179179
pub(crate) const fn new_in(alloc: A) -> Self {
180-
Self { inner: RawVecInner::new_in(alloc, align_of::<T>()), _marker: PhantomData }
180+
Self { inner: RawVecInner::new_in(alloc, Alignment::of::<T>()), _marker: PhantomData }
181181
}
182182

183183
/// 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> {
409409

410410
impl<A: Allocator> RawVecInner<A> {
411411
#[inline]
412-
const fn new_in(alloc: A, align: usize) -> Self {
412+
const fn new_in(alloc: A, align: Alignment) -> Self {
413+
// SAFETY: `Alignment` is non-zero.
413414
let ptr = unsafe { core::mem::transmute(align) };
414415
// `cap: 0` means "unallocated". zero-sized types are ignored.
415416
Self { ptr, cap: ZERO_CAP, alloc }
@@ -465,7 +466,9 @@ impl<A: Allocator> RawVecInner<A> {
465466

466467
// Don't allocate here because `Drop` will not deallocate when `capacity` is 0.
467468
if layout.size() == 0 {
468-
return Ok(Self::new_in(alloc, elem_layout.align()));
469+
// SAFETY: `Layout.align` is a non-zero power of two.
470+
let align = unsafe { Alignment::new_unchecked(elem_layout.align()) };
471+
return Ok(Self::new_in(alloc, align));
469472
}
470473

471474
if let Err(err) = alloc_guard(layout.size()) {

library/alloctests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#![feature(iter_next_chunk)]
2929
#![feature(maybe_uninit_slice)]
3030
#![feature(maybe_uninit_uninit_array_transpose)]
31+
#![feature(ptr_alignment_type)]
3132
#![feature(ptr_internals)]
3233
#![feature(sized_type_properties)]
3334
#![feature(slice_iter_mut_as_mut_slice)]

0 commit comments

Comments
 (0)