about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorSimon Sapin <simon.sapin@exyr.org>2016-09-15 11:19:19 +0200
committerSimon Sapin <simon.sapin@exyr.org>2016-09-15 18:48:16 +0200
commit5ce9feeb8cb48a89feffe579cfc1a9281b4dfbb0 (patch)
treef619e76dc0caf0752720201e3f61f2c9e3cb583b /src
parenteba2270a9cc1c0785cf42fa87fe154f425a2eea0 (diff)
downloadrust-5ce9feeb8cb48a89feffe579cfc1a9281b4dfbb0.tar.gz
rust-5ce9feeb8cb48a89feffe579cfc1a9281b4dfbb0.zip
Add std::ptr::eq, for referential equality of &T references.
Fixes https://github.com/rust-lang/rfcs/issues/1155
Diffstat (limited to 'src')
-rw-r--r--src/libcore/ptr.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs
index 8c8925251e5..69682652a6a 100644
--- a/src/libcore/ptr.rs
+++ b/src/libcore/ptr.rs
@@ -479,6 +479,40 @@ impl<T: ?Sized> PartialEq for *mut T {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: ?Sized> Eq for *mut T {}
 
+/// Compare raw pointers for equality.
+///
+/// This is the same as using the `==` operator, but less generic:
+/// the arguments have to be `*const T` raw pointers,
+/// not anything that implements `PartialEq`.
+///
+/// This can be used to compare `&T` references (which coerce to `*const T` implicitly)
+/// by their address rather than comparing the values they point to
+/// (which is what the `PartialEq for &T` implementation does).
+///
+/// # Examples
+///
+/// ```
+/// #![feature(ptr_eq)]
+/// use std::ptr;
+///
+/// let five = 5;
+/// let other_five = 5;
+/// let five_ref = &five;
+/// let same_five_ref = &five;
+/// let other_five_ref = &other_five;
+///
+/// assert!(five_ref == same_five_ref);
+/// assert!(five_ref == other_five_ref);
+///
+/// assert!(ptr::eq(five_ref, same_five_ref));
+/// assert!(!ptr::eq(five_ref, other_five_ref));
+/// ```
+#[unstable(feature = "ptr_eq", reason = "newly added", issue = "36497")]
+#[inline]
+pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
+    a == b
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: ?Sized> Clone for *const T {
     #[inline]