Skip to content

Commit 2840d58

Browse files
author
Jorge Aparicio
committed
Overload the == operator
- String == &str == CowString - Vec == &[T] == &mut [T] == [T, ..N] == CowVec - InternedString == &str
1 parent 2578de9 commit 2840d58

File tree

8 files changed

+187
-21
lines changed

8 files changed

+187
-21
lines changed

src/libcollections/string.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use str::{CharRange, CowString, FromStr, StrAllocating, Owned};
3030
use vec::{DerefVec, Vec, as_vec};
3131

3232
/// A growable string stored as a UTF-8 encoded buffer.
33-
#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord)]
33+
#[deriving(Clone, PartialOrd, Eq, Ord)]
3434
#[stable]
3535
pub struct String {
3636
vec: Vec<u8>,
@@ -738,6 +738,49 @@ impl Extend<char> for String {
738738
}
739739
}
740740

741+
impl PartialEq for String {
742+
#[inline]
743+
fn eq(&self, other: &String) -> bool { PartialEq::eq(&**self, &**other) }
744+
#[inline]
745+
fn ne(&self, other: &String) -> bool { PartialEq::ne(&**self, &**other) }
746+
}
747+
748+
macro_rules! impl_eq {
749+
($lhs:ty, $rhs: ty) => {
750+
impl<'a> PartialEq<$rhs> for $lhs {
751+
#[inline]
752+
fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) }
753+
#[inline]
754+
fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) }
755+
}
756+
757+
impl<'a> PartialEq<$lhs> for $rhs {
758+
#[inline]
759+
fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&**self, &**other) }
760+
#[inline]
761+
fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&**self, &**other) }
762+
}
763+
764+
}
765+
}
766+
767+
impl_eq!(String, &'a str)
768+
impl_eq!(CowString<'a>, String)
769+
770+
impl<'a, 'b> PartialEq<&'b str> for CowString<'a> {
771+
#[inline]
772+
fn eq(&self, other: &&'b str) -> bool { PartialEq::eq(&**self, &**other) }
773+
#[inline]
774+
fn ne(&self, other: &&'b str) -> bool { PartialEq::ne(&**self, &**other) }
775+
}
776+
777+
impl<'a, 'b> PartialEq<CowString<'a>> for &'b str {
778+
#[inline]
779+
fn eq(&self, other: &CowString<'a>) -> bool { PartialEq::eq(&**self, &**other) }
780+
#[inline]
781+
fn ne(&self, other: &CowString<'a>) -> bool { PartialEq::ne(&**self, &**other) }
782+
}
783+
741784
#[experimental = "waiting on Str stabilization"]
742785
impl Str for String {
743786
#[inline]

src/libcollections/vec.rs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,14 +535,69 @@ impl<T> Extend<T> for Vec<T> {
535535
}
536536
}
537537

538-
#[unstable = "waiting on PartialEq stability"]
539-
impl<T: PartialEq> PartialEq for Vec<T> {
538+
impl<A, B> PartialEq<Vec<B>> for Vec<A> where A: PartialEq<B> {
540539
#[inline]
541-
fn eq(&self, other: &Vec<T>) -> bool {
542-
self.as_slice() == other.as_slice()
540+
fn eq(&self, other: &Vec<B>) -> bool { PartialEq::eq(&**self, &**other) }
541+
#[inline]
542+
fn ne(&self, other: &Vec<B>) -> bool { PartialEq::ne(&**self, &**other) }
543+
}
544+
545+
macro_rules! impl_eq {
546+
($lhs:ty, $rhs:ty) => {
547+
impl<'b, A, B> PartialEq<$rhs> for $lhs where A: PartialEq<B> {
548+
#[inline]
549+
fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) }
550+
#[inline]
551+
fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) }
552+
}
553+
554+
impl<'b, A, B> PartialEq<$lhs> for $rhs where B: PartialEq<A> {
555+
#[inline]
556+
fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&**self, &**other) }
557+
#[inline]
558+
fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&**self, &**other) }
559+
}
560+
}
561+
}
562+
563+
impl_eq!(Vec<A>, &'b [B])
564+
impl_eq!(Vec<A>, &'b mut [B])
565+
566+
impl<'a, A, B> PartialEq<Vec<B>> for CowVec<'a, A> where A: PartialEq<B> + Clone {
567+
#[inline]
568+
fn eq(&self, other: &Vec<B>) -> bool { PartialEq::eq(&**self, &**other) }
569+
#[inline]
570+
fn ne(&self, other: &Vec<B>) -> bool { PartialEq::ne(&**self, &**other) }
571+
}
572+
573+
impl<'a, A, B> PartialEq<CowVec<'a, A>> for Vec<B> where A: Clone, B: PartialEq<A> {
574+
#[inline]
575+
fn eq(&self, other: &CowVec<'a, A>) -> bool { PartialEq::eq(&**self, &**other) }
576+
#[inline]
577+
fn ne(&self, other: &CowVec<'a, A>) -> bool { PartialEq::ne(&**self, &**other) }
578+
}
579+
580+
macro_rules! impl_eq_for_cowvec {
581+
($rhs:ty) => {
582+
impl<'a, 'b, A, B> PartialEq<$rhs> for CowVec<'a, A> where A: PartialEq<B> + Clone {
583+
#[inline]
584+
fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) }
585+
#[inline]
586+
fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) }
587+
}
588+
589+
impl<'a, 'b, A, B> PartialEq<CowVec<'a, A>> for $rhs where A: Clone, B: PartialEq<A> {
590+
#[inline]
591+
fn eq(&self, other: &CowVec<'a, A>) -> bool { PartialEq::eq(&**self, &**other) }
592+
#[inline]
593+
fn ne(&self, other: &CowVec<'a, A>) -> bool { PartialEq::ne(&**self, &**other) }
594+
}
543595
}
544596
}
545597

598+
impl_eq_for_cowvec!(&'b [B])
599+
impl_eq_for_cowvec!(&'b mut [B])
600+
546601
#[unstable = "waiting on PartialOrd stability"]
547602
impl<T: PartialOrd> PartialOrd for Vec<T> {
548603
#[inline]

src/libcore/array.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use clone::Clone;
1818
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
1919
use fmt;
2020
use kinds::Copy;
21+
use ops::Deref;
2122
use option::Option;
2223

2324
// macro for implementing n-ary tuple functions and operations
@@ -39,17 +40,37 @@ macro_rules! array_impls {
3940
}
4041

4142
#[unstable = "waiting for PartialEq to stabilize"]
42-
impl<T:PartialEq> PartialEq for [T, ..$N] {
43+
impl<A, B> PartialEq<[B, ..$N]> for [A, ..$N] where A: PartialEq<B> {
4344
#[inline]
44-
fn eq(&self, other: &[T, ..$N]) -> bool {
45+
fn eq(&self, other: &[B, ..$N]) -> bool {
4546
self[] == other[]
4647
}
4748
#[inline]
48-
fn ne(&self, other: &[T, ..$N]) -> bool {
49+
fn ne(&self, other: &[B, ..$N]) -> bool {
4950
self[] != other[]
5051
}
5152
}
5253

54+
impl<'a, A, B, Rhs> PartialEq<Rhs> for [A, ..$N] where
55+
A: PartialEq<B>,
56+
Rhs: Deref<[B]>,
57+
{
58+
#[inline(always)]
59+
fn eq(&self, other: &Rhs) -> bool { PartialEq::eq(self[], &**other) }
60+
#[inline(always)]
61+
fn ne(&self, other: &Rhs) -> bool { PartialEq::ne(self[], &**other) }
62+
}
63+
64+
impl<'a, A, B, Lhs> PartialEq<[B, ..$N]> for Lhs where
65+
A: PartialEq<B>,
66+
Lhs: Deref<[A]>
67+
{
68+
#[inline(always)]
69+
fn eq(&self, other: &[B, ..$N]) -> bool { PartialEq::eq(&**self, other[]) }
70+
#[inline(always)]
71+
fn ne(&self, other: &[B, ..$N]) -> bool { PartialEq::ne(&**self, other[]) }
72+
}
73+
5374
#[unstable = "waiting for Eq to stabilize"]
5475
impl<T:Eq> Eq for [T, ..$N] { }
5576

src/libcore/borrow.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,12 @@ impl<'a, T, Sized? B> Ord for Cow<'a, T, B> where B: Ord + ToOwned<T> {
196196
}
197197
}
198198

199-
impl<'a, T, Sized? B> PartialEq for Cow<'a, T, B> where B: PartialEq + ToOwned<T> {
199+
impl<'a, 'b, T, U, Sized? B, Sized? C> PartialEq<Cow<'b, U, C>> for Cow<'a, T, B> where
200+
B: PartialEq<C> + ToOwned<T>,
201+
C: ToOwned<U>,
202+
{
200203
#[inline]
201-
fn eq(&self, other: &Cow<'a, T, B>) -> bool {
204+
fn eq(&self, other: &Cow<'b, U, C>) -> bool {
202205
PartialEq::eq(&**self, &**other)
203206
}
204207
}

src/libcore/cmp.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,11 @@ mod impls {
400400
// & pointers
401401

402402
#[unstable = "Trait is unstable."]
403-
impl<'a, Sized? T: PartialEq> PartialEq for &'a T {
403+
impl<'a, 'b, Sized? A, Sized? B> PartialEq<&'b B> for &'a A where A: PartialEq<B> {
404404
#[inline]
405-
fn eq(&self, other: & &'a T) -> bool { PartialEq::eq(*self, *other) }
405+
fn eq(&self, other: & &'b B) -> bool { PartialEq::eq(*self, *other) }
406406
#[inline]
407-
fn ne(&self, other: & &'a T) -> bool { PartialEq::ne(*self, *other) }
407+
fn ne(&self, other: & &'b B) -> bool { PartialEq::ne(*self, *other) }
408408
}
409409
#[unstable = "Trait is unstable."]
410410
impl<'a, Sized? T: PartialOrd> PartialOrd for &'a T {
@@ -432,11 +432,11 @@ mod impls {
432432
// &mut pointers
433433

434434
#[unstable = "Trait is unstable."]
435-
impl<'a, Sized? T: PartialEq> PartialEq for &'a mut T {
435+
impl<'a, 'b, Sized? A, Sized? B> PartialEq<&'b mut B> for &'a mut A where A: PartialEq<B> {
436436
#[inline]
437-
fn eq(&self, other: &&'a mut T) -> bool { PartialEq::eq(*self, *other) }
437+
fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) }
438438
#[inline]
439-
fn ne(&self, other: &&'a mut T) -> bool { PartialEq::ne(*self, *other) }
439+
fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) }
440440
}
441441
#[unstable = "Trait is unstable."]
442442
impl<'a, Sized? T: PartialOrd> PartialOrd for &'a mut T {
@@ -460,4 +460,18 @@ mod impls {
460460
}
461461
#[unstable = "Trait is unstable."]
462462
impl<'a, Sized? T: Eq> Eq for &'a mut T {}
463+
464+
impl<'a, 'b, Sized? A, Sized? B> PartialEq<&'b mut B> for &'a A where A: PartialEq<B> {
465+
#[inline]
466+
fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) }
467+
#[inline]
468+
fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) }
469+
}
470+
471+
impl<'a, 'b, Sized? A, Sized? B> PartialEq<&'b B> for &'a mut A where A: PartialEq<B> {
472+
#[inline]
473+
fn eq(&self, other: &&'b B) -> bool { PartialEq::eq(*self, *other) }
474+
#[inline]
475+
fn ne(&self, other: &&'b B) -> bool { PartialEq::ne(*self, *other) }
476+
}
463477
}

src/libcore/iter.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,7 +2473,11 @@ pub mod order {
24732473
}
24742474

24752475
/// Compare `a` and `b` for equality (Using partial equality, `PartialEq`)
2476-
pub fn eq<A: PartialEq, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
2476+
pub fn eq<A, B, L, R>(mut a: L, mut b: R) -> bool where
2477+
A: PartialEq<B>,
2478+
L: Iterator<A>,
2479+
R: Iterator<B>,
2480+
{
24772481
loop {
24782482
match (a.next(), b.next()) {
24792483
(None, None) => return true,
@@ -2484,7 +2488,11 @@ pub mod order {
24842488
}
24852489

24862490
/// Compare `a` and `b` for nonequality (Using partial equality, `PartialEq`)
2487-
pub fn ne<A: PartialEq, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
2491+
pub fn ne<A, B, L, R>(mut a: L, mut b: R) -> bool where
2492+
A: PartialEq<B>,
2493+
L: Iterator<A>,
2494+
R: Iterator<B>,
2495+
{
24882496
loop {
24892497
match (a.next(), b.next()) {
24902498
(None, None) => return false,

src/libcore/slice.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,12 +1802,12 @@ pub mod bytes {
18021802
//
18031803

18041804
#[unstable = "waiting for DST"]
1805-
impl<T: PartialEq> PartialEq for [T] {
1806-
fn eq(&self, other: &[T]) -> bool {
1805+
impl<A, B> PartialEq<[B]> for [A] where A: PartialEq<B> {
1806+
fn eq(&self, other: &[B]) -> bool {
18071807
self.len() == other.len() &&
18081808
order::eq(self.iter(), other.iter())
18091809
}
1810-
fn ne(&self, other: &[T]) -> bool {
1810+
fn ne(&self, other: &[B]) -> bool {
18111811
self.len() != other.len() ||
18121812
order::ne(self.iter(), other.iter())
18131813
}

src/libsyntax/parse/token.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,28 @@ impl<'a> Equiv<&'a str> for InternedString {
629629
}
630630
}
631631

632+
impl<'a> PartialEq<&'a str> for InternedString {
633+
#[inline(always)]
634+
fn eq(&self, other: & &'a str) -> bool {
635+
PartialEq::eq(self.string.as_slice(), *other)
636+
}
637+
#[inline(always)]
638+
fn ne(&self, other: & &'a str) -> bool {
639+
PartialEq::ne(self.string.as_slice(), *other)
640+
}
641+
}
642+
643+
impl<'a> PartialEq<InternedString > for &'a str {
644+
#[inline(always)]
645+
fn eq(&self, other: &InternedString) -> bool {
646+
PartialEq::eq(*self, other.string.as_slice())
647+
}
648+
#[inline(always)]
649+
fn ne(&self, other: &InternedString) -> bool {
650+
PartialEq::ne(*self, other.string.as_slice())
651+
}
652+
}
653+
632654
impl<D:Decoder<E>, E> Decodable<D, E> for InternedString {
633655
fn decode(d: &mut D) -> Result<InternedString, E> {
634656
Ok(get_name(get_ident_interner().intern(

0 commit comments

Comments
 (0)