about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorThomas de Zeeuw <thomasdezeeuw@gmail.com>2018-11-15 20:24:02 +0100
committerThomas de Zeeuw <thomasdezeeuw@gmail.com>2018-12-03 10:49:33 +0100
commitd4b41fa0313ace66d5f4e5d4a6201cd3aa81b87d (patch)
tree682d5127d22a1788671980ffdb64e1e32f6a7a45 /src/liballoc
parent380dd7d47b574697fe87ef00ad5ffcbb6651c501 (diff)
downloadrust-d4b41fa0313ace66d5f4e5d4a6201cd3aa81b87d.tar.gz
rust-d4b41fa0313ace66d5f4e5d4a6201cd3aa81b87d.zip
Add sync::Weak::ptr_eq
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/sync.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs
index 4f4031e3c4e..0a397f79103 100644
--- a/src/liballoc/sync.rs
+++ b/src/liballoc/sync.rs
@@ -1130,6 +1130,53 @@ impl<T: ?Sized> Weak<T> {
             Some(unsafe { self.ptr.as_ref() })
         }
     }
+
+    /// Returns true if the two `Weak`s point to the same value (not just values
+    /// that compare as equal).
+    ///
+    /// # Notes
+    ///
+    /// Since this compares pointers it means that `Weak::new()` will equal each
+    /// other, even though they don't point to any value.
+    ///
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(weak_ptr_eq)]
+    /// use std::sync::{Arc, Weak};
+    ///
+    /// let first_rc = Arc::new(5);
+    /// let first = Arc::downgrade(&first_rc);
+    /// let second = Arc::downgrade(&first_rc);
+    ///
+    /// assert!(Weak::ptr_eq(&first, &second));
+    ///
+    /// let third_rc = Arc::new(5);
+    /// let third = Arc::downgrade(&third_rc);
+    ///
+    /// assert!(!Weak::ptr_eq(&first, &third));
+    /// ```
+    ///
+    /// Comparing `Weak::new`.
+    ///
+    /// ```
+    /// #![feature(weak_ptr_eq)]
+    /// use std::sync::{Arc, Weak};
+    ///
+    /// let first = Weak::new();
+    /// let second = Weak::new();
+    /// assert!(Weak::ptr_eq(&first, &second));
+    ///
+    /// let third_rc = Arc::new(());
+    /// let third = Arc::downgrade(&third_rc);
+    /// assert!(!Weak::ptr_eq(&first, &third));
+    /// ```
+    #[inline]
+    #[unstable(feature = "weak_ptr_eq", issue = "55981")]
+    pub fn ptr_eq(this: &Self, other: &Self) -> bool {
+        this.ptr.as_ptr() == other.ptr.as_ptr()
+    }
 }
 
 #[stable(feature = "arc_weak", since = "1.4.0")]