Skip to content

Commit 33ef78f

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 4af52ee commit 33ef78f

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
@@ -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]
@@ -1980,15 +1980,15 @@ mod tests {
19801980
let (left, right) = values.split_at_mut(2);
19811981
{
19821982
let left: &[_] = left;
1983-
assert!(left[0..left.len()] == [1, 2]);
1983+
assert!(left[0..left.len()] == [1, 2][]);
19841984
}
19851985
for p in left.iter_mut() {
19861986
*p += 1;
19871987
}
19881988

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

src/libcollections/vec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1800,15 +1800,15 @@ mod tests {
18001800
let (left, right) = values.split_at_mut(2);
18011801
{
18021802
let left: &[_] = left;
1803-
assert!(left[0..left.len()] == [1, 2]);
1803+
assert!(left[0..left.len()] == [1, 2][]);
18041804
}
18051805
for p in left.iter_mut() {
18061806
*p += 1;
18071807
}
18081808

18091809
{
18101810
let right: &[_] = right;
1811-
assert!(right[0..right.len()] == [3, 4, 5]);
1811+
assert!(right[0..right.len()] == [3, 4, 5][]);
18121812
}
18131813
for p in right.iter_mut() {
18141814
*p += 2;
@@ -1863,7 +1863,7 @@ mod tests {
18631863
#[test]
18641864
fn test_retain() {
18651865
let mut vec = vec![1u, 2, 3, 4];
1866-
vec.retain(|x| x%2 == 0);
1866+
vec.retain(|&x| x % 2 == 0);
18671867
assert!(vec == vec![2u, 4]);
18681868
}
18691869

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
@@ -126,6 +126,10 @@ pub mod tuple;
126126
pub mod unit;
127127
pub mod fmt;
128128

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

src/libstd/collections/hash/set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ mod test_set {
792792
};
793793

794794
let v = hs.into_iter().collect::<Vec<char>>();
795-
assert!(['a', 'b'] == v.as_slice() || ['b', 'a'] == v.as_slice());
795+
assert!(['a', 'b'][] == v.as_slice() || ['b', 'a'][] == v.as_slice());
796796
}
797797

798798
#[test]

0 commit comments

Comments
 (0)