From ce9bc13cd3c9ead97cc8268571e3480929c00ff3 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 19 Mar 2015 21:54:23 +0300 Subject: [PATCH] Tweaks to the blanket impls of PartialEq --- src/libcollections/string.rs | 28 ++++++-------------------- src/libcollections/vec.rs | 13 +++--------- src/libcore/array.rs | 5 ----- src/libcore/cmp.rs | 39 ++++++++++++++++++++++++++++++++++++ src/libsyntax/parse/token.rs | 16 +++++++-------- 5 files changed, 56 insertions(+), 45 deletions(-) diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index cd6f27bf65f6e..a75562435a890 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -778,41 +778,25 @@ macro_rules! impl_eq { #[stable(feature = "rust1", since = "1.0.0")] impl<'a> PartialEq<$rhs> for $lhs { #[inline] - fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) } + fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&self[..], &other[..]) } #[inline] - fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) } + fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&self[..], &other[..]) } } #[stable(feature = "rust1", since = "1.0.0")] impl<'a> PartialEq<$lhs> for $rhs { #[inline] - fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&**self, &**other) } + fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&self[..], &other[..]) } #[inline] - fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&**self, &**other) } + fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&self[..], &other[..]) } } - } } -impl_eq! { String, &'a str } +impl_eq! { String, str } +impl_eq! { Cow<'a, str>, str } impl_eq! { Cow<'a, str>, String } -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, 'b> PartialEq<&'b str> for Cow<'a, str> { - #[inline] - fn eq(&self, other: &&'b str) -> bool { PartialEq::eq(&**self, &**other) } - #[inline] - fn ne(&self, other: &&'b str) -> bool { PartialEq::ne(&**self, &**other) } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, 'b> PartialEq> for &'b str { - #[inline] - fn eq(&self, other: &Cow<'a, str>) -> bool { PartialEq::eq(&**self, &**other) } - #[inline] - fn ne(&self, other: &Cow<'a, str>) -> bool { PartialEq::ne(&**self, &**other) } -} - #[unstable(feature = "collections", reason = "waiting on Str stabilization")] impl Str for String { #[inline] diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index b0e8dc7d0b622..a70a53ec538b0 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1502,22 +1502,15 @@ impl Extend for Vec { } __impl_slice_eq1! { Vec, Vec } -__impl_slice_eq2! { Vec, &'b [B] } -__impl_slice_eq2! { Vec, &'b mut [B] } -__impl_slice_eq2! { Cow<'a, [A]>, &'b [B], Clone } -__impl_slice_eq2! { Cow<'a, [A]>, &'b mut [B], Clone } +__impl_slice_eq2! { Vec, [B] } +__impl_slice_eq2! { Cow<'a, [A]>, [B], Clone } __impl_slice_eq2! { Cow<'a, [A]>, Vec, Clone } macro_rules! array_impls { ($($N: expr)+) => { $( - // NOTE: some less important impls are omitted to reduce code bloat __impl_slice_eq2! { Vec, [B; $N] } - __impl_slice_eq2! { Vec, &'b [B; $N] } - // __impl_slice_eq2! { Vec, &'b mut [B; $N] } - // __impl_slice_eq2! { Cow<'a, [A]>, [B; $N], Clone } - // __impl_slice_eq2! { Cow<'a, [A]>, &'b [B; $N], Clone } - // __impl_slice_eq2! { Cow<'a, [A]>, &'b mut [B; $N], Clone } + __impl_slice_eq2! { Cow<'a, [A]>, [B; $N], Clone } )+ } } diff --git a/src/libcore/array.rs b/src/libcore/array.rs index edb11df54894c..420aa1ceda27b 100644 --- a/src/libcore/array.rs +++ b/src/libcore/array.rs @@ -92,13 +92,8 @@ macro_rules! array_impls { } } - // NOTE: some less important impls are omitted to reduce code bloat __impl_slice_eq1! { [A; $N], [B; $N] } __impl_slice_eq2! { [A; $N], [B] } - __impl_slice_eq2! { [A; $N], &'b [B] } - __impl_slice_eq2! { [A; $N], &'b mut [B] } - // __impl_slice_eq2! { [A; $N], &'b [B; $N] } - // __impl_slice_eq2! { [A; $N], &'b mut [B; $N] } #[stable(feature = "rust1", since = "1.0.0")] impl Eq for [T; $N] { } diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index b37bad5f7546c..ba5d75e2a01d2 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -569,6 +569,45 @@ mod impls { // & pointers + /// A trait implemented for everything except for `&/&mut` references + trait NonRef: ::marker::MarkerTrait {} + impl NonRef for .. {} + impl<'a, T> !NonRef for &'a T {} + impl<'a, T> !NonRef for &'a mut T {} + + // NOTE: The numerous impls below can be compressed with a macro + #[stable(feature = "rust1", since = "1.0.0")] + impl<'a, 'b, A: ?Sized, B: ?Sized + NonRef> PartialEq for &'a A where A: PartialEq + { + #[inline] + fn eq(&self, other: &B) -> bool { PartialEq::eq(*self, other) } + #[inline] + fn ne(&self, other: &B) -> bool { PartialEq::ne(*self, other) } + } + #[stable(feature = "rust1", since = "1.0.0")] + impl<'a, 'b, A: ?Sized, B: ?Sized + NonRef> PartialEq for &'a mut A where A: PartialEq + { + #[inline] + fn eq(&self, other: &B) -> bool { PartialEq::eq(*self, other) } + #[inline] + fn ne(&self, other: &B) -> bool { PartialEq::ne(*self, other) } + } + #[stable(feature = "rust1", since = "1.0.0")] + impl<'a, 'b, A: ?Sized + NonRef, B: ?Sized> PartialEq<&'b B> for A where A: PartialEq + { + #[inline] + fn eq(&self, other: &&'b B) -> bool { PartialEq::eq(self, *other) } + #[inline] + fn ne(&self, other: &&'b B) -> bool { PartialEq::ne(self, *other) } + } + #[stable(feature = "rust1", since = "1.0.0")] + impl<'a, 'b, A: ?Sized + NonRef, B: ?Sized> PartialEq<&'b mut B> for A where A: PartialEq + { + #[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) } + } #[stable(feature = "rust1", since = "1.0.0")] impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b B> for &'a A where A: PartialEq { #[inline] diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 4db85eeea46f1..6da7f594c1fdc 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -664,25 +664,25 @@ impl fmt::Display for InternedString { } } -impl<'a> PartialEq<&'a str> for InternedString { +impl PartialEq for InternedString { #[inline(always)] - fn eq(&self, other: & &'a str) -> bool { - PartialEq::eq(&self.string[..], *other) + fn eq(&self, other: &str) -> bool { + PartialEq::eq(&self.string[..], other) } #[inline(always)] - fn ne(&self, other: & &'a str) -> bool { - PartialEq::ne(&self.string[..], *other) + fn ne(&self, other: &str) -> bool { + PartialEq::ne(&self.string[..], other) } } -impl<'a> PartialEq for &'a str { +impl PartialEq for str { #[inline(always)] fn eq(&self, other: &InternedString) -> bool { - PartialEq::eq(*self, &other.string[..]) + PartialEq::eq(self, &other.string[..]) } #[inline(always)] fn ne(&self, other: &InternedString) -> bool { - PartialEq::ne(*self, &other.string[..]) + PartialEq::ne(self, &other.string[..]) } }