Skip to content

Overloaded comparison traits #19167

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

Merged
merged 7 commits into from
Dec 4, 2014
Merged
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
4 changes: 2 additions & 2 deletions src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,15 +945,15 @@ mod tests {
let mut m = list_from(v.as_slice());
m.rotate_backward(); check_links(&m);
m.rotate_forward(); check_links(&m);
assert_eq!(v.iter().collect::<Vec<&int>>(), m.iter().collect());
assert_eq!(v.iter().collect::<Vec<&int>>(), m.iter().collect::<Vec<_>>());
m.rotate_forward(); check_links(&m);
m.rotate_forward(); check_links(&m);
m.pop_front(); check_links(&m);
m.rotate_forward(); check_links(&m);
m.rotate_backward(); check_links(&m);
m.push_front(9); check_links(&m);
m.rotate_forward(); check_links(&m);
assert_eq!(vec![3i,9,5,1,2], m.into_iter().collect());
assert_eq!(vec![3i,9,5,1,2], m.into_iter().collect::<Vec<_>>());
}

#[test]
Expand Down
24 changes: 12 additions & 12 deletions src/libcollections/enum_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,23 +397,23 @@ mod test {
fn test_iterator() {
let mut e1: EnumSet<Foo> = EnumSet::new();

let elems: Vec<Foo> = e1.iter().collect();
let elems: ::vec::Vec<Foo> = e1.iter().collect();
assert!(elems.is_empty())

e1.insert(A);
let elems = e1.iter().collect();
let elems: ::vec::Vec<_> = e1.iter().collect();
assert_eq!(vec![A], elems)

e1.insert(C);
let elems = e1.iter().collect();
let elems: ::vec::Vec<_> = e1.iter().collect();
assert_eq!(vec![A,C], elems)

e1.insert(C);
let elems = e1.iter().collect();
let elems: ::vec::Vec<_> = e1.iter().collect();
assert_eq!(vec![A,C], elems)

e1.insert(B);
let elems = e1.iter().collect();
let elems: ::vec::Vec<_> = e1.iter().collect();
assert_eq!(vec![A,B,C], elems)
}

Expand All @@ -431,35 +431,35 @@ mod test {
e2.insert(C);

let e_union = e1 | e2;
let elems = e_union.iter().collect();
let elems: ::vec::Vec<_> = e_union.iter().collect();
assert_eq!(vec![A,B,C], elems)

let e_intersection = e1 & e2;
let elems = e_intersection.iter().collect();
let elems: ::vec::Vec<_> = e_intersection.iter().collect();
assert_eq!(vec![C], elems)

// Another way to express intersection
let e_intersection = e1 - (e1 - e2);
let elems = e_intersection.iter().collect();
let elems: ::vec::Vec<_> = e_intersection.iter().collect();
assert_eq!(vec![C], elems)

let e_subtract = e1 - e2;
let elems = e_subtract.iter().collect();
let elems: ::vec::Vec<_> = e_subtract.iter().collect();
assert_eq!(vec![A], elems)

// Bitwise XOR of two sets, aka symmetric difference
let e_symmetric_diff = e1 ^ e2;
let elems = e_symmetric_diff.iter().collect();
let elems: ::vec::Vec<_> = e_symmetric_diff.iter().collect();
assert_eq!(vec![A,B], elems)

// Another way to express symmetric difference
let e_symmetric_diff = (e1 - e2) | (e2 - e1);
let elems = e_symmetric_diff.iter().collect();
let elems: ::vec::Vec<_> = e_symmetric_diff.iter().collect();
assert_eq!(vec![A,B], elems)

// Yet another way to express symmetric difference
let e_symmetric_diff = (e1 | e2) - (e1 & e2);
let elems = e_symmetric_diff.iter().collect();
let elems: ::vec::Vec<_> = e_symmetric_diff.iter().collect();
assert_eq!(vec![A,B], elems)
}

Expand Down
1 change: 1 addition & 0 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ impl<'a> Ord for MaybeOwned<'a> {
}
}

#[allow(deprecated)]
#[deprecated = "use std::str::CowString"]
impl<'a, S: Str> Equiv<S> for MaybeOwned<'a> {
#[inline]
Expand Down
54 changes: 50 additions & 4 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -779,7 +822,8 @@ impl<H: hash::Writer> hash::Hash<H> for String {
}
}

#[experimental = "waiting on Equiv stabilization"]
#[allow(deprecated)]
#[deprecated = "Use overloaded `core::cmp::PartialEq`"]
impl<'a, S: Str> Equiv<S> for String {
#[inline]
fn equiv(&self, other: &S) -> bool {
Expand Down Expand Up @@ -967,10 +1011,12 @@ mod tests {
#[test]
fn test_from_utf8_lossy() {
let xs = b"hello";
assert_eq!(String::from_utf8_lossy(xs), "hello".into_cow());
let ys: str::CowString = "hello".into_cow();
assert_eq!(String::from_utf8_lossy(xs), ys);

let xs = "ศไทย中华Việt Nam".as_bytes();
assert_eq!(String::from_utf8_lossy(xs), "ศไทย中华Việt Nam".into_cow());
let ys: str::CowString = "ศไทย中华Việt Nam".into_cow();
assert_eq!(String::from_utf8_lossy(xs), ys);

let xs = b"Hello\xC2 There\xFF Goodbye";
assert_eq!(String::from_utf8_lossy(xs),
Expand Down
74 changes: 65 additions & 9 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -554,7 +609,8 @@ impl<T: PartialOrd> PartialOrd for Vec<T> {
#[unstable = "waiting on Eq stability"]
impl<T: Eq> Eq for Vec<T> {}

#[experimental]
#[allow(deprecated)]
#[deprecated = "Use overloaded `core::cmp::PartialEq`"]
impl<T: PartialEq, Sized? V: AsSlice<T>> Equiv<V> for Vec<T> {
#[inline]
fn equiv(&self, other: &V) -> bool { self.as_slice() == other.as_slice() }
Expand Down Expand Up @@ -1813,27 +1869,27 @@ mod tests {
let mut values = vec![1u8,2,3,4,5];
{
let slice = values.slice_from_mut(2);
assert!(slice == &mut [3, 4, 5]);
assert!(slice == [3, 4, 5]);
for p in slice.iter_mut() {
*p += 2;
}
}

assert!(values.as_slice() == &[1, 2, 5, 6, 7]);
assert!(values.as_slice() == [1, 2, 5, 6, 7]);
}

#[test]
fn test_slice_to_mut() {
let mut values = vec![1u8,2,3,4,5];
{
let slice = values.slice_to_mut(2);
assert!(slice == &mut [1, 2]);
assert!(slice == [1, 2]);
for p in slice.iter_mut() {
*p += 1;
}
}

assert!(values.as_slice() == &[2, 3, 3, 4, 5]);
assert!(values.as_slice() == [2, 3, 3, 4, 5]);
}

#[test]
Expand Down
27 changes: 24 additions & 3 deletions src/libcore/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] { }

Expand Down
7 changes: 5 additions & 2 deletions src/libcore/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Loading