Skip to content

Commit 2c43388

Browse files
authored
Rollup merge of #129323 - Urgau:ptr_fn_addr_eq, r=Mark-Simulacrum
Implement `ptr::fn_addr_eq` This PR implements rust-lang/libs-team#323: `ptr::fn_addr_eq`. r? libs
2 parents 2a7f2da + 4325ac9 commit 2c43388

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

library/core/src/ptr/mod.rs

+27
Original file line numberDiff line numberDiff line change
@@ -2130,6 +2130,33 @@ pub fn addr_eq<T: ?Sized, U: ?Sized>(p: *const T, q: *const U) -> bool {
21302130
(p as *const ()) == (q as *const ())
21312131
}
21322132

2133+
/// Compares the *addresses* of the two function pointers for equality.
2134+
///
2135+
/// Function pointers comparisons can have surprising results since
2136+
/// they are never guaranteed to be unique and could vary between different
2137+
/// code generation units. Furthermore, different functions could have the
2138+
/// same address after being merged together.
2139+
///
2140+
/// This is the same as `f == g` but using this function makes clear
2141+
/// that you are aware of these potentially surprising semantics.
2142+
///
2143+
/// # Examples
2144+
///
2145+
/// ```
2146+
/// #![feature(ptr_fn_addr_eq)]
2147+
/// use std::ptr;
2148+
///
2149+
/// fn a() { println!("a"); }
2150+
/// fn b() { println!("b"); }
2151+
/// assert!(!ptr::fn_addr_eq(a as fn(), b as fn()));
2152+
/// ```
2153+
#[unstable(feature = "ptr_fn_addr_eq", issue = "129322")]
2154+
#[inline(always)]
2155+
#[must_use = "function pointer comparison produces a value"]
2156+
pub fn fn_addr_eq<T: FnPtr, U: FnPtr>(f: T, g: U) -> bool {
2157+
f.addr() == g.addr()
2158+
}
2159+
21332160
/// Hash a raw pointer.
21342161
///
21352162
/// This can be used to hash a `&T` reference (which coerces to `*const T` implicitly)

0 commit comments

Comments
 (0)