Skip to content

Commit 5e616db

Browse files
committed
Tweaks to equality comparisons for slices/arrays/vectors
1 parent 522d09d commit 5e616db

File tree

5 files changed

+86
-103
lines changed

5 files changed

+86
-103
lines changed

src/libcollections/vec.rs

+24-59
Original file line numberDiff line numberDiff line change
@@ -1501,69 +1501,34 @@ impl<T> Extend<T> for Vec<T> {
15011501
}
15021502
}
15031503

1504-
impl<A, B> PartialEq<Vec<B>> for Vec<A> where A: PartialEq<B> {
1505-
#[inline]
1506-
fn eq(&self, other: &Vec<B>) -> bool { PartialEq::eq(&**self, &**other) }
1507-
#[inline]
1508-
fn ne(&self, other: &Vec<B>) -> bool { PartialEq::ne(&**self, &**other) }
1509-
}
1510-
1511-
macro_rules! impl_eq {
1512-
($lhs:ty, $rhs:ty) => {
1513-
impl<'b, A, B> PartialEq<$rhs> for $lhs where A: PartialEq<B> {
1514-
#[inline]
1515-
fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) }
1516-
#[inline]
1517-
fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) }
1518-
}
1519-
1520-
impl<'b, A, B> PartialEq<$lhs> for $rhs where B: PartialEq<A> {
1521-
#[inline]
1522-
fn eq(&self, other: &$lhs) -> bool { PartialEq::eq(&**self, &**other) }
1523-
#[inline]
1524-
fn ne(&self, other: &$lhs) -> bool { PartialEq::ne(&**self, &**other) }
1525-
}
1504+
__impl_slice_eq1! { Vec<A>, Vec<B> }
1505+
__impl_slice_eq2! { Vec<A>, &'b [B] }
1506+
__impl_slice_eq2! { Vec<A>, &'b mut [B] }
1507+
__impl_slice_eq2! { CowVec<'a, A>, &'b [B], Clone }
1508+
__impl_slice_eq2! { CowVec<'a, A>, &'b mut [B], Clone }
1509+
__impl_slice_eq2! { CowVec<'a, A>, Vec<B>, Clone }
1510+
1511+
macro_rules! array_impls {
1512+
($($N: expr)+) => {
1513+
$(
1514+
// NOTE: some less important impls are omitted to reduce code bloat
1515+
__impl_slice_eq2! { Vec<A>, [B; $N] }
1516+
__impl_slice_eq2! { Vec<A>, &'b [B; $N] }
1517+
// __impl_slice_eq2! { Vec<A>, &'b mut [B; $N] }
1518+
// __impl_slice_eq2! { CowVec<'a, A>, [B; $N], Clone }
1519+
// __impl_slice_eq2! { CowVec<'a, A>, &'b [B; $N], Clone }
1520+
// __impl_slice_eq2! { CowVec<'a, A>, &'b mut [B; $N], Clone }
1521+
)+
15261522
}
15271523
}
15281524

1529-
impl_eq! { Vec<A>, &'b [B] }
1530-
impl_eq! { Vec<A>, &'b mut [B] }
1531-
1532-
impl<'a, A, B> PartialEq<Vec<B>> for Cow<'a, [A]> where A: PartialEq<B> + Clone {
1533-
#[inline]
1534-
fn eq(&self, other: &Vec<B>) -> bool { PartialEq::eq(&**self, &**other) }
1535-
#[inline]
1536-
fn ne(&self, other: &Vec<B>) -> bool { PartialEq::ne(&**self, &**other) }
1525+
array_impls! {
1526+
0 1 2 3 4 5 6 7 8 9
1527+
10 11 12 13 14 15 16 17 18 19
1528+
20 21 22 23 24 25 26 27 28 29
1529+
30 31 32
15371530
}
15381531

1539-
impl<'a, A, B> PartialEq<Cow<'a, [A]>> for Vec<B> where A: Clone, B: PartialEq<A> {
1540-
#[inline]
1541-
fn eq(&self, other: &Cow<'a, [A]>) -> bool { PartialEq::eq(&**self, &**other) }
1542-
#[inline]
1543-
fn ne(&self, other: &Cow<'a, [A]>) -> bool { PartialEq::ne(&**self, &**other) }
1544-
}
1545-
1546-
macro_rules! impl_eq_for_cowvec {
1547-
($rhs:ty) => {
1548-
impl<'a, 'b, A, B> PartialEq<$rhs> for Cow<'a, [A]> where A: PartialEq<B> + Clone {
1549-
#[inline]
1550-
fn eq(&self, other: &$rhs) -> bool { PartialEq::eq(&**self, &**other) }
1551-
#[inline]
1552-
fn ne(&self, other: &$rhs) -> bool { PartialEq::ne(&**self, &**other) }
1553-
}
1554-
1555-
impl<'a, 'b, A, B> PartialEq<Cow<'a, [A]>> for $rhs where A: Clone, B: PartialEq<A> {
1556-
#[inline]
1557-
fn eq(&self, other: &Cow<'a, [A]>) -> bool { PartialEq::eq(&**self, &**other) }
1558-
#[inline]
1559-
fn ne(&self, other: &Cow<'a, [A]>) -> bool { PartialEq::ne(&**self, &**other) }
1560-
}
1561-
}
1562-
}
1563-
1564-
impl_eq_for_cowvec! { &'b [B] }
1565-
impl_eq_for_cowvec! { &'b mut [B] }
1566-
15671532
#[stable(feature = "rust1", since = "1.0.0")]
15681533
impl<T: PartialOrd> PartialOrd for Vec<T> {
15691534
#[inline]
@@ -2480,7 +2445,7 @@ mod tests {
24802445
fn test_into_boxed_slice() {
24812446
let xs = vec![1, 2, 3];
24822447
let ys = xs.into_boxed_slice();
2483-
assert_eq!(ys, [1, 2, 3]);
2448+
assert_eq!(&*ys, [1, 2, 3]);
24842449
}
24852450

24862451
#[test]

src/libcore/array.rs

+8-43
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
1919
use fmt;
2020
use hash::{Hash, self};
2121
use iter::IntoIterator;
22-
use marker::Copy;
23-
use ops::Deref;
22+
use marker::{Copy, Sized};
2423
use option::Option;
2524
use slice::{Iter, IterMut, SliceExt};
2625

@@ -76,47 +75,13 @@ macro_rules! array_impls {
7675
}
7776
}
7877

79-
#[stable(feature = "rust1", since = "1.0.0")]
80-
impl<A, B> PartialEq<[B; $N]> for [A; $N] where A: PartialEq<B> {
81-
#[inline]
82-
fn eq(&self, other: &[B; $N]) -> bool {
83-
&self[..] == &other[..]
84-
}
85-
#[inline]
86-
fn ne(&self, other: &[B; $N]) -> bool {
87-
&self[..] != &other[..]
88-
}
89-
}
90-
91-
#[stable(feature = "rust1", since = "1.0.0")]
92-
impl<'a, A, B, Rhs> PartialEq<Rhs> for [A; $N] where
93-
A: PartialEq<B>,
94-
Rhs: Deref<Target=[B]>,
95-
{
96-
#[inline(always)]
97-
fn eq(&self, other: &Rhs) -> bool {
98-
PartialEq::eq(&self[..], &**other)
99-
}
100-
#[inline(always)]
101-
fn ne(&self, other: &Rhs) -> bool {
102-
PartialEq::ne(&self[..], &**other)
103-
}
104-
}
105-
106-
#[stable(feature = "rust1", since = "1.0.0")]
107-
impl<'a, A, B, Lhs> PartialEq<[B; $N]> for Lhs where
108-
A: PartialEq<B>,
109-
Lhs: Deref<Target=[A]>
110-
{
111-
#[inline(always)]
112-
fn eq(&self, other: &[B; $N]) -> bool {
113-
PartialEq::eq(&**self, &other[..])
114-
}
115-
#[inline(always)]
116-
fn ne(&self, other: &[B; $N]) -> bool {
117-
PartialEq::ne(&**self, &other[..])
118-
}
119-
}
78+
// NOTE: some less important impls are omitted to reduce code bloat
79+
__impl_slice_eq1! { [A; $N], [B; $N] }
80+
__impl_slice_eq2! { [A; $N], [B] }
81+
__impl_slice_eq2! { [A; $N], &'b [B] }
82+
__impl_slice_eq2! { [A; $N], &'b mut [B] }
83+
// __impl_slice_eq2! { [A; $N], &'b [B; $N] }
84+
// __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
12085

12186
#[stable(feature = "rust1", since = "1.0.0")]
12287
impl<T:Eq> Eq for [T; $N] { }

src/libcore/cmp_macros.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Utility macros for implementing PartialEq on slice-like types
12+
13+
#![doc(hidden)]
14+
15+
#[macro_export]
16+
macro_rules! __impl_slice_eq1 {
17+
($Lhs: ty, $Rhs: ty) => {
18+
#[stable(feature = "rust1", since = "1.0.0")]
19+
impl<'a, 'b, A, B> PartialEq<$Rhs> for $Lhs where A: PartialEq<B> {
20+
#[inline]
21+
fn eq(&self, other: &$Rhs) -> bool { &self[..] == &other[..] }
22+
#[inline]
23+
fn ne(&self, other: &$Rhs) -> bool { &self[..] != &other[..] }
24+
}
25+
}
26+
}
27+
28+
#[macro_export]
29+
macro_rules! __impl_slice_eq2 {
30+
($Lhs: ty, $Rhs: ty) => {
31+
__impl_slice_eq2! { $Lhs, $Rhs, Sized }
32+
};
33+
($Lhs: ty, $Rhs: ty, $Bound: ident) => {
34+
#[stable(feature = "rust1", since = "1.0.0")]
35+
impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs where A: PartialEq<B> {
36+
#[inline]
37+
fn eq(&self, other: &$Rhs) -> bool { &self[..] == &other[..] }
38+
#[inline]
39+
fn ne(&self, other: &$Rhs) -> bool { &self[..] != &other[..] }
40+
}
41+
42+
#[stable(feature = "rust1", since = "1.0.0")]
43+
impl<'a, 'b, A: $Bound, B> PartialEq<$Lhs> for $Rhs where B: PartialEq<A> {
44+
#[inline]
45+
fn eq(&self, other: &$Lhs) -> bool { &self[..] == &other[..] }
46+
#[inline]
47+
fn ne(&self, other: &$Lhs) -> bool { &self[..] != &other[..] }
48+
}
49+
}
50+
}

src/libcore/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
#[macro_use]
7373
mod macros;
7474

75+
#[macro_use]
76+
mod cmp_macros;
77+
7578
#[path = "num/float_macros.rs"]
7679
#[macro_use]
7780
mod float_macros;

src/libstd/ffi/c_str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ impl CStr {
371371

372372
impl PartialEq for CStr {
373373
fn eq(&self, other: &CStr) -> bool {
374-
self.to_bytes().eq(&other.to_bytes())
374+
self.to_bytes().eq(other.to_bytes())
375375
}
376376
}
377377
impl Eq for CStr {}

0 commit comments

Comments
 (0)