1
Fork 0

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.
This commit is contained in:
Simon Sapin 2016-08-25 18:43:40 +02:00
parent 16ff9e22cd
commit eba2270a9c
2 changed files with 74 additions and 0 deletions

View file

@ -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")]