Skip to content

Commit

Permalink
Rollup merge of #129323 - Urgau:ptr_fn_addr_eq, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
Implement `ptr::fn_addr_eq`

This PR implements rust-lang/libs-team#323: `ptr::fn_addr_eq`.

r? libs
  • Loading branch information
matthiaskrgr authored Aug 24, 2024
2 parents 2a7f2da + 4325ac9 commit 2c43388
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2130,6 +2130,33 @@ pub fn addr_eq<T: ?Sized, U: ?Sized>(p: *const T, q: *const U) -> bool {
(p as *const ()) == (q as *const ())
}

/// Compares the *addresses* of the two function pointers for equality.
///
/// Function pointers comparisons can have surprising results since
/// they are never guaranteed to be unique and could vary between different
/// code generation units. Furthermore, different functions could have the
/// same address after being merged together.
///
/// This is the same as `f == g` but using this function makes clear
/// that you are aware of these potentially surprising semantics.
///
/// # Examples
///
/// ```
/// #![feature(ptr_fn_addr_eq)]
/// use std::ptr;
///
/// fn a() { println!("a"); }
/// fn b() { println!("b"); }
/// assert!(!ptr::fn_addr_eq(a as fn(), b as fn()));
/// ```
#[unstable(feature = "ptr_fn_addr_eq", issue = "129322")]
#[inline(always)]
#[must_use = "function pointer comparison produces a value"]
pub fn fn_addr_eq<T: FnPtr, U: FnPtr>(f: T, g: U) -> bool {
f.addr() == g.addr()
}

/// Hash a raw pointer.
///
/// This can be used to hash a `&T` reference (which coerces to `*const T` implicitly)
Expand Down

0 comments on commit 2c43388

Please sign in to comment.