mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-06 12:04:36 +00:00
constify pointer::is_aligned{,_to}
This commit is contained in:
parent
8cf6b16185
commit
6f6320a0a9
@ -130,6 +130,7 @@
|
|||||||
#![feature(const_option)]
|
#![feature(const_option)]
|
||||||
#![feature(const_option_ext)]
|
#![feature(const_option_ext)]
|
||||||
#![feature(const_pin)]
|
#![feature(const_pin)]
|
||||||
|
#![feature(const_pointer_is_aligned)]
|
||||||
#![feature(const_ptr_sub_ptr)]
|
#![feature(const_ptr_sub_ptr)]
|
||||||
#![feature(const_replace)]
|
#![feature(const_replace)]
|
||||||
#![feature(const_result_drop)]
|
#![feature(const_result_drop)]
|
||||||
|
@ -1363,10 +1363,13 @@ impl<T: ?Sized> *const T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether the pointer is properly aligned for `T`.
|
/// Returns whether the pointer is properly aligned for `T`.
|
||||||
|
// #[cfg(not(bootstrap))] -- Calling this function in a const context from the bootstrap
|
||||||
|
// compiler will always return false.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[inline]
|
#[inline]
|
||||||
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
|
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
|
||||||
pub fn is_aligned(self) -> bool
|
#[rustc_const_unstable(feature = "const_pointer_is_aligned", issue = "none")]
|
||||||
|
pub const fn is_aligned(self) -> bool
|
||||||
where
|
where
|
||||||
T: Sized,
|
T: Sized,
|
||||||
{
|
{
|
||||||
@ -1381,16 +1384,26 @@ impl<T: ?Sized> *const T {
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// The function panics if `align` is not a power-of-two (this includes 0).
|
/// The function panics if `align` is not a power-of-two (this includes 0).
|
||||||
|
// #[cfg(not(bootstrap))] -- Calling this function in a const context from the bootstrap
|
||||||
|
// compiler will always return false.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[inline]
|
#[inline]
|
||||||
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
|
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
|
||||||
pub fn is_aligned_to(self, align: usize) -> bool {
|
#[rustc_const_unstable(feature = "const_pointer_is_aligned", issue = "none")]
|
||||||
if !align.is_power_of_two() {
|
pub const fn is_aligned_to(self, align: usize) -> bool {
|
||||||
panic!("is_aligned_to: align is not a power-of-two");
|
assert!(align.is_power_of_two(), "is_aligned_to: align is not a power-of-two");
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn runtime(ptr: *const u8, align: usize) -> bool {
|
||||||
|
ptr.addr() & (align - 1) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cast is needed for `T: !Sized`
|
const fn comptime(ptr: *const u8, align: usize) -> bool {
|
||||||
self.cast::<u8>().addr() & align - 1 == 0
|
ptr.align_offset(align) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// SAFETY: `ptr.align_offset(align)` returns 0 if and only if the pointer is already aligned.
|
||||||
|
unsafe { intrinsics::const_eval_select((self.cast::<u8>(), align), comptime, runtime) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1631,10 +1631,13 @@ impl<T: ?Sized> *mut T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether the pointer is properly aligned for `T`.
|
/// Returns whether the pointer is properly aligned for `T`.
|
||||||
|
// #[cfg(not(bootstrap))] -- Calling this function in a const context from the bootstrap
|
||||||
|
// compiler will always return false.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[inline]
|
#[inline]
|
||||||
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
|
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
|
||||||
pub fn is_aligned(self) -> bool
|
#[rustc_const_unstable(feature = "const_pointer_is_aligned", issue = "none")]
|
||||||
|
pub const fn is_aligned(self) -> bool
|
||||||
where
|
where
|
||||||
T: Sized,
|
T: Sized,
|
||||||
{
|
{
|
||||||
@ -1649,16 +1652,26 @@ impl<T: ?Sized> *mut T {
|
|||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// The function panics if `align` is not a power-of-two (this includes 0).
|
/// The function panics if `align` is not a power-of-two (this includes 0).
|
||||||
|
// #[cfg(not(bootstrap))] -- Calling this function in a const context from the bootstrap
|
||||||
|
// compiler will always return false.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[inline]
|
#[inline]
|
||||||
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
|
#[unstable(feature = "pointer_is_aligned", issue = "96284")]
|
||||||
pub fn is_aligned_to(self, align: usize) -> bool {
|
#[rustc_const_unstable(feature = "const_pointer_is_aligned", issue = "none")]
|
||||||
if !align.is_power_of_two() {
|
pub const fn is_aligned_to(self, align: usize) -> bool {
|
||||||
panic!("is_aligned_to: align is not a power-of-two");
|
assert!(align.is_power_of_two(), "is_aligned_to: align is not a power-of-two");
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn runtime(ptr: *mut u8, align: usize) -> bool {
|
||||||
|
ptr.addr() & (align - 1) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cast is needed for `T: !Sized`
|
const fn comptime(ptr: *mut u8, align: usize) -> bool {
|
||||||
self.cast::<u8>().addr() & align - 1 == 0
|
ptr.align_offset(align) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// SAFETY: `ptr.align_offset(align)` returns 0 if and only if the pointer is already aligned.
|
||||||
|
unsafe { intrinsics::const_eval_select((self.cast::<u8>(), align), comptime, runtime) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user