diff options
| author | Jorge Aparicio <japaricious@gmail.com> | 2014-11-21 00:14:05 -0500 |
|---|---|---|
| committer | Jorge Aparicio <japaricious@gmail.com> | 2014-12-03 10:41:42 -0500 |
| commit | 2840d58dab0144c5589b60322c4f681bd8052aba (patch) | |
| tree | 0c48a16b8c12222544acc8f2e7289381bd5af631 /src/libcore | |
| parent | 2578de9d6090210d9e94fd013190f387c8a88048 (diff) | |
| download | rust-2840d58dab0144c5589b60322c4f681bd8052aba.tar.gz rust-2840d58dab0144c5589b60322c4f681bd8052aba.zip | |
Overload the `==` operator
- String == &str == CowString - Vec == &[T] == &mut [T] == [T, ..N] == CowVec - InternedString == &str
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/array.rs | 27 | ||||
| -rw-r--r-- | src/libcore/borrow.rs | 7 | ||||
| -rw-r--r-- | src/libcore/cmp.rs | 26 | ||||
| -rw-r--r-- | src/libcore/iter.rs | 12 | ||||
| -rw-r--r-- | src/libcore/slice.rs | 6 |
5 files changed, 62 insertions, 16 deletions
diff --git a/src/libcore/array.rs b/src/libcore/array.rs index 60765e82cb4..ffaf35414ea 100644 --- a/src/libcore/array.rs +++ b/src/libcore/array.rs @@ -18,6 +18,7 @@ use clone::Clone; use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering}; use fmt; use kinds::Copy; +use ops::Deref; use option::Option; // macro for implementing n-ary tuple functions and operations @@ -39,17 +40,37 @@ macro_rules! array_impls { } #[unstable = "waiting for PartialEq to stabilize"] - impl<T:PartialEq> PartialEq for [T, ..$N] { + impl<A, B> PartialEq<[B, ..$N]> for [A, ..$N] where A: PartialEq<B> { #[inline] - fn eq(&self, other: &[T, ..$N]) -> bool { + fn eq(&self, other: &[B, ..$N]) -> bool { self[] == other[] } #[inline] - fn ne(&self, other: &[T, ..$N]) -> bool { + fn ne(&self, other: &[B, ..$N]) -> bool { self[] != other[] } } + impl<'a, A, B, Rhs> PartialEq<Rhs> for [A, ..$N] where + A: PartialEq<B>, + Rhs: Deref<[B]>, + { + #[inline(always)] + fn eq(&self, other: &Rhs) -> bool { PartialEq::eq(self[], &**other) } + #[inline(always)] + fn ne(&self, other: &Rhs) -> bool { PartialEq::ne(self[], &**other) } + } + + impl<'a, A, B, Lhs> PartialEq<[B, ..$N]> for Lhs where + A: PartialEq<B>, + Lhs: Deref<[A]> + { + #[inline(always)] + fn eq(&self, other: &[B, ..$N]) -> bool { PartialEq::eq(&**self, other[]) } + #[inline(always)] + fn ne(&self, other: &[B, ..$N]) -> bool { PartialEq::ne(&**self, other[]) } + } + #[unstable = "waiting for Eq to stabilize"] impl<T:Eq> Eq for [T, ..$N] { } diff --git a/src/libcore/borrow.rs b/src/libcore/borrow.rs index 06fda8d6092..b88fb914dd8 100644 --- a/src/libcore/borrow.rs +++ b/src/libcore/borrow.rs @@ -196,9 +196,12 @@ impl<'a, T, Sized? B> Ord for Cow<'a, T, B> where B: Ord + ToOwned<T> { } } -impl<'a, T, Sized? B> PartialEq for Cow<'a, T, B> where B: PartialEq + ToOwned<T> { +impl<'a, 'b, T, U, Sized? B, Sized? C> PartialEq<Cow<'b, U, C>> for Cow<'a, T, B> where + B: PartialEq<C> + ToOwned<T>, + C: ToOwned<U>, +{ #[inline] - fn eq(&self, other: &Cow<'a, T, B>) -> bool { + fn eq(&self, other: &Cow<'b, U, C>) -> bool { PartialEq::eq(&**self, &**other) } } diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 11878dc76d9..d5001a08b1d 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -400,11 +400,11 @@ mod impls { // & pointers #[unstable = "Trait is unstable."] - impl<'a, Sized? T: PartialEq> PartialEq for &'a T { + impl<'a, 'b, Sized? A, Sized? B> PartialEq<&'b B> for &'a A where A: PartialEq<B> { #[inline] - fn eq(&self, other: & &'a T) -> bool { PartialEq::eq(*self, *other) } + fn eq(&self, other: & &'b B) -> bool { PartialEq::eq(*self, *other) } #[inline] - fn ne(&self, other: & &'a T) -> bool { PartialEq::ne(*self, *other) } + fn ne(&self, other: & &'b B) -> bool { PartialEq::ne(*self, *other) } } #[unstable = "Trait is unstable."] impl<'a, Sized? T: PartialOrd> PartialOrd for &'a T { @@ -432,11 +432,11 @@ mod impls { // &mut pointers #[unstable = "Trait is unstable."] - impl<'a, Sized? T: PartialEq> PartialEq for &'a mut T { + impl<'a, 'b, Sized? A, Sized? B> PartialEq<&'b mut B> for &'a mut A where A: PartialEq<B> { #[inline] - fn eq(&self, other: &&'a mut T) -> bool { PartialEq::eq(*self, *other) } + fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) } #[inline] - fn ne(&self, other: &&'a mut T) -> bool { PartialEq::ne(*self, *other) } + fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) } } #[unstable = "Trait is unstable."] impl<'a, Sized? T: PartialOrd> PartialOrd for &'a mut T { @@ -460,4 +460,18 @@ mod impls { } #[unstable = "Trait is unstable."] impl<'a, Sized? T: Eq> Eq for &'a mut T {} + + impl<'a, 'b, Sized? A, Sized? B> PartialEq<&'b mut B> for &'a A where A: PartialEq<B> { + #[inline] + fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) } + #[inline] + fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) } + } + + impl<'a, 'b, Sized? A, Sized? B> PartialEq<&'b B> for &'a mut A where A: PartialEq<B> { + #[inline] + fn eq(&self, other: &&'b B) -> bool { PartialEq::eq(*self, *other) } + #[inline] + fn ne(&self, other: &&'b B) -> bool { PartialEq::ne(*self, *other) } + } } diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 2d488a4b155..f9595f0663d 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -2473,7 +2473,11 @@ pub mod order { } /// Compare `a` and `b` for equality (Using partial equality, `PartialEq`) - pub fn eq<A: PartialEq, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool { + pub fn eq<A, B, L, R>(mut a: L, mut b: R) -> bool where + A: PartialEq<B>, + L: Iterator<A>, + R: Iterator<B>, + { loop { match (a.next(), b.next()) { (None, None) => return true, @@ -2484,7 +2488,11 @@ pub mod order { } /// Compare `a` and `b` for nonequality (Using partial equality, `PartialEq`) - pub fn ne<A: PartialEq, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool { + pub fn ne<A, B, L, R>(mut a: L, mut b: R) -> bool where + A: PartialEq<B>, + L: Iterator<A>, + R: Iterator<B>, + { loop { match (a.next(), b.next()) { (None, None) => return false, diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 950f04a5d97..b445dba6117 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -1802,12 +1802,12 @@ pub mod bytes { // #[unstable = "waiting for DST"] -impl<T: PartialEq> PartialEq for [T] { - fn eq(&self, other: &[T]) -> bool { +impl<A, B> PartialEq<[B]> for [A] where A: PartialEq<B> { + fn eq(&self, other: &[B]) -> bool { self.len() == other.len() && order::eq(self.iter(), other.iter()) } - fn ne(&self, other: &[T]) -> bool { + fn ne(&self, other: &[B]) -> bool { self.len() != other.len() || order::ne(self.iter(), other.iter()) } |
