Skip to content

Commit

Permalink
auto merge of #13102 : huonw/rust/totaleq-deriving, r=thestinger
Browse files Browse the repository at this point in the history
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
:(.)
  • Loading branch information
bors committed Mar 23, 2014
2 parents cafb7ed + f6db0ef commit 903e838
Show file tree
Hide file tree
Showing 23 changed files with 74 additions and 208 deletions.
2 changes: 1 addition & 1 deletion src/compiletest/runtest.rs
Expand Up @@ -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");
}

Expand Down
57 changes: 13 additions & 44 deletions src/libcollections/btree.rs
Expand Up @@ -94,17 +94,12 @@ impl<K: Clone + TotalOrd, V: Clone> Clone for BTree<K, V> {

impl<K: TotalOrd, V: TotalEq> Eq for BTree<K, V> {
fn eq(&self, other: &BTree<K, V>) -> bool {
self.equals(other)
}
}

impl<K: TotalOrd, V: TotalEq> TotalEq for BTree<K, V> {
///Testing equality on BTrees by comparing the root.
fn equals(&self, other: &BTree<K, V>) -> bool {
self.root.cmp(&other.root) == Equal
}
}

impl<K: TotalOrd, V: TotalEq> TotalEq for BTree<K, V> {}

impl<K: TotalOrd, V: TotalEq> Ord for BTree<K, V> {
fn lt(&self, other: &BTree<K, V>) -> bool {
self.cmp(other) == Less
Expand Down Expand Up @@ -204,14 +199,6 @@ impl<K: Clone + TotalOrd, V: Clone> Clone for Node<K, V> {

impl<K: TotalOrd, V: TotalEq> Eq for Node<K, V> {
fn eq(&self, other: &Node<K, V>) -> bool {
self.equals(other)
}
}

impl<K: TotalOrd, V: TotalEq> TotalEq for Node<K, V> {
///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<K, V>) -> bool{
match *self{
BranchNode(ref branch) => {
if other.is_leaf() {
Expand All @@ -232,6 +219,8 @@ impl<K: TotalOrd, V: TotalEq> TotalEq for Node<K, V> {
}
}

impl<K: TotalOrd, V: TotalEq> TotalEq for Node<K, V> {}

impl<K: TotalOrd, V: TotalEq> Ord for Node<K, V> {
fn lt(&self, other: &Node<K, V>) -> bool {
self.cmp(other) == Less
Expand Down Expand Up @@ -405,16 +394,11 @@ impl<K: Clone + TotalOrd, V: Clone> Clone for Leaf<K, V> {

impl<K: TotalOrd, V: TotalEq> Eq for Leaf<K, V> {
fn eq(&self, other: &Leaf<K, V>) -> bool {
self.equals(other)
self.elts == other.elts
}
}

impl<K: TotalOrd, V: TotalEq> TotalEq for Leaf<K, V> {
///Implementation of equals function for leaves that compares LeafElts.
fn equals(&self, other: &Leaf<K, V>) -> bool {
self.elts.equals(&other.elts)
}
}
impl<K: TotalOrd, V: TotalEq> TotalEq for Leaf<K, V> {}

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

impl<K: TotalOrd, V: TotalEq> Eq for Branch<K, V> {
fn eq(&self, other: &Branch<K, V>) -> bool {
self.equals(other)
self.elts == other.elts
}
}

impl<K: TotalOrd, V: TotalEq> TotalEq for Branch<K, V> {
///Equals function for Branches--compares all the elements in each branch
fn equals(&self, other: &Branch<K, V>) -> bool {
self.elts.equals(&other.elts)
}
}
impl<K: TotalOrd, V: TotalEq> TotalEq for Branch<K, V> {}

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

impl<K: TotalOrd, V: TotalEq> Eq for LeafElt<K, V> {
fn eq(&self, other: &LeafElt<K, V>) -> bool {
self.equals(other)
self.key == other.key && self.value == other.value
}
}

impl<K: TotalOrd, V: TotalEq> TotalEq for LeafElt<K, V> {
///TotalEq for LeafElts
fn equals(&self, other: &LeafElt<K, V>) -> bool {
self.key.equals(&other.key) && self.value.equals(&other.value)
}
}
impl<K: TotalOrd, V: TotalEq> TotalEq for LeafElt<K, V> {}

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

impl<K: TotalOrd, V: TotalEq> Eq for BranchElt<K, V>{
fn eq(&self, other: &BranchElt<K, V>) -> bool {
self.equals(other)
self.key == other.key && self.value == other.value
}
}

impl<K: TotalOrd, V: TotalEq> TotalEq for BranchElt<K, V>{
///TotalEq for BranchElts
fn equals(&self, other: &BranchElt<K, V>) -> bool {
self.key.equals(&other.key)&&self.value.equals(&other.value)
}
}
impl<K: TotalOrd, V: TotalEq> TotalEq for BranchElt<K, V>{}

impl<K: TotalOrd, V: TotalEq> Ord for BranchElt<K, V> {
fn lt(&self, other: &BranchElt<K, V>) -> bool {
Expand Down Expand Up @@ -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.
Expand Down
41 changes: 6 additions & 35 deletions src/libnum/bigint.rs
Expand Up @@ -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]
Expand Down Expand Up @@ -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<Sign> for Sign {
/// Negate Sign value.
#[inline]
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/libnum/rational.rs
Expand Up @@ -147,20 +147,20 @@ macro_rules! cmp_impl {
cmp_impl!(impl $imp, $($method -> bool),+)
};
// return something other than a Ratio<T>
(impl $imp:ident, $($method:ident -> $res:ty),+) => {
(impl $imp:ident, $($method:ident -> $res:ty),*) => {
impl<T: Mul<T,T> + $imp> $imp for Ratio<T> {
$(
#[inline]
fn $method(&self, other: &Ratio<T>) -> $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 */
Expand Down
33 changes: 17 additions & 16 deletions src/libstd/cmp.rs
Expand Up @@ -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.
Expand All @@ -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 {}
}
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/iter.rs
Expand Up @@ -2180,13 +2180,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<A: TotalEq, T: Iterator<A>>(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 },
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/libstd/managed.rs
Expand Up @@ -45,10 +45,7 @@ impl<T: TotalOrd> TotalOrd for @T {
}

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

#[test]
fn test() {
Expand Down
5 changes: 1 addition & 4 deletions src/libstd/owned.rs
Expand Up @@ -53,7 +53,4 @@ impl<T: TotalOrd> TotalOrd for ~T {
}

#[cfg(not(test))]
impl<T: TotalEq> TotalEq for ~T {
#[inline]
fn equals(&self, other: &~T) -> bool { (**self).equals(*other) }
}
impl<T: TotalEq> TotalEq for ~T {}
6 changes: 1 addition & 5 deletions src/libstd/reference.rs
Expand Up @@ -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 {}
12 changes: 2 additions & 10 deletions src/libstd/slice.rs
Expand Up @@ -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<T:TotalEq> TotalEq for ~[T] {
#[inline]
fn equals(&self, other: &~[T]) -> bool { self.as_slice().equals(&other.as_slice()) }
}
impl<T:TotalEq> TotalEq for ~[T] {}

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

0 comments on commit 903e838

Please sign in to comment.