Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks to the blanket impls of PartialEq #23521

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 6 additions & 22 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cow<'a, str>> 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]
Expand Down
13 changes: 3 additions & 10 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1502,22 +1502,15 @@ impl<T> Extend<T> for Vec<T> {
}

__impl_slice_eq1! { Vec<A>, Vec<B> }
__impl_slice_eq2! { Vec<A>, &'b [B] }
__impl_slice_eq2! { Vec<A>, &'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<A>, [B] }
__impl_slice_eq2! { Cow<'a, [A]>, [B], Clone }
__impl_slice_eq2! { Cow<'a, [A]>, Vec<B>, Clone }

macro_rules! array_impls {
($($N: expr)+) => {
$(
// NOTE: some less important impls are omitted to reduce code bloat
__impl_slice_eq2! { Vec<A>, [B; $N] }
__impl_slice_eq2! { Vec<A>, &'b [B; $N] }
// __impl_slice_eq2! { Vec<A>, &'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 }
)+
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/libcore/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T:Eq> Eq for [T; $N] { }
Expand Down
39 changes: 39 additions & 0 deletions src/libcore/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<B> for &'a A where A: PartialEq<B>
{
#[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<B> for &'a mut A where A: PartialEq<B>
{
#[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<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) }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, 'b, A: ?Sized + NonRef, B: ?Sized> PartialEq<&'b mut B> for 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) }
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b B> for &'a A where A: PartialEq<B> {
#[inline]
Expand Down
16 changes: 8 additions & 8 deletions src/libsyntax/parse/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,25 +664,25 @@ impl fmt::Display for InternedString {
}
}

impl<'a> PartialEq<&'a str> for InternedString {
impl PartialEq<str> 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<InternedString > for &'a str {
impl PartialEq<InternedString > 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[..])
}
}

Expand Down