diff options
| author | Thomas de Zeeuw <thomasdezeeuw@gmail.com> | 2018-11-15 16:41:06 +0100 |
|---|---|---|
| committer | Thomas de Zeeuw <thomasdezeeuw@gmail.com> | 2018-12-03 10:49:27 +0100 |
| commit | 380dd7d47b574697fe87ef00ad5ffcbb6651c501 (patch) | |
| tree | 9131b57c8ce95f2acc05743e03a1d8fa485da78e /src/liballoc | |
| parent | 6a2d1b4e15d5de90f8c36181b1d429da658adfd2 (diff) | |
| download | rust-380dd7d47b574697fe87ef00ad5ffcbb6651c501.tar.gz rust-380dd7d47b574697fe87ef00ad5ffcbb6651c501.zip | |
Add rc::Weak.ptr_eq
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/rc.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 705345ce963..064b3180d4c 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -1260,6 +1260,52 @@ 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::rc::{Rc, Weak}; + /// + /// let first_rc = Rc::new(5); + /// let first = Rc::downgrade(&first_rc); + /// let second = Rc::downgrade(&first_rc); + /// + /// assert!(Weak::ptr_eq(&first, &second)); + /// + /// let third_rc = Rc::new(5); + /// let third = Rc::downgrade(&third_rc); + /// + /// assert!(!Weak::ptr_eq(&first, &third)); + /// ``` + /// + /// Comparing `Weak::new`. + /// + /// ``` + /// #![feature(weak_ptr_eq)] + /// use std::rc::{Rc, Weak}; + /// + /// let first = Weak::new(); + /// let second = Weak::new(); + /// assert!(Weak::ptr_eq(&first, &second)); + /// + /// let third_rc = Rc::new(()); + /// let third = Rc::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 = "rc_weak", since = "1.4.0")] |
