Skip to content

Commit 44b1911

Browse files
committed
Golf
1 parent 96bf871 commit 44b1911

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

library/alloc/src/raw_vec.rs

+17-6
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, Alignment, NonNull, Unique};
7+
use core::ptr::{self, NonNull, Unique};
88

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

299299
fn current_memory(&self) -> Option<(NonNull<u8>, Layout)> {
300-
if T::IS_ZST || self.cap.0 == 0 {
300+
// Reduce the amount of code we need to monomorphize per `T`.
301+
#[inline]
302+
#[rustc_no_mir_inline]
303+
unsafe fn inner(size: usize, align: usize, cap: usize) -> Layout {
304+
// SAFETY: Precondition guaranteed by the caller
305+
unsafe {
306+
let size = size.unchecked_mul(cap);
307+
Layout::from_size_align_unchecked(size, align)
308+
}
309+
}
310+
311+
let cap = self.cap.0;
312+
313+
if T::IS_ZST || cap == 0 {
301314
None
302315
} else {
303316
// We could use Layout::array here which ensures the absence of isize and usize overflows
@@ -306,10 +319,8 @@ impl<T, A: Allocator> RawVec<T, A> {
306319
// support such types. So we can do better by skipping some checks and avoid an unwrap.
307320
const { assert!(mem::size_of::<T>() % mem::align_of::<T>() == 0) };
308321
unsafe {
309-
let align = Alignment::of::<T>();
310-
let size = mem::size_of::<T>().unchecked_mul(self.cap.0);
311-
let layout = Layout::from_size_alignment(size, align).unwrap_unchecked();
312-
Some((self.ptr.cast().into(), layout))
322+
let layout = inner(mem::size_of::<T>(), mem::align_of::<T>(), cap);
323+
Some((self.non_null().cast(), layout))
313324
}
314325
}
315326
}

library/core/src/alloc/layout.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,8 @@ 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")]
102100
#[inline]
103-
pub const fn from_size_alignment(size: usize, align: Alignment) -> Result<Self, LayoutError> {
101+
const fn from_size_alignment(size: usize, align: Alignment) -> Result<Self, LayoutError> {
104102
if size > Self::max_size_for_align(align) {
105103
return Err(LayoutError);
106104
}

library/core/src/result.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,7 @@ impl<T, E> Result<T, E> {
14811481
#[track_caller]
14821482
#[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
14831483
pub unsafe fn unwrap_unchecked(self) -> T {
1484+
debug_assert!(self.is_ok());
14841485
match self {
14851486
Ok(t) => t,
14861487
// SAFETY: the safety contract must be upheld by the caller.
@@ -1512,6 +1513,7 @@ impl<T, E> Result<T, E> {
15121513
#[track_caller]
15131514
#[stable(feature = "option_result_unwrap_unchecked", since = "1.58.0")]
15141515
pub unsafe fn unwrap_err_unchecked(self) -> E {
1516+
debug_assert!(self.is_err());
15151517
match self {
15161518
// SAFETY: the safety contract must be upheld by the caller.
15171519
Ok(_) => unsafe { hint::unreachable_unchecked() },

0 commit comments

Comments
 (0)