diff options
| author | Simon Sapin <simon.sapin@exyr.org> | 2016-08-25 18:43:40 +0200 |
|---|---|---|
| committer | Simon Sapin <simon.sapin@exyr.org> | 2016-09-15 18:48:16 +0200 |
| commit | eba2270a9cc1c0785cf42fa87fe154f425a2eea0 (patch) | |
| tree | c0d1099f1b0d3bd7e474046b3d9bab4c9e01a6b9 /src/liballoc | |
| parent | 16ff9e22cdb552fd10e6cee2eb22f0c5da6d7e79 (diff) | |
| download | rust-eba2270a9cc1c0785cf42fa87fe154f425a2eea0.tar.gz rust-eba2270a9cc1c0785cf42fa87fe154f425a2eea0.zip | |
Add `pub fn ptr_eq(this: &Self, other: &Self) -> bool` to `Rc` and `Arc`.
Servo and Kuchiki have had helper functions doing this for some time.
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/arc.rs | 37 | ||||
| -rw-r--r-- | src/liballoc/rc.rs | 37 |
2 files changed, 74 insertions, 0 deletions
diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index 3d579641b96..5f9ccd1820c 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -331,6 +331,33 @@ impl<T: ?Sized> Arc<T> { deallocate(ptr as *mut u8, size_of_val(&*ptr), align_of_val(&*ptr)) } } + + #[inline] + #[unstable(feature = "ptr_eq", + reason = "newly added", + issue = "36497")] + /// Return whether two `Arc` references point to the same value + /// (not just values that compare equal). + /// + /// # Examples + /// + /// ``` + /// #![feature(ptr_eq)] + /// + /// use std::sync::Arc; + /// + /// let five = Arc::new(5); + /// let same_five = five.clone(); + /// let other_five = Arc::new(5); + /// + /// assert!(Arc::ptr_eq(&five, &same_five)); + /// assert!(!Arc::ptr_eq(&five, &other_five)); + /// ``` + pub fn ptr_eq(this: &Self, other: &Self) -> bool { + let this_ptr: *const ArcInner<T> = *this.ptr; + let other_ptr: *const ArcInner<T> = *other.ptr; + this_ptr == other_ptr + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -1200,6 +1227,16 @@ mod tests { let foo: Weak<usize> = Weak::new(); assert!(foo.upgrade().is_none()); } + + #[test] + fn test_ptr_eq() { + let five = Arc::new(5); + let same_five = five.clone(); + let other_five = Arc::new(5); + + assert!(Arc::ptr_eq(&five, &same_five)); + assert!(!Arc::ptr_eq(&five, &other_five)); + } } #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index dadddbc2cb3..32e5587ff41 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -376,6 +376,33 @@ impl<T: ?Sized> Rc<T> { None } } + + #[inline] + #[unstable(feature = "ptr_eq", + reason = "newly added", + issue = "36497")] + /// Return whether two `Rc` references point to the same value + /// (not just values that compare equal). + /// + /// # Examples + /// + /// ``` + /// #![feature(ptr_eq)] + /// + /// use std::rc::Rc; + /// + /// let five = Rc::new(5); + /// let same_five = five.clone(); + /// let other_five = Rc::new(5); + /// + /// assert!(Rc::ptr_eq(&five, &same_five)); + /// assert!(!Rc::ptr_eq(&five, &other_five)); + /// ``` + pub fn ptr_eq(this: &Self, other: &Self) -> bool { + let this_ptr: *const RcBox<T> = *this.ptr; + let other_ptr: *const RcBox<T> = *other.ptr; + this_ptr == other_ptr + } } impl<T: Clone> Rc<T> { @@ -1174,6 +1201,16 @@ mod tests { let foo: Weak<usize> = Weak::new(); assert!(foo.upgrade().is_none()); } + + #[test] + fn test_ptr_eq() { + let five = Rc::new(5); + let same_five = five.clone(); + let other_five = Rc::new(5); + + assert!(Rc::ptr_eq(&five, &same_five)); + assert!(!Rc::ptr_eq(&five, &other_five)); + } } #[stable(feature = "rust1", since = "1.0.0")] |
