Skip to content

Commit

Permalink
Use Alignment::of more
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Jun 18, 2024
1 parent 289a208 commit 31f0305
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
#![feature(maybe_uninit_uninit_array_transpose)]
#![feature(panic_internals)]
#![feature(pattern)]
#![feature(ptr_alignment_type)]
#![feature(ptr_internals)]
#![feature(ptr_metadata)]
#![feature(ptr_sub_ptr)]
Expand Down
6 changes: 3 additions & 3 deletions library/alloc/src/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::alloc::LayoutError;
use core::cmp;
use core::hint;
use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
use core::ptr::{self, NonNull, Unique};
use core::ptr::{self, Alignment, NonNull, Unique};

#[cfg(not(no_global_oom_handling))]
use crate::alloc::handle_alloc_error;
Expand Down Expand Up @@ -306,9 +306,9 @@ impl<T, A: Allocator> RawVec<T, A> {
// support such types. So we can do better by skipping some checks and avoid an unwrap.
const { assert!(mem::size_of::<T>() % mem::align_of::<T>() == 0) };
unsafe {
let align = mem::align_of::<T>();
let align = Alignment::of::<T>();
let size = mem::size_of::<T>().unchecked_mul(self.cap.0);
let layout = Layout::from_size_align_unchecked(size, align);
let layout = Layout::from_size_alignment(size, align).unwrap_unchecked();
Some((self.ptr.cast().into(), layout))
}
}
Expand Down
4 changes: 3 additions & 1 deletion library/core/src/alloc/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ impl Layout {
}

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

0 comments on commit 31f0305

Please sign in to comment.