about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-08-24 22:14:13 +0200
committerGitHub <noreply@github.com>2024-08-24 22:14:13 +0200
commit2c4338802a9f4f1c5b5abc39d94c146411ded1ed (patch)
treea30adcae44208c773e10fe7d6b5e638e377a4bc8
parent2a7f2da422d5dfaca89a9e8c3b8c5dcf22a38e9a (diff)
parent4325ac9652016896edc4209805d23c1cb0ea5ae0 (diff)
downloadrust-2c4338802a9f4f1c5b5abc39d94c146411ded1ed.tar.gz
rust-2c4338802a9f4f1c5b5abc39d94c146411ded1ed.zip
Rollup merge of #129323 - Urgau:ptr_fn_addr_eq, r=Mark-Simulacrum
Implement `ptr::fn_addr_eq`

This PR implements https://github.com/rust-lang/libs-team/issues/323: `ptr::fn_addr_eq`.

r? libs
-rw-r--r--library/core/src/ptr/mod.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs
index 3fe5212cea7..65e473d99b3 100644
--- a/library/core/src/ptr/mod.rs
+++ b/library/core/src/ptr/mod.rs
@@ -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)