Skip to content

Commit 78e9621

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 d872845 commit 78e9621

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

library/alloc/src/lib.rs

+1
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

+5-4
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,7 @@ 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+
return Ok(Self::new_in(alloc, elem_layout.alignment()));
469470
}
470471

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

library/alloctests/lib.rs

+1
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)]

library/core/src/alloc/layout.rs

+8
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,14 @@ impl Layout {
520520
unsafe { Ok(Layout::from_size_align_unchecked(array_size, align.as_usize())) }
521521
}
522522
}
523+
524+
/// Perma-unstable access to `align` as `Alignment` type.
525+
#[unstable(issue = "none", feature = "std_internals")]
526+
#[doc(hidden)]
527+
#[inline]
528+
pub const fn alignment(&self) -> Alignment {
529+
self.align
530+
}
523531
}
524532

525533
#[stable(feature = "alloc_layout", since = "1.28.0")]

0 commit comments

Comments
 (0)