]> git.lizzy.rs Git - rust.git/commitdiff
Implement cmp traits for Rc<T> and add a ptr_eq method.
authorLéo Testard <leo.testard@gmail.com>
Sun, 24 Nov 2013 16:29:44 +0000 (17:29 +0100)
committerLéo Testard <leo.testard@gmail.com>
Sun, 24 Nov 2013 16:29:44 +0000 (17:29 +0100)
src/libstd/rc.rs

index 242533773d7cec59cddae60049a8797078c11dd9..1b1546e57a32a58bbbb5a5a4d9c2dfded0f6dc31 100644 (file)
@@ -22,6 +22,7 @@
 use kinds::{Freeze, Send};
 use clone::{Clone, DeepClone};
 use cell::RefCell;
+use cmp::{Eq, TotalEq, Ord, TotalOrd, Ordering};
 
 struct RcBox<T> {
     value: T,
@@ -80,6 +81,60 @@ pub unsafe fn new_unchecked(value: T) -> Rc<T> {
     pub fn borrow<'r>(&'r self) -> &'r T {
         unsafe { &(*self.ptr).value }
     }
+
+    /// Determine if two reference-counted pointers point to the same object
+    #[inline]
+    pub fn ptr_eq(&self, other: &Rc<T>) -> bool {
+        self.ptr == other.ptr
+    }
+}
+
+impl<T: Eq> Eq for Rc<T> {
+    #[inline]
+    fn eq(&self, other: &Rc<T>) -> bool {
+        unsafe { (*self.ptr).value == (*other.ptr).value }
+    }
+
+    #[inline]
+    fn ne(&self, other: &Rc<T>) -> bool {
+        unsafe { (*self.ptr).value != (*other.ptr).value }
+    }
+}
+
+impl<T: TotalEq> TotalEq for Rc<T> {
+    #[inline]
+    fn equals(&self, other: &Rc<T>) -> bool {
+        unsafe { (*self.ptr).value.equals(&(*other.ptr).value) }
+    }
+}
+
+impl<T: Ord> Ord for Rc<T> {
+    #[inline]
+    fn lt(&self, other: &Rc<T>) -> bool {
+        unsafe { (*self.ptr).value < (*other.ptr).value }
+    }
+
+    #[inline]
+    fn le(&self, other: &Rc<T>) -> bool {
+        unsafe { (*self.ptr).value <= (*other.ptr).value }
+    }
+
+    #[inline]
+    fn ge(&self, other: &Rc<T>) -> bool {
+        unsafe { (*self.ptr).value >= (*other.ptr).value }
+    }
+
+    #[inline]
+    fn gt(&self, other: &Rc<T>) -> bool {
+        unsafe { (*self.ptr).value > (*other.ptr).value }
+    }
+}
+
+impl<T: TotalOrd> TotalOrd for Rc<T> {
+    #[inline]
+    fn cmp(&self, other: &Rc<T>) -> Ordering {
+        unsafe { (*self.ptr).value.cmp(&(*other.ptr).value) }
+    }
 }
 
 impl<T> Clone for Rc<T> {