Skip to content

Commit

Permalink
Use const_eval_select in ptr::is_null
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Dec 18, 2022
1 parent d0dc9ef commit 0af922b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
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

0 comments on commit 0af922b

Please sign in to comment.