Skip to content

Commit e84e7a0

Browse files
committed
auto merge of #18467 : japaric/rust/eq, r=alexcrichton
`eq`, `ne`, `cmp`, etc methods now require one less level of indirection when dealing with `&str`/`&[T]` ``` rust "foo".ne(&"bar") -> "foo".ne("bar") slice.cmp(&another_slice) -> slice.cmp(another_slice) // slice and another_slice have type `&[T]` ``` [breaking-change]
2 parents 0e2f9b9 + 11f4bae commit e84e7a0

File tree

22 files changed

+635
-29
lines changed

22 files changed

+635
-29
lines changed

src/liballoc/boxed.rs

+40
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,16 @@ impl<T: Clone> Clone for Box<T> {
6161
}
6262
}
6363

64+
// NOTE(stage0): remove impl after a snapshot
65+
#[cfg(stage0)]
6466
impl<T:PartialEq> PartialEq for Box<T> {
6567
#[inline]
6668
fn eq(&self, other: &Box<T>) -> bool { *(*self) == *(*other) }
6769
#[inline]
6870
fn ne(&self, other: &Box<T>) -> bool { *(*self) != *(*other) }
6971
}
72+
// NOTE(stage0): remove impl after a snapshot
73+
#[cfg(stage0)]
7074
impl<T:PartialOrd> PartialOrd for Box<T> {
7175
#[inline]
7276
fn partial_cmp(&self, other: &Box<T>) -> Option<Ordering> {
@@ -81,14 +85,50 @@ impl<T:PartialOrd> PartialOrd for Box<T> {
8185
#[inline]
8286
fn gt(&self, other: &Box<T>) -> bool { *(*self) > *(*other) }
8387
}
88+
// NOTE(stage0): remove impl after a snapshot
89+
#[cfg(stage0)]
8490
impl<T: Ord> Ord for Box<T> {
8591
#[inline]
8692
fn cmp(&self, other: &Box<T>) -> Ordering {
8793
(**self).cmp(&**other)
8894
}
8995
}
96+
// NOTE(stage0): remove impl after a snapshot
97+
#[cfg(stage0)]
9098
impl<T: Eq> Eq for Box<T> {}
9199

100+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
101+
impl<Sized? T: PartialEq> PartialEq for Box<T> {
102+
#[inline]
103+
fn eq(&self, other: &Box<T>) -> bool { PartialEq::eq(&**self, &**other) }
104+
#[inline]
105+
fn ne(&self, other: &Box<T>) -> bool { PartialEq::ne(&**self, &**other) }
106+
}
107+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
108+
impl<Sized? T: PartialOrd> PartialOrd for Box<T> {
109+
#[inline]
110+
fn partial_cmp(&self, other: &Box<T>) -> Option<Ordering> {
111+
PartialOrd::partial_cmp(&**self, &**other)
112+
}
113+
#[inline]
114+
fn lt(&self, other: &Box<T>) -> bool { PartialOrd::lt(&**self, &**other) }
115+
#[inline]
116+
fn le(&self, other: &Box<T>) -> bool { PartialOrd::le(&**self, &**other) }
117+
#[inline]
118+
fn ge(&self, other: &Box<T>) -> bool { PartialOrd::ge(&**self, &**other) }
119+
#[inline]
120+
fn gt(&self, other: &Box<T>) -> bool { PartialOrd::gt(&**self, &**other) }
121+
}
122+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
123+
impl<Sized? T: Ord> Ord for Box<T> {
124+
#[inline]
125+
fn cmp(&self, other: &Box<T>) -> Ordering {
126+
Ord::cmp(&**self, &**other)
127+
}
128+
}
129+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
130+
impl<Sized? T: Eq> Eq for Box<T> {}
131+
92132
/// Extension methods for an owning `Any` trait object.
93133
#[unstable = "post-DST and coherence changes, this will not be a trait but \
94134
rather a direct `impl` on `Box<Any>`"]

src/libcollections/slice.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1598,15 +1598,15 @@ mod tests {
15981598
#[test]
15991599
fn test_total_ord() {
16001600
let c: &[int] = &[1, 2, 3];
1601-
[1, 2, 3, 4][].cmp(& c) == Greater;
1601+
[1, 2, 3, 4][].cmp(c) == Greater;
16021602
let c: &[int] = &[1, 2, 3, 4];
1603-
[1, 2, 3][].cmp(& c) == Less;
1603+
[1, 2, 3][].cmp(c) == Less;
16041604
let c: &[int] = &[1, 2, 3, 6];
1605-
[1, 2, 3, 4][].cmp(& c) == Equal;
1605+
[1, 2, 3, 4][].cmp(c) == Equal;
16061606
let c: &[int] = &[1, 2, 3, 4, 5, 6];
1607-
[1, 2, 3, 4, 5, 5, 5, 5][].cmp(& c) == Less;
1607+
[1, 2, 3, 4, 5, 5, 5, 5][].cmp(c) == Less;
16081608
let c: &[int] = &[1, 2, 3, 4];
1609-
[2, 2][].cmp(& c) == Greater;
1609+
[2, 2][].cmp(c) == Greater;
16101610
}
16111611

16121612
#[test]

src/libcollections/str.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,17 @@ impl<'a> PartialOrd for MaybeOwned<'a> {
532532
}
533533

534534
impl<'a> Ord for MaybeOwned<'a> {
535+
// NOTE(stage0): remove method after a snapshot
536+
#[cfg(stage0)]
535537
#[inline]
536538
fn cmp(&self, other: &MaybeOwned) -> Ordering {
537539
self.as_slice().cmp(&other.as_slice())
538540
}
541+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
542+
#[inline]
543+
fn cmp(&self, other: &MaybeOwned) -> Ordering {
544+
self.as_slice().cmp(other.as_slice())
545+
}
539546
}
540547

541548
impl<'a, S: Str> Equiv<S> for MaybeOwned<'a> {
@@ -1523,11 +1530,11 @@ mod tests {
15231530

15241531
#[test]
15251532
fn test_total_ord() {
1526-
"1234".cmp(&("123")) == Greater;
1527-
"123".cmp(&("1234")) == Less;
1528-
"1234".cmp(&("1234")) == Equal;
1529-
"12345555".cmp(&("123456")) == Less;
1530-
"22".cmp(&("1234")) == Greater;
1533+
"1234".cmp("123") == Greater;
1534+
"123".cmp("1234") == Less;
1535+
"1234".cmp("1234") == Equal;
1536+
"12345555".cmp("123456") == Less;
1537+
"22".cmp("1234") == Greater;
15311538
}
15321539

15331540
#[test]

src/libcollections/tree/map.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ impl<K, V> TreeMap<K, V> {
579579
/// let headers = get_headers();
580580
/// let ua_key = "User-Agent";
581581
/// let ua = headers.find_with(|k| {
582-
/// ua_key.cmp(&k.as_slice())
582+
/// ua_key.cmp(k.as_slice())
583583
/// });
584584
///
585585
/// assert_eq!((*ua.unwrap()).as_slice(), "Curl-Rust/0.1");
@@ -603,7 +603,7 @@ impl<K, V> TreeMap<K, V> {
603603
/// t.insert("User-Agent", "Curl-Rust/0.1");
604604
///
605605
/// let new_ua = "Safari/156.0";
606-
/// match t.find_with_mut(|k| "User-Agent".cmp(k)) {
606+
/// match t.find_with_mut(|&k| "User-Agent".cmp(k)) {
607607
/// Some(x) => *x = new_ua,
608608
/// None => panic!(),
609609
/// }
@@ -1302,7 +1302,7 @@ mod test_treemap {
13021302
#[test]
13031303
fn find_with_empty() {
13041304
let m: TreeMap<&'static str,int> = TreeMap::new();
1305-
assert!(m.find_with(|k| "test".cmp(k)) == None);
1305+
assert!(m.find_with(|&k| "test".cmp(k)) == None);
13061306
}
13071307

13081308
#[test]
@@ -1311,7 +1311,7 @@ mod test_treemap {
13111311
assert!(m.insert("test1", 2i));
13121312
assert!(m.insert("test2", 3i));
13131313
assert!(m.insert("test3", 3i));
1314-
assert_eq!(m.find_with(|k| "test4".cmp(k)), None);
1314+
assert_eq!(m.find_with(|&k| "test4".cmp(k)), None);
13151315
}
13161316

13171317
#[test]
@@ -1320,7 +1320,7 @@ mod test_treemap {
13201320
assert!(m.insert("test1", 2i));
13211321
assert!(m.insert("test2", 3i));
13221322
assert!(m.insert("test3", 4i));
1323-
assert_eq!(m.find_with(|k| "test2".cmp(k)), Some(&3i));
1323+
assert_eq!(m.find_with(|&k| "test2".cmp(k)), Some(&3i));
13241324
}
13251325

13261326
#[test]
@@ -1343,10 +1343,10 @@ mod test_treemap {
13431343
assert!(m.insert("t2", 8));
13441344
assert!(m.insert("t5", 14));
13451345
let new = 100;
1346-
match m.find_with_mut(|k| "t5".cmp(k)) {
1346+
match m.find_with_mut(|&k| "t5".cmp(k)) {
13471347
None => panic!(), Some(x) => *x = new
13481348
}
1349-
assert_eq!(m.find_with(|k| "t5".cmp(k)), Some(&new));
1349+
assert_eq!(m.find_with(|&k| "t5".cmp(k)), Some(&new));
13501350
}
13511351

13521352
#[test]

src/libcollections/vec.rs

+14
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,17 @@ impl<T: PartialEq> PartialEq for Vec<T> {
506506

507507
#[unstable = "waiting on PartialOrd stability"]
508508
impl<T: PartialOrd> PartialOrd for Vec<T> {
509+
// NOTE(stage0): remove method after a snapshot
510+
#[cfg(stage0)]
509511
#[inline]
510512
fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering> {
511513
self.as_slice().partial_cmp(&other.as_slice())
512514
}
515+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
516+
#[inline]
517+
fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering> {
518+
self.as_slice().partial_cmp(other.as_slice())
519+
}
513520
}
514521

515522
#[unstable = "waiting on Eq stability"]
@@ -523,10 +530,17 @@ impl<T: PartialEq, V: AsSlice<T>> Equiv<V> for Vec<T> {
523530

524531
#[unstable = "waiting on Ord stability"]
525532
impl<T: Ord> Ord for Vec<T> {
533+
// NOTE(stage0): remove method after a snapshot
534+
#[cfg(stage0)]
526535
#[inline]
527536
fn cmp(&self, other: &Vec<T>) -> Ordering {
528537
self.as_slice().cmp(&other.as_slice())
529538
}
539+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
540+
#[inline]
541+
fn cmp(&self, other: &Vec<T>) -> Ordering {
542+
self.as_slice().cmp(other.as_slice())
543+
}
530544
}
531545

532546
// FIXME: #13996: need a way to mark the return value as `noalias`

0 commit comments

Comments
 (0)