Skip to content

Commit b8768cd

Browse files
committed
Add impls of the comparison operators for fixed-length arrays of lengths 0...32 and repair various cases where slices and fixed-length arrays were being compared.
1 parent b106480 commit b8768cd

File tree

5 files changed

+102
-11
lines changed

5 files changed

+102
-11
lines changed

src/libcollections/slice.rs

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

16141614
#[test]
@@ -1982,15 +1982,15 @@ mod tests {
19821982
let (left, right) = values.split_at_mut(2);
19831983
{
19841984
let left: &[_] = left;
1985-
assert!(left[0..left.len()] == [1, 2]);
1985+
assert!(left[0..left.len()] == [1, 2][]);
19861986
}
19871987
for p in left.iter_mut() {
19881988
*p += 1;
19891989
}
19901990

19911991
{
19921992
let right: &[_] = right;
1993-
assert!(right[0..right.len()] == [3, 4, 5]);
1993+
assert!(right[0..right.len()] == [3, 4, 5][]);
19941994
}
19951995
for p in right.iter_mut() {
19961996
*p += 2;

src/libcollections/vec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1772,15 +1772,15 @@ mod tests {
17721772
let (left, right) = values.split_at_mut(2);
17731773
{
17741774
let left: &[_] = left;
1775-
assert!(left[0..left.len()] == [1, 2]);
1775+
assert!(left[0..left.len()] == [1, 2][]);
17761776
}
17771777
for p in left.iter_mut() {
17781778
*p += 1;
17791779
}
17801780

17811781
{
17821782
let right: &[_] = right;
1783-
assert!(right[0..right.len()] == [3, 4, 5]);
1783+
assert!(right[0..right.len()] == [3, 4, 5][]);
17841784
}
17851785
for p in right.iter_mut() {
17861786
*p += 2;
@@ -1835,7 +1835,7 @@ mod tests {
18351835
#[test]
18361836
fn test_retain() {
18371837
let mut vec = vec![1u, 2, 3, 4];
1838-
vec.retain(|x| x%2 == 0);
1838+
vec.retain(|&x| x % 2 == 0);
18391839
assert!(vec == vec![2u, 4]);
18401840
}
18411841

src/libcore/array.rs

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2014 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+
/*!
12+
* Implementations of things like `Eq` for fixed-length arrays
13+
* up to a certain length. Eventually we should able to generalize
14+
* to all lengths.
15+
*/
16+
17+
#![doc(primitive = "tuple")]
18+
#![stable]
19+
20+
#[unstable = "this is just a documentation module and should not be part \
21+
of the public api"]
22+
pub use unit;
23+
24+
use cmp::*;
25+
use option::{Option};
26+
27+
// macro for implementing n-ary tuple functions and operations
28+
macro_rules! array_impls {
29+
($($N:expr)+) => {
30+
$(
31+
#[unstable = "waiting for PartialEq to stabilize"]
32+
impl<T:PartialEq> PartialEq for [T, ..$N] {
33+
#[inline]
34+
fn eq(&self, other: &[T, ..$N]) -> bool {
35+
self[] == other[]
36+
}
37+
#[inline]
38+
fn ne(&self, other: &[T, ..$N]) -> bool {
39+
self[] != other[]
40+
}
41+
}
42+
43+
#[unstable = "waiting for Eq to stabilize"]
44+
impl<T:Eq> Eq for [T, ..$N] { }
45+
46+
#[unstable = "waiting for PartialOrd to stabilize"]
47+
impl<T:PartialOrd> PartialOrd for [T, ..$N] {
48+
#[inline]
49+
fn partial_cmp(&self, other: &[T, ..$N]) -> Option<Ordering> {
50+
PartialOrd::partial_cmp(&self[], &other[])
51+
}
52+
#[inline]
53+
fn lt(&self, other: &[T, ..$N]) -> bool {
54+
PartialOrd::lt(&self[], &other[])
55+
}
56+
#[inline]
57+
fn le(&self, other: &[T, ..$N]) -> bool {
58+
PartialOrd::le(&self[], &other[])
59+
}
60+
#[inline]
61+
fn ge(&self, other: &[T, ..$N]) -> bool {
62+
PartialOrd::ge(&self[], &other[])
63+
}
64+
#[inline]
65+
fn gt(&self, other: &[T, ..$N]) -> bool {
66+
PartialOrd::gt(&self[], &other[])
67+
}
68+
}
69+
70+
#[unstable = "waiting for Ord to stabilize"]
71+
impl<T:Ord> Ord for [T, ..$N] {
72+
#[inline]
73+
fn cmp(&self, other: &[T, ..$N]) -> Ordering {
74+
Ord::cmp(&self[], &other[])
75+
}
76+
}
77+
)+
78+
}
79+
}
80+
81+
array_impls! {
82+
0 1 2 3 4 5 6 7 8 9
83+
10 11 12 13 14 15 16 17 18 19
84+
20 21 22 23 24 25 26 27 28 29
85+
30 31 32
86+
}
87+

src/libcore/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ pub mod tuple;
127127
pub mod unit;
128128
pub mod fmt;
129129

130+
// note: does not need to be public
131+
#[cfg(not(stage0))]
132+
mod array;
133+
130134
#[doc(hidden)]
131135
mod core {
132136
pub use panicking;

src/libstd/collections/hashmap/set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ mod test_set {
668668
};
669669

670670
let v = hs.into_iter().collect::<Vec<char>>();
671-
assert!(['a', 'b'] == v.as_slice() || ['b', 'a'] == v.as_slice());
671+
assert!(['a', 'b'][] == v.as_slice() || ['b', 'a'][] == v.as_slice());
672672
}
673673

674674
#[test]

0 commit comments

Comments
 (0)