From 9eb723d00081938ebc6306a7cb73209e45140c95 Mon Sep 17 00:00:00 2001 From: Daniel Brooks Date: Mon, 12 May 2014 02:04:07 -0700 Subject: [PATCH] define Eq,TotalEq,Ord,TotalOrd for &mut T Also Show, which is useful in assertions. Fixes #14074 --- src/libcore/cmp.rs | 34 +++++++++++++++++++++++++++++++++- src/libstd/fmt/mod.rs | 3 +++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index af611cd94e5..2e5d7c8d720 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -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. // @@ -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 Eq for @T { #[inline] @@ -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); diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs index b4bb6236f08..3ba013df278 100644 --- a/src/libstd/fmt/mod.rs +++ b/src/libstd/fmt/mod.rs @@ -1124,6 +1124,9 @@ impl Show for Box { 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 {