Skip to content

Implement cmp traits for Rc<T> and add a ptr_eq method. #10634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/libstd/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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> {
Expand Down