Skip to content

Commit 1578329

Browse files
committed
Auto merge of #116325 - scottmcm:addr_eq, r=dtolnay
Add `ptr::addr_eq` Seconded ACP: rust-lang/libs-team#274 (comment) Tracking issue: #116324 cc `@dtolnay` #106447
2 parents 30ec747 + 1878791 commit 1578329

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

library/core/src/ptr/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1868,6 +1868,28 @@ pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
18681868
a == b
18691869
}
18701870

1871+
/// Compares the *addresses* of the two pointers for equality,
1872+
/// ignoring any metadata in fat pointers.
1873+
///
1874+
/// If the arguments are thin pointers of the same type,
1875+
/// then this is the same as [`eq`].
1876+
///
1877+
/// # Examples
1878+
///
1879+
/// ```
1880+
/// #![feature(ptr_addr_eq)]
1881+
///
1882+
/// let whole: &[i32; 3] = &[1, 2, 3];
1883+
/// let first: &i32 = &whole[0];
1884+
/// assert!(std::ptr::addr_eq(whole, first));
1885+
/// assert!(!std::ptr::eq::<dyn std::fmt::Debug>(whole, first));
1886+
/// ```
1887+
#[unstable(feature = "ptr_addr_eq", issue = "116324")]
1888+
#[inline(always)]
1889+
pub fn addr_eq<T: ?Sized, U: ?Sized>(p: *const T, q: *const U) -> bool {
1890+
(p as *const ()) == (q as *const ())
1891+
}
1892+
18711893
/// Hash a raw pointer.
18721894
///
18731895
/// This can be used to hash a `&T` reference (which coerces to `*const T` implicitly)

0 commit comments

Comments
 (0)