about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorLéo Testard <leo.testard@gmail.com>2013-11-24 17:29:44 +0100
committerLéo Testard <leo.testard@gmail.com>2013-11-24 17:29:44 +0100
commitfdac9e470cd87429b6aefc6e02772163a8d41fc8 (patch)
treed50730f2b723aedcb9c3673cbe7a2fed4466437f /src/libstd
parentce45bb7f4448fc810b35961c23bc23a27d685754 (diff)
downloadrust-fdac9e470cd87429b6aefc6e02772163a8d41fc8.tar.gz
rust-fdac9e470cd87429b6aefc6e02772163a8d41fc8.zip
Implement cmp traits for Rc<T> and add a ptr_eq method.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/rc.rs55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/libstd/rc.rs b/src/libstd/rc.rs
index 242533773d7..1b1546e57a3 100644
--- a/src/libstd/rc.rs
+++ b/src/libstd/rc.rs
@@ -22,6 +22,7 @@ use ops::Drop;
 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 @@ impl<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> {