Skip to content

Commit

Permalink
define Eq,TotalEq,Ord,TotalOrd for &mut T
Browse files Browse the repository at this point in the history
Also Show, which is useful in assertions. Fixes #14074
  • Loading branch information
db48x committed May 14, 2014
1 parent 72fc4a5 commit 9eb723d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/libcore/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down Expand Up @@ -218,6 +218,29 @@ mod impls {
}
impl<'a, T: TotalEq> TotalEq for &'a T {}

// &mut pointers
impl<'a, T: Eq> Eq for &'a mut T {
#[inline]
fn eq(&self, other: &&'a mut T) -> bool { **self == *(*other) }
#[inline]
fn ne(&self, other: &&'a mut T) -> bool { **self != *(*other) }
}
impl<'a, T: Ord> Ord for &'a mut T {
#[inline]
fn lt(&self, other: &&'a mut T) -> bool { **self < **other }
#[inline]
fn le(&self, other: &&'a mut T) -> bool { **self <= **other }
#[inline]
fn ge(&self, other: &&'a mut T) -> bool { **self >= **other }
#[inline]
fn gt(&self, other: &&'a mut T) -> bool { **self > **other }
}
impl<'a, T: TotalOrd> TotalOrd for &'a mut T {
#[inline]
fn cmp(&self, other: &&'a mut T) -> Ordering { (**self).cmp(*other) }
}
impl<'a, T: TotalEq> TotalEq for &'a mut T {}

// @ pointers
impl<T:Eq> Eq for @T {
#[inline]
Expand Down Expand Up @@ -278,6 +301,15 @@ mod test {
assert_eq!(12u.cmp(-5), Greater);
}

#[test]
fn test_mut_int_totalord() {
assert_eq!((&mut 5u).cmp(&10), Less);
assert_eq!((&mut 10u).cmp(&5), Greater);
assert_eq!((&mut 5u).cmp(&5), Equal);
assert_eq!((&mut -5u).cmp(&12), Less);
assert_eq!((&mut 12u).cmp(-5), Greater);
}

#[test]
fn test_ordering_order() {
assert!(Less < Equal);
Expand Down
3 changes: 3 additions & 0 deletions src/libstd/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,9 @@ impl<T: Show> Show for Box<T> {
impl<'a, T: Show> Show for &'a T {
fn fmt(&self, f: &mut Formatter) -> Result { secret_show(*self, f) }
}
impl<'a, T: Show> Show for &'a mut T {
fn fmt(&self, f: &mut Formatter) -> Result { secret_show(*self, f) }
}

impl Bool for bool {
fn fmt(&self, f: &mut Formatter) -> Result {
Expand Down

5 comments on commit 9eb723d

@bors
Copy link
Contributor

@bors bors commented on 9eb723d May 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at db48x@9eb723d

@bors
Copy link
Contributor

@bors bors commented on 9eb723d May 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging db48x/rust/ord-for-mut-refs = 9eb723d into auto

@bors
Copy link
Contributor

@bors bors commented on 9eb723d May 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

db48x/rust/ord-for-mut-refs = 9eb723d merged ok, testing candidate = 73a68cd

@bors
Copy link
Contributor

@bors bors commented on 9eb723d May 15, 2014

@bors
Copy link
Contributor

@bors bors commented on 9eb723d May 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 73a68cd

Please sign in to comment.