Skip to content

Commit 31f0305

Browse files
committed
Use Alignment::of more
1 parent 289a208 commit 31f0305

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
#![feature(maybe_uninit_uninit_array_transpose)]
139139
#![feature(panic_internals)]
140140
#![feature(pattern)]
141+
#![feature(ptr_alignment_type)]
141142
#![feature(ptr_internals)]
142143
#![feature(ptr_metadata)]
143144
#![feature(ptr_sub_ptr)]

library/alloc/src/raw_vec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::alloc::LayoutError;
44
use core::cmp;
55
use core::hint;
66
use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
7-
use core::ptr::{self, NonNull, Unique};
7+
use core::ptr::{self, Alignment, NonNull, Unique};
88

99
#[cfg(not(no_global_oom_handling))]
1010
use crate::alloc::handle_alloc_error;
@@ -306,9 +306,9 @@ impl<T, A: Allocator> RawVec<T, A> {
306306
// support such types. So we can do better by skipping some checks and avoid an unwrap.
307307
const { assert!(mem::size_of::<T>() % mem::align_of::<T>() == 0) };
308308
unsafe {
309-
let align = mem::align_of::<T>();
309+
let align = Alignment::of::<T>();
310310
let size = mem::size_of::<T>().unchecked_mul(self.cap.0);
311-
let layout = Layout::from_size_align_unchecked(size, align);
311+
let layout = Layout::from_size_alignment(size, align).unwrap_unchecked();
312312
Some((self.ptr.cast().into(), layout))
313313
}
314314
}

library/core/src/alloc/layout.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ impl Layout {
9797
}
9898

9999
/// Internal helper constructor to skip revalidating alignment validity.
100+
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
101+
#[rustc_const_unstable(feature = "ptr_alignment_type", issue = "102070")]
100102
#[inline]
101-
const fn from_size_alignment(size: usize, align: Alignment) -> Result<Self, LayoutError> {
103+
pub const fn from_size_alignment(size: usize, align: Alignment) -> Result<Self, LayoutError> {
102104
if size > Self::max_size_for_align(align) {
103105
return Err(LayoutError);
104106
}

0 commit comments

Comments
 (0)