diff options
| author | bors <bors@rust-lang.org> | 2023-10-02 04:17:01 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-10-02 04:17:01 +0000 |
| commit | 15783292e5e26336f76ddc2123d66025ec6d84b7 (patch) | |
| tree | 70df3ea7ff86c40726fb783906d935fd81d46925 | |
| parent | 30ec74728dafd672d9b53d4037694a7dcd71a9de (diff) | |
| parent | 18787914aa24bac7237e7abd96dfffed4fa73b61 (diff) | |
| download | rust-15783292e5e26336f76ddc2123d66025ec6d84b7.tar.gz rust-15783292e5e26336f76ddc2123d66025ec6d84b7.zip | |
Auto merge of #116325 - scottmcm:addr_eq, r=dtolnay
Add `ptr::addr_eq` Seconded ACP: https://github.com/rust-lang/libs-team/issues/274#issuecomment-1741853598 Tracking issue: https://github.com/rust-lang/rust/issues/116324 cc `@dtolnay` https://github.com/rust-lang/rust/issues/106447
| -rw-r--r-- | library/core/src/ptr/mod.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index d1286a1dea7..fe3d46d77f0 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -1868,6 +1868,28 @@ pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool { a == b } +/// Compares the *addresses* of the two pointers for equality, +/// ignoring any metadata in fat pointers. +/// +/// If the arguments are thin pointers of the same type, +/// then this is the same as [`eq`]. +/// +/// # Examples +/// +/// ``` +/// #![feature(ptr_addr_eq)] +/// +/// let whole: &[i32; 3] = &[1, 2, 3]; +/// let first: &i32 = &whole[0]; +/// assert!(std::ptr::addr_eq(whole, first)); +/// assert!(!std::ptr::eq::<dyn std::fmt::Debug>(whole, first)); +/// ``` +#[unstable(feature = "ptr_addr_eq", issue = "116324")] +#[inline(always)] +pub fn addr_eq<T: ?Sized, U: ?Sized>(p: *const T, q: *const U) -> bool { + (p as *const ()) == (q as *const ()) +} + /// Hash a raw pointer. /// /// This can be used to hash a `&T` reference (which coerces to `*const T` implicitly) |
