Skip to content

Commit

Permalink
Golf
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Jun 20, 2024
1 parent 96bf871 commit 44b1911
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
23 changes: 17 additions & 6 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, Alignment, NonNull, Unique};
use core::ptr::{self, NonNull, Unique};

#[cfg(not(no_global_oom_handling))]
use crate::alloc::handle_alloc_error;
Expand Down Expand Up @@ -297,7 +297,20 @@ impl<T, A: Allocator> RawVec<T, A> {
}

fn current_memory(&self) -> Option<(NonNull<u8>, Layout)> {
if T::IS_ZST || self.cap.0 == 0 {
// Reduce the amount of code we need to monomorphize per `T`.
#[inline]
#[rustc_no_mir_inline]
unsafe fn inner(size: usize, align: usize, cap: usize) -> Layout {
// SAFETY: Precondition guaranteed by the caller
unsafe {
let size = size.unchecked_mul(cap);
Layout::from_size_align_unchecked(size, align)
}
}

let cap = self.cap.0;

if T::IS_ZST || cap == 0 {
None
} else {
// We could use Layout::array here which ensures the absence of isize and usize overflows
Expand All @@ -306,10 +319,8 @@ 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 = Alignment::of::<T>();
let size = mem::size_of::<T>().unchecked_mul(self.cap.0);
let layout = Layout::from_size_alignment(size, align).unwrap_unchecked();
Some((self.ptr.cast().into(), layout))
let layout = inner(mem::size_of::<T>(), mem::align_of::<T>(), cap);
Some((self.non_null().cast(), layout))
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions library/core/src/alloc/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ 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]
pub const fn from_size_alignment(size: usize, align: Alignment) -> Result<Self, LayoutError> {
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
2 changes: 2 additions & 0 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,7 @@ impl<T, E> Result<T, E> {
#[track_caller]
#[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
pub unsafe fn unwrap_unchecked(self) -> T {
debug_assert!(self.is_ok());
match self {
Ok(t) => t,
// SAFETY: the safety contract must be upheld by the caller.
Expand Down Expand Up @@ -1512,6 +1513,7 @@ impl<T, E> Result<T, E> {
#[track_caller]
#[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
pub unsafe fn unwrap_err_unchecked(self) -> E {
debug_assert!(self.is_err());
match self {
// SAFETY: the safety contract must be upheld by the caller.
Ok(_) => unsafe { hint::unreachable_unchecked() },
Expand Down

0 comments on commit 44b1911

Please sign in to comment.