about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2014-10-31 05:41:25 -0400
committerNiko Matsakis <niko@alum.mit.edu>2014-11-05 09:15:28 -0500
commit33ef78fa8bbe9b8d05ba0da607d4da5e31475a95 (patch)
tree774228e56b93cc095456f45929824f2c667b11cf
parent4af52eee59ff25a7f636798bdbc3f1bec985828f (diff)
downloadrust-33ef78fa8bbe9b8d05ba0da607d4da5e31475a95.tar.gz
rust-33ef78fa8bbe9b8d05ba0da607d4da5e31475a95.zip
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.
-rw-r--r--src/libcollections/slice.rs14
-rw-r--r--src/libcollections/vec.rs6
-rw-r--r--src/libcore/array.rs87
-rw-r--r--src/libcore/lib.rs4
-rw-r--r--src/libstd/collections/hash/set.rs2
5 files changed, 102 insertions, 11 deletions
diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs
index 107dc3e5c28..eb4ff345b51 100644
--- a/src/libcollections/slice.rs
+++ b/src/libcollections/slice.rs
@@ -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]
@@ -1980,7 +1980,7 @@ mod tests {
             let (left, right) = values.split_at_mut(2);
             {
                 let left: &[_] = left;
-                assert!(left[0..left.len()] == [1, 2]);
+                assert!(left[0..left.len()] == [1, 2][]);
             }
             for p in left.iter_mut() {
                 *p += 1;
@@ -1988,7 +1988,7 @@ mod tests {
 
             {
                 let right: &[_] = right;
-                assert!(right[0..right.len()] == [3, 4, 5]);
+                assert!(right[0..right.len()] == [3, 4, 5][]);
             }
             for p in right.iter_mut() {
                 *p += 2;
diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs
index b29d7d33782..40e7c949972 100644
--- a/src/libcollections/vec.rs
+++ b/src/libcollections/vec.rs
@@ -1800,7 +1800,7 @@ mod tests {
             let (left, right) = values.split_at_mut(2);
             {
                 let left: &[_] = left;
-                assert!(left[0..left.len()] == [1, 2]);
+                assert!(left[0..left.len()] == [1, 2][]);
             }
             for p in left.iter_mut() {
                 *p += 1;
@@ -1808,7 +1808,7 @@ mod tests {
 
             {
                 let right: &[_] = right;
-                assert!(right[0..right.len()] == [3, 4, 5]);
+                assert!(right[0..right.len()] == [3, 4, 5][]);
             }
             for p in right.iter_mut() {
                 *p += 2;
@@ -1863,7 +1863,7 @@ mod tests {
     #[test]
     fn test_retain() {
         let mut vec = vec![1u, 2, 3, 4];
-        vec.retain(|x| x%2 == 0);
+        vec.retain(|&x| x % 2 == 0);
         assert!(vec == vec![2u, 4]);
     }
 
diff --git a/src/libcore/array.rs b/src/libcore/array.rs
new file mode 100644
index 00000000000..999574b4d7d
--- /dev/null
+++ b/src/libcore/array.rs
@@ -0,0 +1,87 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+/*!
+ * Implementations of things like `Eq` for fixed-length arrays
+ * up to a certain length. Eventually we should able to generalize
+ * to all lengths.
+ */
+
+#![doc(primitive = "tuple")]
+#![stable]
+
+#[unstable = "this is just a documentation module and should not be part \
+              of the public api"]
+pub use unit;
+
+use cmp::*;
+use option::{Option};
+
+// macro for implementing n-ary tuple functions and operations
+macro_rules! array_impls {
+    ($($N:expr)+) => {
+        $(
+            #[unstable = "waiting for PartialEq to stabilize"]
+            impl<T:PartialEq> PartialEq for [T, ..$N] {
+                #[inline]
+                fn eq(&self, other: &[T, ..$N]) -> bool {
+                    self[] == other[]
+                }
+                #[inline]
+                fn ne(&self, other: &[T, ..$N]) -> bool {
+                    self[] != other[]
+                }
+            }
+
+            #[unstable = "waiting for Eq to stabilize"]
+            impl<T:Eq> Eq for [T, ..$N] { }
+
+            #[unstable = "waiting for PartialOrd to stabilize"]
+            impl<T:PartialOrd> PartialOrd for [T, ..$N] {
+                #[inline]
+                fn partial_cmp(&self, other: &[T, ..$N]) -> Option<Ordering> {
+                    PartialOrd::partial_cmp(&self[], &other[])
+                }
+                #[inline]
+                fn lt(&self, other: &[T, ..$N]) -> bool {
+                    PartialOrd::lt(&self[], &other[])
+                }
+                #[inline]
+                fn le(&self, other: &[T, ..$N]) -> bool {
+                    PartialOrd::le(&self[], &other[])
+                }
+                #[inline]
+                fn ge(&self, other: &[T, ..$N]) -> bool {
+                    PartialOrd::ge(&self[], &other[])
+                }
+                #[inline]
+                fn gt(&self, other: &[T, ..$N]) -> bool {
+                    PartialOrd::gt(&self[], &other[])
+                }
+            }
+
+            #[unstable = "waiting for Ord to stabilize"]
+            impl<T:Ord> Ord for [T, ..$N] {
+                #[inline]
+                fn cmp(&self, other: &[T, ..$N]) -> Ordering {
+                    Ord::cmp(&self[], &other[])
+                }
+            }
+        )+
+    }
+}
+
+array_impls! {
+     0  1  2  3  4  5  6  7  8  9
+    10 11 12 13 14 15 16 17 18 19
+    20 21 22 23 24 25 26 27 28 29
+    30 31 32
+}
+
diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs
index 7c5c54c6d8a..d9a0c398605 100644
--- a/src/libcore/lib.rs
+++ b/src/libcore/lib.rs
@@ -126,6 +126,10 @@ pub mod tuple;
 pub mod unit;
 pub mod fmt;
 
+// note: does not need to be public
+#[cfg(not(stage0))]
+mod array;
+
 #[doc(hidden)]
 mod core {
     pub use panicking;
diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs
index b9758e11bc7..688036d22dd 100644
--- a/src/libstd/collections/hash/set.rs
+++ b/src/libstd/collections/hash/set.rs
@@ -792,7 +792,7 @@ mod test_set {
         };
 
         let v = hs.into_iter().collect::<Vec<char>>();
-        assert!(['a', 'b'] == v.as_slice() || ['b', 'a'] == v.as_slice());
+        assert!(['a', 'b'][] == v.as_slice() || ['b', 'a'][] == v.as_slice());
     }
 
     #[test]