Skip to content

Commit f6db0ef

Browse files
committed
std: remove the equals method from TotalEq.
`TotalEq` is now just an assertion about the `Eq` impl of a type (i.e. `==` is a total equality if a type implements `TotalEq`) so the extra method is just confusing. Also, a new method magically appeared as a hack to allow deriving to assert that the contents of a struct/enum are also TotalEq, because the deriving infrastructure makes it very hard to do anything but create a trait method. (You didn't hear about this horrible work-around from me :(.)
1 parent 2ddb605 commit f6db0ef

23 files changed

+74
-208
lines changed

src/compiletest/runtest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
337337
}
338338
}
339339

340-
if tool_path.equals(&~"") {
340+
if tool_path.is_empty() {
341341
fatal(~"cannot found android cross path");
342342
}
343343

src/libcollections/btree.rs

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,12 @@ impl<K: Clone + TotalOrd, V: Clone> Clone for BTree<K, V> {
9494

9595
impl<K: TotalOrd, V: TotalEq> Eq for BTree<K, V> {
9696
fn eq(&self, other: &BTree<K, V>) -> bool {
97-
self.equals(other)
98-
}
99-
}
100-
101-
impl<K: TotalOrd, V: TotalEq> TotalEq for BTree<K, V> {
102-
///Testing equality on BTrees by comparing the root.
103-
fn equals(&self, other: &BTree<K, V>) -> bool {
10497
self.root.cmp(&other.root) == Equal
10598
}
10699
}
107100

101+
impl<K: TotalOrd, V: TotalEq> TotalEq for BTree<K, V> {}
102+
108103
impl<K: TotalOrd, V: TotalEq> Ord for BTree<K, V> {
109104
fn lt(&self, other: &BTree<K, V>) -> bool {
110105
self.cmp(other) == Less
@@ -204,14 +199,6 @@ impl<K: Clone + TotalOrd, V: Clone> Clone for Node<K, V> {
204199

205200
impl<K: TotalOrd, V: TotalEq> Eq for Node<K, V> {
206201
fn eq(&self, other: &Node<K, V>) -> bool {
207-
self.equals(other)
208-
}
209-
}
210-
211-
impl<K: TotalOrd, V: TotalEq> TotalEq for Node<K, V> {
212-
///Returns whether two nodes are equal based on the keys of each element.
213-
///Two nodes are equal if all of their keys are the same.
214-
fn equals(&self, other: &Node<K, V>) -> bool{
215202
match *self{
216203
BranchNode(ref branch) => {
217204
if other.is_leaf() {
@@ -232,6 +219,8 @@ impl<K: TotalOrd, V: TotalEq> TotalEq for Node<K, V> {
232219
}
233220
}
234221

222+
impl<K: TotalOrd, V: TotalEq> TotalEq for Node<K, V> {}
223+
235224
impl<K: TotalOrd, V: TotalEq> Ord for Node<K, V> {
236225
fn lt(&self, other: &Node<K, V>) -> bool {
237226
self.cmp(other) == Less
@@ -405,16 +394,11 @@ impl<K: Clone + TotalOrd, V: Clone> Clone for Leaf<K, V> {
405394

406395
impl<K: TotalOrd, V: TotalEq> Eq for Leaf<K, V> {
407396
fn eq(&self, other: &Leaf<K, V>) -> bool {
408-
self.equals(other)
397+
self.elts == other.elts
409398
}
410399
}
411400

412-
impl<K: TotalOrd, V: TotalEq> TotalEq for Leaf<K, V> {
413-
///Implementation of equals function for leaves that compares LeafElts.
414-
fn equals(&self, other: &Leaf<K, V>) -> bool {
415-
self.elts.equals(&other.elts)
416-
}
417-
}
401+
impl<K: TotalOrd, V: TotalEq> TotalEq for Leaf<K, V> {}
418402

419403
impl<K: TotalOrd, V: TotalEq> Ord for Leaf<K, V> {
420404
fn lt(&self, other: &Leaf<K, V>) -> bool {
@@ -639,16 +623,11 @@ impl<K: Clone + TotalOrd, V: Clone> Clone for Branch<K, V> {
639623

640624
impl<K: TotalOrd, V: TotalEq> Eq for Branch<K, V> {
641625
fn eq(&self, other: &Branch<K, V>) -> bool {
642-
self.equals(other)
626+
self.elts == other.elts
643627
}
644628
}
645629

646-
impl<K: TotalOrd, V: TotalEq> TotalEq for Branch<K, V> {
647-
///Equals function for Branches--compares all the elements in each branch
648-
fn equals(&self, other: &Branch<K, V>) -> bool {
649-
self.elts.equals(&other.elts)
650-
}
651-
}
630+
impl<K: TotalOrd, V: TotalEq> TotalEq for Branch<K, V> {}
652631

653632
impl<K: TotalOrd, V: TotalEq> Ord for Branch<K, V> {
654633
fn lt(&self, other: &Branch<K, V>) -> bool {
@@ -712,16 +691,11 @@ impl<K: Clone + TotalOrd, V: Clone> Clone for LeafElt<K, V> {
712691

713692
impl<K: TotalOrd, V: TotalEq> Eq for LeafElt<K, V> {
714693
fn eq(&self, other: &LeafElt<K, V>) -> bool {
715-
self.equals(other)
694+
self.key == other.key && self.value == other.value
716695
}
717696
}
718697

719-
impl<K: TotalOrd, V: TotalEq> TotalEq for LeafElt<K, V> {
720-
///TotalEq for LeafElts
721-
fn equals(&self, other: &LeafElt<K, V>) -> bool {
722-
self.key.equals(&other.key) && self.value.equals(&other.value)
723-
}
724-
}
698+
impl<K: TotalOrd, V: TotalEq> TotalEq for LeafElt<K, V> {}
725699

726700
impl<K: TotalOrd, V: TotalEq> Ord for LeafElt<K, V> {
727701
fn lt(&self, other: &LeafElt<K, V>) -> bool {
@@ -766,16 +740,11 @@ impl<K: Clone + TotalOrd, V: Clone> Clone for BranchElt<K, V> {
766740

767741
impl<K: TotalOrd, V: TotalEq> Eq for BranchElt<K, V>{
768742
fn eq(&self, other: &BranchElt<K, V>) -> bool {
769-
self.equals(other)
743+
self.key == other.key && self.value == other.value
770744
}
771745
}
772746

773-
impl<K: TotalOrd, V: TotalEq> TotalEq for BranchElt<K, V>{
774-
///TotalEq for BranchElts
775-
fn equals(&self, other: &BranchElt<K, V>) -> bool {
776-
self.key.equals(&other.key)&&self.value.equals(&other.value)
777-
}
778-
}
747+
impl<K: TotalOrd, V: TotalEq> TotalEq for BranchElt<K, V>{}
779748

780749
impl<K: TotalOrd, V: TotalEq> Ord for BranchElt<K, V> {
781750
fn lt(&self, other: &BranchElt<K, V>) -> bool {
@@ -900,7 +869,7 @@ mod test_btree {
900869
fn btree_clone_test() {
901870
let b = BTree::new(1, ~"abc", 2);
902871
let b2 = b.clone();
903-
assert!(b.root.equals(&b2.root))
872+
assert!(b.root == b2.root)
904873
}
905874
906875
//Tests the BTree's cmp() method when one node is "less than" another.

src/libnum/bigint.rs

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,11 @@ pub struct BigUint {
9292

9393
impl Eq for BigUint {
9494
#[inline]
95-
fn eq(&self, other: &BigUint) -> bool { self.equals(other) }
96-
}
97-
98-
impl TotalEq for BigUint {
99-
#[inline]
100-
fn equals(&self, other: &BigUint) -> bool {
95+
fn eq(&self, other: &BigUint) -> bool {
10196
match self.cmp(other) { Equal => true, _ => false }
10297
}
10398
}
99+
impl TotalEq for BigUint {}
104100

105101
impl Ord for BigUint {
106102
#[inline]
@@ -852,31 +848,9 @@ fn get_radix_base(radix: uint) -> (uint, uint) {
852848
}
853849

854850
/// A Sign is a `BigInt`'s composing element.
855-
#[deriving(Eq, Clone, Show)]
851+
#[deriving(Eq, Ord, TotalEq, TotalOrd, Clone, Show)]
856852
pub enum Sign { Minus, Zero, Plus }
857853

858-
impl Ord for Sign {
859-
#[inline]
860-
fn lt(&self, other: &Sign) -> bool {
861-
match self.cmp(other) { Less => true, _ => false}
862-
}
863-
}
864-
865-
impl TotalEq for Sign {
866-
#[inline]
867-
fn equals(&self, other: &Sign) -> bool { *self == *other }
868-
}
869-
impl TotalOrd for Sign {
870-
#[inline]
871-
fn cmp(&self, other: &Sign) -> Ordering {
872-
match (*self, *other) {
873-
(Minus, Minus) | (Zero, Zero) | (Plus, Plus) => Equal,
874-
(Minus, Zero) | (Minus, Plus) | (Zero, Plus) => Less,
875-
_ => Greater
876-
}
877-
}
878-
}
879-
880854
impl Neg<Sign> for Sign {
881855
/// Negate Sign value.
882856
#[inline]
@@ -898,16 +872,13 @@ pub struct BigInt {
898872

899873
impl Eq for BigInt {
900874
#[inline]
901-
fn eq(&self, other: &BigInt) -> bool { self.equals(other) }
902-
}
903-
904-
impl TotalEq for BigInt {
905-
#[inline]
906-
fn equals(&self, other: &BigInt) -> bool {
875+
fn eq(&self, other: &BigInt) -> bool {
907876
match self.cmp(other) { Equal => true, _ => false }
908877
}
909878
}
910879

880+
impl TotalEq for BigInt {}
881+
911882
impl Ord for BigInt {
912883
#[inline]
913884
fn lt(&self, other: &BigInt) -> bool {

src/libnum/rational.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,20 @@ macro_rules! cmp_impl {
147147
cmp_impl!(impl $imp, $($method -> bool),+)
148148
};
149149
// return something other than a Ratio<T>
150-
(impl $imp:ident, $($method:ident -> $res:ty),+) => {
150+
(impl $imp:ident, $($method:ident -> $res:ty),*) => {
151151
impl<T: Mul<T,T> + $imp> $imp for Ratio<T> {
152152
$(
153153
#[inline]
154154
fn $method(&self, other: &Ratio<T>) -> $res {
155155
(self.numer * other.denom). $method (&(self.denom*other.numer))
156156
}
157-
)+
157+
)*
158158
}
159159
};
160160
}
161161
cmp_impl!(impl Eq, eq, ne)
162-
cmp_impl!(impl TotalEq, equals)
163162
cmp_impl!(impl Ord, lt, gt, le, ge)
163+
cmp_impl!(impl TotalEq, )
164164
cmp_impl!(impl TotalOrd, cmp -> cmp::Ordering)
165165

166166
/* Arithmetic */

src/libstd/cmp.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ pub trait Eq {
4242
}
4343

4444
/// Trait for equality comparisons where `a == b` and `a != b` are strict inverses.
45+
#[cfg(not(stage0))]
46+
pub trait TotalEq: Eq {
47+
// FIXME #13101: this method is used solely by #[deriving] to
48+
// assert that every component of a type implements #[deriving]
49+
// itself, the current deriving infrastructure means doing this
50+
// assertion without using a method on this trait is nearly
51+
// impossible.
52+
//
53+
// This should never be implemented by hand.
54+
#[doc(hidden)]
55+
#[inline(always)]
56+
fn assert_receiver_is_total_eq(&self) {}
57+
}
58+
59+
#[cfg(stage0)]
4560
pub trait TotalEq: Eq {
4661
/// This method must return the same value as `eq`. It exists to prevent
4762
/// deriving `TotalEq` from fields not implementing the `TotalEq` trait.
@@ -52,10 +67,7 @@ pub trait TotalEq: Eq {
5267

5368
macro_rules! totaleq_impl(
5469
($t:ty) => {
55-
impl TotalEq for $t {
56-
#[inline]
57-
fn equals(&self, other: &$t) -> bool { *self == *other }
58-
}
70+
impl TotalEq for $t {}
5971
}
6072
)
6173

@@ -84,12 +96,7 @@ pub trait TotalOrd: TotalEq + Ord {
8496
fn cmp(&self, other: &Self) -> Ordering;
8597
}
8698

87-
impl TotalEq for Ordering {
88-
#[inline]
89-
fn equals(&self, other: &Ordering) -> bool {
90-
*self == *other
91-
}
92-
}
99+
impl TotalEq for Ordering {}
93100
impl TotalOrd for Ordering {
94101
#[inline]
95102
fn cmp(&self, other: &Ordering) -> Ordering {
@@ -194,12 +201,6 @@ mod test {
194201
assert_eq!(12.cmp(-5), Greater);
195202
}
196203

197-
#[test]
198-
fn test_int_totaleq() {
199-
assert!(5.equals(&5));
200-
assert!(!2.equals(&17));
201-
}
202-
203204
#[test]
204205
fn test_ordering_order() {
205206
assert!(Less < Equal);

src/libstd/iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,13 +2195,13 @@ pub mod order {
21952195
use option::{Some, None};
21962196
use super::Iterator;
21972197

2198-
/// Compare `a` and `b` for equality using `TotalOrd`
2198+
/// Compare `a` and `b` for equality using `TotalEq`
21992199
pub fn equals<A: TotalEq, T: Iterator<A>>(mut a: T, mut b: T) -> bool {
22002200
loop {
22012201
match (a.next(), b.next()) {
22022202
(None, None) => return true,
22032203
(None, _) | (_, None) => return false,
2204-
(Some(x), Some(y)) => if !x.equals(&y) { return false },
2204+
(Some(x), Some(y)) => if x != y { return false },
22052205
}
22062206
}
22072207
}

src/libstd/managed.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ impl<T: TotalOrd> TotalOrd for @T {
4545
}
4646

4747
#[cfg(not(test))]
48-
impl<T: TotalEq> TotalEq for @T {
49-
#[inline]
50-
fn equals(&self, other: &@T) -> bool { (**self).equals(*other) }
51-
}
48+
impl<T: TotalEq> TotalEq for @T {}
5249

5350
#[test]
5451
fn test() {

src/libstd/owned.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,4 @@ impl<T: TotalOrd> TotalOrd for ~T {
5353
}
5454

5555
#[cfg(not(test))]
56-
impl<T: TotalEq> TotalEq for ~T {
57-
#[inline]
58-
fn equals(&self, other: &~T) -> bool { (**self).equals(*other) }
59-
}
56+
impl<T: TotalEq> TotalEq for ~T {}

src/libstd/reference.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,4 @@ impl<'a, T: TotalOrd> TotalOrd for &'a T {
5454
}
5555

5656
#[cfg(not(test))]
57-
impl<'a, T: TotalEq> TotalEq for &'a T {
58-
#[inline]
59-
fn equals(&self, other: & &'a T) -> bool { (**self).equals(*other) }
60-
}
61-
57+
impl<'a, T: TotalEq> TotalEq for &'a T {}

src/libstd/slice.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -649,17 +649,9 @@ pub mod traits {
649649
fn ne(&self, other: &~[T]) -> bool { !self.eq(other) }
650650
}
651651

652-
impl<'a,T:TotalEq> TotalEq for &'a [T] {
653-
fn equals(&self, other: & &'a [T]) -> bool {
654-
self.len() == other.len() &&
655-
order::equals(self.iter(), other.iter())
656-
}
657-
}
652+
impl<'a,T:TotalEq> TotalEq for &'a [T] {}
658653

659-
impl<T:TotalEq> TotalEq for ~[T] {
660-
#[inline]
661-
fn equals(&self, other: &~[T]) -> bool { self.as_slice().equals(&other.as_slice()) }
662-
}
654+
impl<T:TotalEq> TotalEq for ~[T] {}
663655

664656
impl<'a,T:Eq, V: Vector<T>> Equiv<V> for &'a [T] {
665657
#[inline]

0 commit comments

Comments
 (0)