Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,24 @@ impl<T: ?Sized> *const T {
#[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
#[inline]
pub const fn is_null(self) -> bool {
#[inline]
const fn ct(ptr: *const u8) -> bool {
match (ptr).guaranteed_eq(null()) {
None => false,
Some(res) => res,
}
}

#[inline]
fn rt(ptr: *const u8) -> bool {
ptr.addr() == 0
}

// Compare via a cast to a thin pointer, so fat pointers are only
// considering their "data" part for null-ness.
match (self as *const u8).guaranteed_eq(null()) {
None => false,
Some(res) => res,
}

// SAFETY: NOYET
unsafe { intrinsics::const_eval_select((self as *const u8,), ct, rt) }
}

/// Casts to a pointer of another type.
Expand Down
20 changes: 16 additions & 4 deletions library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,24 @@ impl<T: ?Sized> *mut T {
#[rustc_const_unstable(feature = "const_ptr_is_null", issue = "74939")]
#[inline]
pub const fn is_null(self) -> bool {
#[inline]
const fn ct(ptr: *mut u8) -> bool {
match (ptr).guaranteed_eq(null_mut()) {
None => false,
Some(res) => res,
}
}

#[inline]
fn rt(ptr: *mut u8) -> bool {
ptr.addr() == 0
}

// Compare via a cast to a thin pointer, so fat pointers are only
// considering their "data" part for null-ness.
match (self as *mut u8).guaranteed_eq(null_mut()) {
None => false,
Some(res) => res,
}

// SAFETY: NOYET
unsafe { intrinsics::const_eval_select((self as *mut u8,), ct, rt) }
}

/// Casts to a pointer of another type.
Expand Down