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 | |
| 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
| -rw-r--r-- | src/libcollections/string.rs | 45 | ||||
| -rw-r--r-- | src/libcollections/vec.rs | 63 | ||||
| -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 | ||||
| -rw-r--r-- | src/libsyntax/parse/token.rs | 22 |
8 files changed, 187 insertions, 21 deletions
diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 38b67fbd744..180978b3d77 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -30,7 +30,7 @@ use str::{CharRange, CowString, FromStr, StrAllocating, Owned}; use vec::{DerefVec, Vec, as_vec}; /// A growable string stored as a UTF-8 encoded buffer. -#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord)] +#[deriving(Clone, PartialOrd, Eq, Ord)] #[stable] pub struct String { vec: Vec<u8>, @@ -738,6 +738,49 @@ impl Extend<char> for String { } } +impl PartialEq for String { + #[inline] + fn eq(&self, other: &String) -> bool { PartialEq::eq(&**self, &**other) } + #[inline] + fn ne(&self, other: &String) -> bool { PartialEq::ne(&**self, &**other) } +} + +macro_rules! impl_eq { + ($lhs:ty, $rhs: ty) => { + impl<'a> PartialEq<$rhs> for $lhs { + #[inline] + fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) } + #[inline] + fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) } + } + + impl<'a> PartialEq<$lhs> for $rhs { + #[inline] + fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&**self, &**other) } + #[inline] + fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&**self, &**other) } + } + + } +} + +impl_eq!(String, &'a str) +impl_eq!(CowString<'a>, String) + +impl<'a, 'b> PartialEq<&'b str> for CowString<'a> { + #[inline] + fn eq(&self, other: &&'b str) -> bool { PartialEq::eq(&**self, &**other) } + #[inline] + fn ne(&self, other: &&'b str) -> bool { PartialEq::ne(&**self, &**other) } +} + +impl<'a, 'b> PartialEq<CowString<'a>> for &'b str { + #[inline] + fn eq(&self, other: &CowString<'a>) -> bool { PartialEq::eq(&**self, &**other) } + #[inline] + fn ne(&self, other: &CowString<'a>) -> bool { PartialEq::ne(&**self, &**other) } +} + #[experimental = "waiting on Str stabilization"] impl Str for String { #[inline] diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 5edd3d0b780..8d98df8b200 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -535,14 +535,69 @@ impl<T> Extend<T> for Vec<T> { } } -#[unstable = "waiting on PartialEq stability"] -impl<T: PartialEq> PartialEq for Vec<T> { +impl<A, B> PartialEq<Vec<B>> for Vec<A> where A: PartialEq<B> { #[inline] - fn eq(&self, other: &Vec<T>) -> bool { - self.as_slice() == other.as_slice() + fn eq(&self, other: &Vec<B>) -> bool { PartialEq::eq(&**self, &**other) } + #[inline] + fn ne(&self, other: &Vec<B>) -> bool { PartialEq::ne(&**self, &**other) } +} + +macro_rules! impl_eq { + ($lhs:ty, $rhs:ty) => { + impl<'b, A, B> PartialEq<$rhs> for $lhs where A: PartialEq<B> { + #[inline] + fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) } + #[inline] + fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) } + } + + impl<'b, A, B> PartialEq<$lhs> for $rhs where B: PartialEq<A> { + #[inline] + fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&**self, &**other) } + #[inline] + fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&**self, &**other) } + } + } +} + +impl_eq!(Vec<A>, &'b [B]) +impl_eq!(Vec<A>, &'b mut [B]) + +impl<'a, A, B> PartialEq<Vec<B>> for CowVec<'a, A> where A: PartialEq<B> + Clone { + #[inline] + fn eq(&self, other: &Vec<B>) -> bool { PartialEq::eq(&**self, &**other) } + #[inline] + fn ne(&self, other: &Vec<B>) -> bool { PartialEq::ne(&**self, &**other) } +} + +impl<'a, A, B> PartialEq<CowVec<'a, A>> for Vec<B> where A: Clone, B: PartialEq<A> { + #[inline] + fn eq(&self, other: &CowVec<'a, A>) -> bool { PartialEq::eq(&**self, &**other) } + #[inline] + fn ne(&self, other: &CowVec<'a, A>) -> bool { PartialEq::ne(&**self, &**other) } +} + +macro_rules! impl_eq_for_cowvec { + ($rhs:ty) => { + impl<'a, 'b, A, B> PartialEq<$rhs> for CowVec<'a, A> where A: PartialEq<B> + Clone { + #[inline] + fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) } + #[inline] + fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) } + } + + impl<'a, 'b, A, B> PartialEq<CowVec<'a, A>> for $rhs where A: Clone, B: PartialEq<A> { + #[inline] + fn eq(&self, other: &CowVec<'a, A>) -> bool { PartialEq::eq(&**self, &**other) } + #[inline] + fn ne(&self, other: &CowVec<'a, A>) -> bool { PartialEq::ne(&**self, &**other) } + } } } +impl_eq_for_cowvec!(&'b [B]) +impl_eq_for_cowvec!(&'b mut [B]) + #[unstable = "waiting on PartialOrd stability"] impl<T: PartialOrd> PartialOrd for Vec<T> { #[inline] 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()) } diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 37df2bf14c2..1376f59d79f 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -629,6 +629,28 @@ impl<'a> Equiv<&'a str> for InternedString { } } +impl<'a> PartialEq<&'a str> for InternedString { + #[inline(always)] + fn eq(&self, other: & &'a str) -> bool { + PartialEq::eq(self.string.as_slice(), *other) + } + #[inline(always)] + fn ne(&self, other: & &'a str) -> bool { + PartialEq::ne(self.string.as_slice(), *other) + } +} + +impl<'a> PartialEq<InternedString > for &'a str { + #[inline(always)] + fn eq(&self, other: &InternedString) -> bool { + PartialEq::eq(*self, other.string.as_slice()) + } + #[inline(always)] + fn ne(&self, other: &InternedString) -> bool { + PartialEq::ne(*self, other.string.as_slice()) + } +} + impl<D:Decoder<E>, E> Decodable<D, E> for InternedString { fn decode(d: &mut D) -> Result<InternedString, E> { Ok(get_name(get_ident_interner().intern( |
