Skip to content

Commit 5ce9fee

Browse files
committed
Add std::ptr::eq, for referential equality of &T references.
Fixes rust-lang/rfcs#1155
1 parent eba2270 commit 5ce9fee

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/libcore/ptr.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,40 @@ impl<T: ?Sized> PartialEq for *mut T {
479479
#[stable(feature = "rust1", since = "1.0.0")]
480480
impl<T: ?Sized> Eq for *mut T {}
481481

482+
/// Compare raw pointers for equality.
483+
///
484+
/// This is the same as using the `==` operator, but less generic:
485+
/// the arguments have to be `*const T` raw pointers,
486+
/// not anything that implements `PartialEq`.
487+
///
488+
/// This can be used to compare `&T` references (which coerce to `*const T` implicitly)
489+
/// by their address rather than comparing the values they point to
490+
/// (which is what the `PartialEq for &T` implementation does).
491+
///
492+
/// # Examples
493+
///
494+
/// ```
495+
/// #![feature(ptr_eq)]
496+
/// use std::ptr;
497+
///
498+
/// let five = 5;
499+
/// let other_five = 5;
500+
/// let five_ref = &five;
501+
/// let same_five_ref = &five;
502+
/// let other_five_ref = &other_five;
503+
///
504+
/// assert!(five_ref == same_five_ref);
505+
/// assert!(five_ref == other_five_ref);
506+
///
507+
/// assert!(ptr::eq(five_ref, same_five_ref));
508+
/// assert!(!ptr::eq(five_ref, other_five_ref));
509+
/// ```
510+
#[unstable(feature = "ptr_eq", reason = "newly added", issue = "36497")]
511+
#[inline]
512+
pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
513+
a == b
514+
}
515+
482516
#[stable(feature = "rust1", since = "1.0.0")]
483517
impl<T: ?Sized> Clone for *const T {
484518
#[inline]

0 commit comments

Comments
 (0)