Skip to content

Commit

Permalink
auto merge of #18467 : japaric/rust/eq, r=alexcrichton
Browse files Browse the repository at this point in the history
`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]
  • Loading branch information
bors committed Nov 6, 2014
2 parents 0e2f9b9 + 11f4bae commit e84e7a0
Show file tree
Hide file tree
Showing 22 changed files with 635 additions and 29 deletions.
40 changes: 40 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ impl<T: Clone> Clone for Box<T> {
}
}

// NOTE(stage0): remove impl after a snapshot
#[cfg(stage0)]
impl<T:PartialEq> PartialEq for Box<T> {
#[inline]
fn eq(&self, other: &Box<T>) -> bool { *(*self) == *(*other) }
#[inline]
fn ne(&self, other: &Box<T>) -> bool { *(*self) != *(*other) }
}
// NOTE(stage0): remove impl after a snapshot
#[cfg(stage0)]
impl<T:PartialOrd> PartialOrd for Box<T> {
#[inline]
fn partial_cmp(&self, other: &Box<T>) -> Option<Ordering> {
Expand All @@ -81,14 +85,50 @@ impl<T:PartialOrd> PartialOrd for Box<T> {
#[inline]
fn gt(&self, other: &Box<T>) -> bool { *(*self) > *(*other) }
}
// NOTE(stage0): remove impl after a snapshot
#[cfg(stage0)]
impl<T: Ord> Ord for Box<T> {
#[inline]
fn cmp(&self, other: &Box<T>) -> Ordering {
(**self).cmp(&**other)
}
}
// NOTE(stage0): remove impl after a snapshot
#[cfg(stage0)]
impl<T: Eq> Eq for Box<T> {}

#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
impl<Sized? T: PartialEq> PartialEq for Box<T> {
#[inline]
fn eq(&self, other: &Box<T>) -> bool { PartialEq::eq(&**self, &**other) }
#[inline]
fn ne(&self, other: &Box<T>) -> bool { PartialEq::ne(&**self, &**other) }
}
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
impl<Sized? T: PartialOrd> PartialOrd for Box<T> {
#[inline]
fn partial_cmp(&self, other: &Box<T>) -> Option<Ordering> {
PartialOrd::partial_cmp(&**self, &**other)
}
#[inline]
fn lt(&self, other: &Box<T>) -> bool { PartialOrd::lt(&**self, &**other) }
#[inline]
fn le(&self, other: &Box<T>) -> bool { PartialOrd::le(&**self, &**other) }
#[inline]
fn ge(&self, other: &Box<T>) -> bool { PartialOrd::ge(&**self, &**other) }
#[inline]
fn gt(&self, other: &Box<T>) -> bool { PartialOrd::gt(&**self, &**other) }
}
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
impl<Sized? T: Ord> Ord for Box<T> {
#[inline]
fn cmp(&self, other: &Box<T>) -> Ordering {
Ord::cmp(&**self, &**other)
}
}
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
impl<Sized? T: Eq> Eq for Box<T> {}

/// Extension methods for an owning `Any` trait object.
#[unstable = "post-DST and coherence changes, this will not be a trait but \
rather a direct `impl` on `Box<Any>`"]
Expand Down
10 changes: 5 additions & 5 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1598,15 +1598,15 @@ mod tests {
#[test]
fn test_total_ord() {
let c: &[int] = &[1, 2, 3];
[1, 2, 3, 4][].cmp(& c) == Greater;
[1, 2, 3, 4][].cmp(c) == Greater;
let c: &[int] = &[1, 2, 3, 4];
[1, 2, 3][].cmp(& c) == Less;
[1, 2, 3][].cmp(c) == Less;
let c: &[int] = &[1, 2, 3, 6];
[1, 2, 3, 4][].cmp(& c) == Equal;
[1, 2, 3, 4][].cmp(c) == Equal;
let c: &[int] = &[1, 2, 3, 4, 5, 6];
[1, 2, 3, 4, 5, 5, 5, 5][].cmp(& c) == Less;
[1, 2, 3, 4, 5, 5, 5, 5][].cmp(c) == Less;
let c: &[int] = &[1, 2, 3, 4];
[2, 2][].cmp(& c) == Greater;
[2, 2][].cmp(c) == Greater;
}

#[test]
Expand Down
17 changes: 12 additions & 5 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,17 @@ impl<'a> PartialOrd for MaybeOwned<'a> {
}

impl<'a> Ord for MaybeOwned<'a> {
// NOTE(stage0): remove method after a snapshot
#[cfg(stage0)]
#[inline]
fn cmp(&self, other: &MaybeOwned) -> Ordering {
self.as_slice().cmp(&other.as_slice())
}
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
#[inline]
fn cmp(&self, other: &MaybeOwned) -> Ordering {
self.as_slice().cmp(other.as_slice())
}
}

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

#[test]
fn test_total_ord() {
"1234".cmp(&("123")) == Greater;
"123".cmp(&("1234")) == Less;
"1234".cmp(&("1234")) == Equal;
"12345555".cmp(&("123456")) == Less;
"22".cmp(&("1234")) == Greater;
"1234".cmp("123") == Greater;
"123".cmp("1234") == Less;
"1234".cmp("1234") == Equal;
"12345555".cmp("123456") == Less;
"22".cmp("1234") == Greater;
}

#[test]
Expand Down
14 changes: 7 additions & 7 deletions src/libcollections/tree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ impl<K, V> TreeMap<K, V> {
/// let headers = get_headers();
/// let ua_key = "User-Agent";
/// let ua = headers.find_with(|k| {
/// ua_key.cmp(&k.as_slice())
/// ua_key.cmp(k.as_slice())
/// });
///
/// assert_eq!((*ua.unwrap()).as_slice(), "Curl-Rust/0.1");
Expand All @@ -603,7 +603,7 @@ impl<K, V> TreeMap<K, V> {
/// t.insert("User-Agent", "Curl-Rust/0.1");
///
/// let new_ua = "Safari/156.0";
/// match t.find_with_mut(|k| "User-Agent".cmp(k)) {
/// match t.find_with_mut(|&k| "User-Agent".cmp(k)) {
/// Some(x) => *x = new_ua,
/// None => panic!(),
/// }
Expand Down Expand Up @@ -1302,7 +1302,7 @@ mod test_treemap {
#[test]
fn find_with_empty() {
let m: TreeMap<&'static str,int> = TreeMap::new();
assert!(m.find_with(|k| "test".cmp(k)) == None);
assert!(m.find_with(|&k| "test".cmp(k)) == None);
}

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

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

#[test]
Expand All @@ -1343,10 +1343,10 @@ mod test_treemap {
assert!(m.insert("t2", 8));
assert!(m.insert("t5", 14));
let new = 100;
match m.find_with_mut(|k| "t5".cmp(k)) {
match m.find_with_mut(|&k| "t5".cmp(k)) {
None => panic!(), Some(x) => *x = new
}
assert_eq!(m.find_with(|k| "t5".cmp(k)), Some(&new));
assert_eq!(m.find_with(|&k| "t5".cmp(k)), Some(&new));
}

#[test]
Expand Down
14 changes: 14 additions & 0 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,17 @@ impl<T: PartialEq> PartialEq for Vec<T> {

#[unstable = "waiting on PartialOrd stability"]
impl<T: PartialOrd> PartialOrd for Vec<T> {
// NOTE(stage0): remove method after a snapshot
#[cfg(stage0)]
#[inline]
fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering> {
self.as_slice().partial_cmp(&other.as_slice())
}
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
#[inline]
fn partial_cmp(&self, other: &Vec<T>) -> Option<Ordering> {
self.as_slice().partial_cmp(other.as_slice())
}
}

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

#[unstable = "waiting on Ord stability"]
impl<T: Ord> Ord for Vec<T> {
// NOTE(stage0): remove method after a snapshot
#[cfg(stage0)]
#[inline]
fn cmp(&self, other: &Vec<T>) -> Ordering {
self.as_slice().cmp(&other.as_slice())
}
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
#[inline]
fn cmp(&self, other: &Vec<T>) -> Ordering {
self.as_slice().cmp(other.as_slice())
}
}

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

0 comments on commit e84e7a0

Please sign in to comment.