diff --git a/src/compiletest/runtest.rs b/src/compiletest/runtest.rs index eef1a1ac241dc..51fa002420cb8 100644 --- a/src/compiletest/runtest.rs +++ b/src/compiletest/runtest.rs @@ -337,7 +337,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) { } } - if tool_path.equals(&~"") { + if tool_path.is_empty() { fatal(~"cannot found android cross path"); } diff --git a/src/libcollections/btree.rs b/src/libcollections/btree.rs index 6411b6bc97435..3f53ede6027eb 100644 --- a/src/libcollections/btree.rs +++ b/src/libcollections/btree.rs @@ -94,17 +94,12 @@ impl Clone for BTree { impl Eq for BTree { fn eq(&self, other: &BTree) -> bool { - self.equals(other) - } -} - -impl TotalEq for BTree { - ///Testing equality on BTrees by comparing the root. - fn equals(&self, other: &BTree) -> bool { self.root.cmp(&other.root) == Equal } } +impl TotalEq for BTree {} + impl Ord for BTree { fn lt(&self, other: &BTree) -> bool { self.cmp(other) == Less @@ -204,14 +199,6 @@ impl Clone for Node { impl Eq for Node { fn eq(&self, other: &Node) -> bool { - self.equals(other) - } -} - -impl TotalEq for Node { - ///Returns whether two nodes are equal based on the keys of each element. - ///Two nodes are equal if all of their keys are the same. - fn equals(&self, other: &Node) -> bool{ match *self{ BranchNode(ref branch) => { if other.is_leaf() { @@ -232,6 +219,8 @@ impl TotalEq for Node { } } +impl TotalEq for Node {} + impl Ord for Node { fn lt(&self, other: &Node) -> bool { self.cmp(other) == Less @@ -405,16 +394,11 @@ impl Clone for Leaf { impl Eq for Leaf { fn eq(&self, other: &Leaf) -> bool { - self.equals(other) + self.elts == other.elts } } -impl TotalEq for Leaf { - ///Implementation of equals function for leaves that compares LeafElts. - fn equals(&self, other: &Leaf) -> bool { - self.elts.equals(&other.elts) - } -} +impl TotalEq for Leaf {} impl Ord for Leaf { fn lt(&self, other: &Leaf) -> bool { @@ -639,16 +623,11 @@ impl Clone for Branch { impl Eq for Branch { fn eq(&self, other: &Branch) -> bool { - self.equals(other) + self.elts == other.elts } } -impl TotalEq for Branch { - ///Equals function for Branches--compares all the elements in each branch - fn equals(&self, other: &Branch) -> bool { - self.elts.equals(&other.elts) - } -} +impl TotalEq for Branch {} impl Ord for Branch { fn lt(&self, other: &Branch) -> bool { @@ -712,16 +691,11 @@ impl Clone for LeafElt { impl Eq for LeafElt { fn eq(&self, other: &LeafElt) -> bool { - self.equals(other) + self.key == other.key && self.value == other.value } } -impl TotalEq for LeafElt { - ///TotalEq for LeafElts - fn equals(&self, other: &LeafElt) -> bool { - self.key.equals(&other.key) && self.value.equals(&other.value) - } -} +impl TotalEq for LeafElt {} impl Ord for LeafElt { fn lt(&self, other: &LeafElt) -> bool { @@ -766,16 +740,11 @@ impl Clone for BranchElt { impl Eq for BranchElt{ fn eq(&self, other: &BranchElt) -> bool { - self.equals(other) + self.key == other.key && self.value == other.value } } -impl TotalEq for BranchElt{ - ///TotalEq for BranchElts - fn equals(&self, other: &BranchElt) -> bool { - self.key.equals(&other.key)&&self.value.equals(&other.value) - } -} +impl TotalEq for BranchElt{} impl Ord for BranchElt { fn lt(&self, other: &BranchElt) -> bool { @@ -900,7 +869,7 @@ mod test_btree { fn btree_clone_test() { let b = BTree::new(1, ~"abc", 2); let b2 = b.clone(); - assert!(b.root.equals(&b2.root)) + assert!(b.root == b2.root) } //Tests the BTree's cmp() method when one node is "less than" another. diff --git a/src/libnum/bigint.rs b/src/libnum/bigint.rs index 5716a1dedf3ae..89ad2b5d1bc9e 100644 --- a/src/libnum/bigint.rs +++ b/src/libnum/bigint.rs @@ -92,15 +92,11 @@ pub struct BigUint { impl Eq for BigUint { #[inline] - fn eq(&self, other: &BigUint) -> bool { self.equals(other) } -} - -impl TotalEq for BigUint { - #[inline] - fn equals(&self, other: &BigUint) -> bool { + fn eq(&self, other: &BigUint) -> bool { match self.cmp(other) { Equal => true, _ => false } } } +impl TotalEq for BigUint {} impl Ord for BigUint { #[inline] @@ -852,31 +848,9 @@ fn get_radix_base(radix: uint) -> (uint, uint) { } /// A Sign is a `BigInt`'s composing element. -#[deriving(Eq, Clone, Show)] +#[deriving(Eq, Ord, TotalEq, TotalOrd, Clone, Show)] pub enum Sign { Minus, Zero, Plus } -impl Ord for Sign { - #[inline] - fn lt(&self, other: &Sign) -> bool { - match self.cmp(other) { Less => true, _ => false} - } -} - -impl TotalEq for Sign { - #[inline] - fn equals(&self, other: &Sign) -> bool { *self == *other } -} -impl TotalOrd for Sign { - #[inline] - fn cmp(&self, other: &Sign) -> Ordering { - match (*self, *other) { - (Minus, Minus) | (Zero, Zero) | (Plus, Plus) => Equal, - (Minus, Zero) | (Minus, Plus) | (Zero, Plus) => Less, - _ => Greater - } - } -} - impl Neg for Sign { /// Negate Sign value. #[inline] @@ -898,16 +872,13 @@ pub struct BigInt { impl Eq for BigInt { #[inline] - fn eq(&self, other: &BigInt) -> bool { self.equals(other) } -} - -impl TotalEq for BigInt { - #[inline] - fn equals(&self, other: &BigInt) -> bool { + fn eq(&self, other: &BigInt) -> bool { match self.cmp(other) { Equal => true, _ => false } } } +impl TotalEq for BigInt {} + impl Ord for BigInt { #[inline] fn lt(&self, other: &BigInt) -> bool { diff --git a/src/libnum/rational.rs b/src/libnum/rational.rs index a71674c412281..6fb3d492432b2 100644 --- a/src/libnum/rational.rs +++ b/src/libnum/rational.rs @@ -147,20 +147,20 @@ macro_rules! cmp_impl { cmp_impl!(impl $imp, $($method -> bool),+) }; // return something other than a Ratio - (impl $imp:ident, $($method:ident -> $res:ty),+) => { + (impl $imp:ident, $($method:ident -> $res:ty),*) => { impl + $imp> $imp for Ratio { $( #[inline] fn $method(&self, other: &Ratio) -> $res { (self.numer * other.denom). $method (&(self.denom*other.numer)) } - )+ + )* } }; } cmp_impl!(impl Eq, eq, ne) -cmp_impl!(impl TotalEq, equals) cmp_impl!(impl Ord, lt, gt, le, ge) +cmp_impl!(impl TotalEq, ) cmp_impl!(impl TotalOrd, cmp -> cmp::Ordering) /* Arithmetic */ diff --git a/src/libstd/cmp.rs b/src/libstd/cmp.rs index 5130da44ed577..e956a1cdf1d63 100644 --- a/src/libstd/cmp.rs +++ b/src/libstd/cmp.rs @@ -42,6 +42,21 @@ pub trait Eq { } /// Trait for equality comparisons where `a == b` and `a != b` are strict inverses. +#[cfg(not(stage0))] +pub trait TotalEq: Eq { + // FIXME #13101: this method is used solely by #[deriving] to + // assert that every component of a type implements #[deriving] + // itself, the current deriving infrastructure means doing this + // assertion without using a method on this trait is nearly + // impossible. + // + // This should never be implemented by hand. + #[doc(hidden)] + #[inline(always)] + fn assert_receiver_is_total_eq(&self) {} +} + +#[cfg(stage0)] pub trait TotalEq: Eq { /// This method must return the same value as `eq`. It exists to prevent /// deriving `TotalEq` from fields not implementing the `TotalEq` trait. @@ -52,10 +67,7 @@ pub trait TotalEq: Eq { macro_rules! totaleq_impl( ($t:ty) => { - impl TotalEq for $t { - #[inline] - fn equals(&self, other: &$t) -> bool { *self == *other } - } + impl TotalEq for $t {} } ) @@ -84,12 +96,7 @@ pub trait TotalOrd: TotalEq + Ord { fn cmp(&self, other: &Self) -> Ordering; } -impl TotalEq for Ordering { - #[inline] - fn equals(&self, other: &Ordering) -> bool { - *self == *other - } -} +impl TotalEq for Ordering {} impl TotalOrd for Ordering { #[inline] fn cmp(&self, other: &Ordering) -> Ordering { @@ -194,12 +201,6 @@ mod test { assert_eq!(12.cmp(-5), Greater); } - #[test] - fn test_int_totaleq() { - assert!(5.equals(&5)); - assert!(!2.equals(&17)); - } - #[test] fn test_ordering_order() { assert!(Less < Equal); diff --git a/src/libstd/iter.rs b/src/libstd/iter.rs index 320383d4f81c2..f2cb46acc2ea9 100644 --- a/src/libstd/iter.rs +++ b/src/libstd/iter.rs @@ -2195,13 +2195,13 @@ pub mod order { use option::{Some, None}; use super::Iterator; - /// Compare `a` and `b` for equality using `TotalOrd` + /// Compare `a` and `b` for equality using `TotalEq` pub fn equals>(mut a: T, mut b: T) -> bool { loop { match (a.next(), b.next()) { (None, None) => return true, (None, _) | (_, None) => return false, - (Some(x), Some(y)) => if !x.equals(&y) { return false }, + (Some(x), Some(y)) => if x != y { return false }, } } } diff --git a/src/libstd/managed.rs b/src/libstd/managed.rs index 141ed7a92067a..bf73c05440c14 100644 --- a/src/libstd/managed.rs +++ b/src/libstd/managed.rs @@ -45,10 +45,7 @@ impl TotalOrd for @T { } #[cfg(not(test))] -impl TotalEq for @T { - #[inline] - fn equals(&self, other: &@T) -> bool { (**self).equals(*other) } -} +impl TotalEq for @T {} #[test] fn test() { diff --git a/src/libstd/owned.rs b/src/libstd/owned.rs index dc8ea34c84bd3..1fa86c5311799 100644 --- a/src/libstd/owned.rs +++ b/src/libstd/owned.rs @@ -53,7 +53,4 @@ impl TotalOrd for ~T { } #[cfg(not(test))] -impl TotalEq for ~T { - #[inline] - fn equals(&self, other: &~T) -> bool { (**self).equals(*other) } -} +impl TotalEq for ~T {} diff --git a/src/libstd/reference.rs b/src/libstd/reference.rs index fdbca2b9f3322..eb615afd85f9a 100644 --- a/src/libstd/reference.rs +++ b/src/libstd/reference.rs @@ -54,8 +54,4 @@ impl<'a, T: TotalOrd> TotalOrd for &'a T { } #[cfg(not(test))] -impl<'a, T: TotalEq> TotalEq for &'a T { - #[inline] - fn equals(&self, other: & &'a T) -> bool { (**self).equals(*other) } -} - +impl<'a, T: TotalEq> TotalEq for &'a T {} diff --git a/src/libstd/slice.rs b/src/libstd/slice.rs index 12718c559235c..bb5b36896af85 100644 --- a/src/libstd/slice.rs +++ b/src/libstd/slice.rs @@ -649,17 +649,9 @@ pub mod traits { fn ne(&self, other: &~[T]) -> bool { !self.eq(other) } } - impl<'a,T:TotalEq> TotalEq for &'a [T] { - fn equals(&self, other: & &'a [T]) -> bool { - self.len() == other.len() && - order::equals(self.iter(), other.iter()) - } - } + impl<'a,T:TotalEq> TotalEq for &'a [T] {} - impl TotalEq for ~[T] { - #[inline] - fn equals(&self, other: &~[T]) -> bool { self.as_slice().equals(&other.as_slice()) } - } + impl TotalEq for ~[T] {} impl<'a,T:Eq, V: Vector> Equiv for &'a [T] { #[inline] diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 0cd5e64783196..b14773d35c5e3 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -1262,16 +1262,11 @@ impl<'a> IntoMaybeOwned<'a> for MaybeOwned<'a> { impl<'a> Eq for MaybeOwned<'a> { #[inline] fn eq(&self, other: &MaybeOwned) -> bool { - self.as_slice().equals(&other.as_slice()) + self.as_slice() == other.as_slice() } } -impl<'a> TotalEq for MaybeOwned<'a> { - #[inline] - fn equals(&self, other: &MaybeOwned) -> bool { - self.as_slice().equals(&other.as_slice()) - } -} +impl<'a> TotalEq for MaybeOwned<'a> {} impl<'a> Ord for MaybeOwned<'a> { #[inline] @@ -1290,7 +1285,7 @@ impl<'a> TotalOrd for MaybeOwned<'a> { impl<'a, S: Str> Equiv for MaybeOwned<'a> { #[inline] fn equiv(&self, other: &S) -> bool { - self.as_slice().equals(&other.as_slice()) + self.as_slice() == other.as_slice() } } @@ -1577,19 +1572,9 @@ pub mod traits { } } - impl<'a> TotalEq for &'a str { - #[inline] - fn equals(&self, other: & &'a str) -> bool { - eq_slice((*self), (*other)) - } - } + impl<'a> TotalEq for &'a str {} - impl TotalEq for ~str { - #[inline] - fn equals(&self, other: &~str) -> bool { - eq_slice((*self), (*other)) - } - } + impl TotalEq for ~str {} impl<'a> Ord for &'a str { #[inline] @@ -4450,11 +4435,9 @@ mod tests { assert_eq!(Owned(~""), Default::default()); assert!(s.cmp(&o) == Equal); - assert!(s.equals(&o)); assert!(s.equiv(&o)); assert!(o.cmp(&s) == Equal); - assert!(o.equals(&s)); assert!(o.equiv(&s)); } diff --git a/src/libstd/tuple.rs b/src/libstd/tuple.rs index e9125dde01172..c4ce6b5ae666f 100644 --- a/src/libstd/tuple.rs +++ b/src/libstd/tuple.rs @@ -75,12 +75,7 @@ macro_rules! tuple_impls { } #[cfg(not(test))] - impl<$($T:TotalEq),+> TotalEq for ($($T,)+) { - #[inline] - fn equals(&self, other: &($($T,)+)) -> bool { - $(self.$refN().equals(other.$refN()))&&+ - } - } + impl<$($T:TotalEq),+> TotalEq for ($($T,)+) {} #[cfg(not(test))] impl<$($T:Ord + Eq),+> Ord for ($($T,)+) { @@ -338,12 +333,6 @@ mod tests { assert!(((1.0, 2.0) < (2.0, nan))); assert!(!((2.0, 2.0) < (2.0, nan))); - // TotalEq - assert!(small.equals(&small)); - assert!(big.equals(&big)); - assert!(!small.equals(&big)); - assert!(!big.equals(&small)); - // TotalOrd assert!(small.cmp(&small) == Equal); assert!(big.cmp(&big) == Equal); diff --git a/src/libstd/unit.rs b/src/libstd/unit.rs index b23dafbca6970..38307f415ac26 100644 --- a/src/libstd/unit.rs +++ b/src/libstd/unit.rs @@ -37,10 +37,7 @@ impl TotalOrd for () { } #[cfg(not(test))] -impl TotalEq for () { - #[inline] - fn equals(&self, _other: &()) -> bool { true } -} +impl TotalEq for () {} #[cfg(not(test))] impl Default for () { diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index c1f5dea91d11d..d00a411e9a073 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -339,12 +339,7 @@ impl Ord for Vec { } } -impl TotalEq for Vec { - #[inline] - fn equals(&self, other: &Vec) -> bool { - self.as_slice().equals(&other.as_slice()) - } -} +impl TotalEq for Vec {} impl TotalOrd for Vec { #[inline] diff --git a/src/libsyntax/ext/deriving/cmp/totaleq.rs b/src/libsyntax/ext/deriving/cmp/totaleq.rs index ffabed95db524..33512b3df5ee5 100644 --- a/src/libsyntax/ext/deriving/cmp/totaleq.rs +++ b/src/libsyntax/ext/deriving/cmp/totaleq.rs @@ -19,9 +19,18 @@ pub fn expand_deriving_totaleq(cx: &mut ExtCtxt, mitem: @MetaItem, item: @Item, push: |@Item|) { - fn cs_equals(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> @Expr { - cs_and(|cx, span, _, _| cx.expr_bool(span, false), - cx, span, substr) + fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> @Expr { + cs_same_method(|cx, span, exprs| { + // create `a.(); b.(); c.(); ...` + // (where method is `assert_receiver_is_total_eq`) + let stmts = exprs.move_iter().map(|e| cx.stmt_expr(e)).collect(); + let block = cx.block(span, stmts, None); + cx.expr_block(block) + }, + |cx, sp, _, _| cx.span_bug(sp, "non matching enums in deriving(TotalEq)?"), + cx, + span, + substr) } let trait_def = TraitDef { @@ -32,14 +41,14 @@ pub fn expand_deriving_totaleq(cx: &mut ExtCtxt, generics: LifetimeBounds::empty(), methods: vec!( MethodDef { - name: "equals", + name: "assert_receiver_is_total_eq", generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), - args: vec!(borrowed_self()), - ret_ty: Literal(Path::new(vec!("bool"))), + args: vec!(), + ret_ty: nil_ty(), inline: true, const_nonmatching: true, - combine_substructure: cs_equals + combine_substructure: cs_total_eq_assert } ) }; diff --git a/src/libsyntax/util/interner.rs b/src/libsyntax/util/interner.rs index 9b73cf533a741..969b9f81785e1 100644 --- a/src/libsyntax/util/interner.rs +++ b/src/libsyntax/util/interner.rs @@ -95,11 +95,7 @@ pub struct RcStr { priv string: Rc<~str>, } -impl TotalEq for RcStr { - fn equals(&self, other: &RcStr) -> bool { - self.as_slice().equals(&other.as_slice()) - } -} +impl TotalEq for RcStr {} impl TotalOrd for RcStr { fn cmp(&self, other: &RcStr) -> Ordering { diff --git a/src/libuuid/lib.rs b/src/libuuid/lib.rs index 47602bfcdf265..49af5ba9b2b8f 100644 --- a/src/libuuid/lib.rs +++ b/src/libuuid/lib.rs @@ -491,14 +491,7 @@ impl Eq for Uuid { } } -/// Test two UUIDs for equality -/// -/// UUIDs are equal only when they are byte-for-byte identical -impl TotalEq for Uuid { - fn equals(&self, other: &Uuid) -> bool { - self.bytes == other.bytes - } -} +impl TotalEq for Uuid {} // FIXME #9845: Test these more thoroughly impl Encodable for Uuid { diff --git a/src/test/run-pass/deriving-cmp-generic-enum.rs b/src/test/run-pass/deriving-cmp-generic-enum.rs index 094d17c100ebd..5d41e275cd3f6 100644 --- a/src/test/run-pass/deriving-cmp-generic-enum.rs +++ b/src/test/run-pass/deriving-cmp-generic-enum.rs @@ -39,9 +39,6 @@ pub fn main() { assert_eq!(*e1 == *e2, eq); assert_eq!(*e1 != *e2, !eq); - // TotalEq - assert_eq!(e1.equals(e2), eq); - // Ord assert_eq!(*e1 < *e2, lt); assert_eq!(*e1 > *e2, gt); diff --git a/src/test/run-pass/deriving-cmp-generic-struct-enum.rs b/src/test/run-pass/deriving-cmp-generic-struct-enum.rs index aedf4732afdbb..c2f0c269d62b3 100644 --- a/src/test/run-pass/deriving-cmp-generic-struct-enum.rs +++ b/src/test/run-pass/deriving-cmp-generic-struct-enum.rs @@ -35,9 +35,6 @@ pub fn main() { assert_eq!(*es1 == *es2, eq); assert_eq!(*es1 != *es2, !eq); - // TotalEq - assert_eq!(es1.equals(es2), eq); - // Ord assert_eq!(*es1 < *es2, lt); assert_eq!(*es1 > *es2, gt); diff --git a/src/test/run-pass/deriving-cmp-generic-struct.rs b/src/test/run-pass/deriving-cmp-generic-struct.rs index d04bdee34e611..4f62d1fa6313a 100644 --- a/src/test/run-pass/deriving-cmp-generic-struct.rs +++ b/src/test/run-pass/deriving-cmp-generic-struct.rs @@ -35,9 +35,6 @@ pub fn main() { assert_eq!(*s1 == *s2, eq); assert_eq!(*s1 != *s2, !eq); - // TotalEq - assert_eq!(s1.equals(s2), eq); - // Ord assert_eq!(*s1 < *s2, lt); assert_eq!(*s1 > *s2, gt); diff --git a/src/test/run-pass/deriving-cmp-generic-tuple-struct.rs b/src/test/run-pass/deriving-cmp-generic-tuple-struct.rs index 5d4cf7c745769..b8ba4c64616ce 100644 --- a/src/test/run-pass/deriving-cmp-generic-tuple-struct.rs +++ b/src/test/run-pass/deriving-cmp-generic-tuple-struct.rs @@ -33,9 +33,6 @@ pub fn main() { assert_eq!(*ts1 == *ts2, eq); assert_eq!(*ts1 != *ts2, !eq); - // TotalEq - assert_eq!(ts1.equals(ts2), eq); - // Ord assert_eq!(*ts1 < *ts2, lt); assert_eq!(*ts1 > *ts2, gt); diff --git a/src/test/run-pass/deriving-cmp-shortcircuit.rs b/src/test/run-pass/deriving-cmp-shortcircuit.rs index 431c856ee88a5..03e6d04d87ff0 100644 --- a/src/test/run-pass/deriving-cmp-shortcircuit.rs +++ b/src/test/run-pass/deriving-cmp-shortcircuit.rs @@ -21,9 +21,7 @@ impl Ord for FailCmp { fn lt(&self, _: &FailCmp) -> bool { fail!("lt") } } -impl TotalEq for FailCmp { - fn equals(&self, _: &FailCmp) -> bool { fail!("equals") } -} +impl TotalEq for FailCmp {} impl TotalOrd for FailCmp { fn cmp(&self, _: &FailCmp) -> Ordering { fail!("cmp") } @@ -41,6 +39,5 @@ pub fn main() { assert!(a != b); assert!(a < b); - assert!(!a.equals(&b)); assert_eq!(a.cmp(&b), ::std::cmp::Less); } diff --git a/src/test/run-pass/deriving-self-lifetime-totalord-totaleq.rs b/src/test/run-pass/deriving-self-lifetime-totalord-totaleq.rs index 723738495e757..1c92104104210 100644 --- a/src/test/run-pass/deriving-self-lifetime-totalord-totaleq.rs +++ b/src/test/run-pass/deriving-self-lifetime-totalord-totaleq.rs @@ -19,10 +19,6 @@ struct A<'a> { pub fn main() { let (a, b) = (A { x: &1 }, A { x: &2 }); - assert!(a.equals(&a)); - assert!(b.equals(&b)); - - assert_eq!(a.cmp(&a), Equal); assert_eq!(b.cmp(&b), Equal);