Skip to content

Commit fdac9e4

Browse files
committed
Implement cmp traits for Rc<T> and add a ptr_eq method.
1 parent ce45bb7 commit fdac9e4

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/libstd/rc.rs

+55
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use ops::Drop;
2222
use kinds::{Freeze, Send};
2323
use clone::{Clone, DeepClone};
2424
use cell::RefCell;
25+
use cmp::{Eq, TotalEq, Ord, TotalOrd, Ordering};
2526

2627
struct RcBox<T> {
2728
value: T,
@@ -80,6 +81,60 @@ impl<T> Rc<T> {
8081
pub fn borrow<'r>(&'r self) -> &'r T {
8182
unsafe { &(*self.ptr).value }
8283
}
84+
85+
/// Determine if two reference-counted pointers point to the same object
86+
#[inline]
87+
pub fn ptr_eq(&self, other: &Rc<T>) -> bool {
88+
self.ptr == other.ptr
89+
}
90+
}
91+
92+
impl<T: Eq> Eq for Rc<T> {
93+
#[inline]
94+
fn eq(&self, other: &Rc<T>) -> bool {
95+
unsafe { (*self.ptr).value == (*other.ptr).value }
96+
}
97+
98+
#[inline]
99+
fn ne(&self, other: &Rc<T>) -> bool {
100+
unsafe { (*self.ptr).value != (*other.ptr).value }
101+
}
102+
}
103+
104+
impl<T: TotalEq> TotalEq for Rc<T> {
105+
#[inline]
106+
fn equals(&self, other: &Rc<T>) -> bool {
107+
unsafe { (*self.ptr).value.equals(&(*other.ptr).value) }
108+
}
109+
}
110+
111+
impl<T: Ord> Ord for Rc<T> {
112+
#[inline]
113+
fn lt(&self, other: &Rc<T>) -> bool {
114+
unsafe { (*self.ptr).value < (*other.ptr).value }
115+
}
116+
117+
#[inline]
118+
fn le(&self, other: &Rc<T>) -> bool {
119+
unsafe { (*self.ptr).value <= (*other.ptr).value }
120+
}
121+
122+
#[inline]
123+
fn ge(&self, other: &Rc<T>) -> bool {
124+
unsafe { (*self.ptr).value >= (*other.ptr).value }
125+
}
126+
127+
#[inline]
128+
fn gt(&self, other: &Rc<T>) -> bool {
129+
unsafe { (*self.ptr).value > (*other.ptr).value }
130+
}
131+
}
132+
133+
impl<T: TotalOrd> TotalOrd for Rc<T> {
134+
#[inline]
135+
fn cmp(&self, other: &Rc<T>) -> Ordering {
136+
unsafe { (*self.ptr).value.cmp(&(*other.ptr).value) }
137+
}
83138
}
84139

85140
impl<T> Clone for Rc<T> {

0 commit comments

Comments
 (0)