Implement ptr::fn_addr_eq
· patricklam/verify-rust-std@62d240d (original) (raw)
`@@ -2130,6 +2130,33 @@ pub fn addr_eq<T: ?Sized, U: ?Sized>(p: *const T, q: *const U) -> bool {
`
2130
2130
`(p as *const ()) == (q as *const ())
`
2131
2131
`}
`
2132
2132
``
``
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
+
2133
2160
`/// Hash a raw pointer.
`
2134
2161
`///
`
2135
2162
`` /// This can be used to hash a &T
reference (which coerces to *const T
implicitly)
``