address more review comments
* `cfg` only the body of `align_offset` * put explicit panics back * explain why `ptr.align_offset(align) == 0` is slow
This commit is contained in:
parent
093c02ed46
commit
df0bcfe644
2 changed files with 54 additions and 58 deletions
|
@ -1323,21 +1323,6 @@ impl<T: ?Sized> *const T {
|
|||
#[must_use]
|
||||
#[stable(feature = "align_offset", since = "1.36.0")]
|
||||
#[rustc_const_unstable(feature = "const_align_offset", issue = "90962")]
|
||||
#[cfg(not(bootstrap))]
|
||||
pub const fn align_offset(self, align: usize) -> usize
|
||||
where
|
||||
T: Sized,
|
||||
{
|
||||
assert!(align.is_power_of_two(), "align_offset: align is not a power-of-two");
|
||||
|
||||
// SAFETY: `align` has been checked to be a power of 2 above
|
||||
unsafe { align_offset(self, align) }
|
||||
}
|
||||
|
||||
#[stable(feature = "align_offset", since = "1.36.0")]
|
||||
#[rustc_const_unstable(feature = "const_align_offset", issue = "90962")]
|
||||
#[allow(missing_docs)]
|
||||
#[cfg(bootstrap)]
|
||||
pub const fn align_offset(self, align: usize) -> usize
|
||||
where
|
||||
T: Sized,
|
||||
|
@ -1346,6 +1331,8 @@ impl<T: ?Sized> *const T {
|
|||
panic!("align_offset: align is not a power-of-two");
|
||||
}
|
||||
|
||||
#[cfg(bootstrap)]
|
||||
{
|
||||
fn rt_impl<T>(p: *const T, align: usize) -> usize {
|
||||
// SAFETY: `align` has been checked to be a power of 2 above
|
||||
unsafe { align_offset(p, align) }
|
||||
|
@ -1363,6 +1350,13 @@ impl<T: ?Sized> *const T {
|
|||
unsafe { intrinsics::const_eval_select((self, align), ctfe_impl, rt_impl) }
|
||||
}
|
||||
|
||||
#[cfg(not(bootstrap))]
|
||||
{
|
||||
// SAFETY: `align` has been checked to be a power of 2 above
|
||||
unsafe { align_offset(self, align) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns whether the pointer is properly aligned for `T`.
|
||||
///
|
||||
/// # Examples
|
||||
|
@ -1522,13 +1516,17 @@ impl<T: ?Sized> *const T {
|
|||
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
|
||||
#[rustc_const_unstable(feature = "const_pointer_is_aligned", issue = "none")]
|
||||
pub const fn is_aligned_to(self, align: usize) -> bool {
|
||||
assert!(align.is_power_of_two(), "is_aligned_to: align is not a power-of-two");
|
||||
if !align.is_power_of_two() {
|
||||
panic!("is_aligned_to: align is not a power-of-two")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn runtime(ptr: *const u8, align: usize) -> bool {
|
||||
ptr.addr() & (align - 1) == 0
|
||||
}
|
||||
|
||||
// This optimizes to `(ptr + align - 1) & -align == ptr`, which is slightly
|
||||
// slower than `ptr & (align - 1) == 0`
|
||||
const fn comptime(ptr: *const u8, align: usize) -> bool {
|
||||
ptr.align_offset(align) == 0
|
||||
}
|
||||
|
|
|
@ -1591,21 +1591,6 @@ impl<T: ?Sized> *mut T {
|
|||
#[must_use]
|
||||
#[stable(feature = "align_offset", since = "1.36.0")]
|
||||
#[rustc_const_unstable(feature = "const_align_offset", issue = "90962")]
|
||||
#[cfg(not(bootstrap))]
|
||||
pub const fn align_offset(self, align: usize) -> usize
|
||||
where
|
||||
T: Sized,
|
||||
{
|
||||
assert!(align.is_power_of_two(), "align_offset: align is not a power-of-two");
|
||||
|
||||
// SAFETY: `align` has been checked to be a power of 2 above
|
||||
unsafe { align_offset(self, align) }
|
||||
}
|
||||
|
||||
#[stable(feature = "align_offset", since = "1.36.0")]
|
||||
#[rustc_const_unstable(feature = "const_align_offset", issue = "90962")]
|
||||
#[allow(missing_docs)]
|
||||
#[cfg(bootstrap)]
|
||||
pub const fn align_offset(self, align: usize) -> usize
|
||||
where
|
||||
T: Sized,
|
||||
|
@ -1614,6 +1599,8 @@ impl<T: ?Sized> *mut T {
|
|||
panic!("align_offset: align is not a power-of-two");
|
||||
}
|
||||
|
||||
#[cfg(bootstrap)]
|
||||
{
|
||||
fn rt_impl<T>(p: *mut T, align: usize) -> usize {
|
||||
// SAFETY: `align` has been checked to be a power of 2 above
|
||||
unsafe { align_offset(p, align) }
|
||||
|
@ -1631,6 +1618,13 @@ impl<T: ?Sized> *mut T {
|
|||
unsafe { intrinsics::const_eval_select((self, align), ctfe_impl, rt_impl) }
|
||||
}
|
||||
|
||||
#[cfg(not(bootstrap))]
|
||||
{
|
||||
// SAFETY: `align` has been checked to be a power of 2 above
|
||||
unsafe { align_offset(self, align) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns whether the pointer is properly aligned for `T`.
|
||||
///
|
||||
/// # Examples
|
||||
|
@ -1790,13 +1784,17 @@ impl<T: ?Sized> *mut T {
|
|||
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
|
||||
#[rustc_const_unstable(feature = "const_pointer_is_aligned", issue = "none")]
|
||||
pub const fn is_aligned_to(self, align: usize) -> bool {
|
||||
assert!(align.is_power_of_two(), "is_aligned_to: align is not a power-of-two");
|
||||
if !align.is_power_of_two() {
|
||||
panic!("is_aligned_to: align is not a power-of-two")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn runtime(ptr: *mut u8, align: usize) -> bool {
|
||||
ptr.addr() & (align - 1) == 0
|
||||
}
|
||||
|
||||
// This optimizes to `(ptr + align - 1) & -align == ptr`, which is slightly
|
||||
// slower than `ptr & (align - 1) == 0`
|
||||
const fn comptime(ptr: *mut u8, align: usize) -> bool {
|
||||
ptr.align_offset(align) == 0
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue