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.
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.
11 //! Implementations of things like `Eq` for fixed-length arrays
12 //! up to a certain length. Eventually we should able to generalize
15 #![unstable(feature = "core")] // not yet reviewed
18 use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
20 use hash::{Hash, self};
21 use iter::IntoIterator;
25 use slice::{Iter, IterMut, SliceExt};
27 // macro for implementing n-ary tuple functions and operations
28 macro_rules! array_impls {
31 #[stable(feature = "rust1", since = "1.0.0")]
32 impl<T:Copy> Clone for [T; $N] {
33 fn clone(&self) -> [T; $N] {
39 impl<S: hash::Writer + hash::Hasher, T: Hash<S>> Hash<S> for [T; $N] {
40 fn hash(&self, state: &mut S) {
41 Hash::hash(&self[..], state)
45 #[stable(feature = "rust1", since = "1.0.0")]
46 impl<T: Hash> Hash for [T; $N] {
47 fn hash<H: hash::Hasher>(&self, state: &mut H) {
48 Hash::hash(&self[..], state)
52 #[stable(feature = "rust1", since = "1.0.0")]
53 impl<T: fmt::Debug> fmt::Debug for [T; $N] {
54 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55 fmt::Debug::fmt(&&self[..], f)
59 #[stable(feature = "rust1", since = "1.0.0")]
60 impl<'a, T> IntoIterator for &'a [T; $N] {
62 type IntoIter = Iter<'a, T>;
64 fn into_iter(self) -> Iter<'a, T> {
69 #[stable(feature = "rust1", since = "1.0.0")]
70 impl<'a, T> IntoIterator for &'a mut [T; $N] {
71 type Item = &'a mut T;
72 type IntoIter = IterMut<'a, T>;
74 fn into_iter(self) -> IterMut<'a, T> {
79 #[stable(feature = "rust1", since = "1.0.0")]
80 impl<A, B> PartialEq<[B; $N]> for [A; $N] where A: PartialEq<B> {
82 fn eq(&self, other: &[B; $N]) -> bool {
83 &self[..] == &other[..]
86 fn ne(&self, other: &[B; $N]) -> bool {
87 &self[..] != &other[..]
91 #[stable(feature = "rust1", since = "1.0.0")]
92 impl<'a, A, B, Rhs> PartialEq<Rhs> for [A; $N] where
94 Rhs: Deref<Target=[B]>,
97 fn eq(&self, other: &Rhs) -> bool {
98 PartialEq::eq(&self[..], &**other)
101 fn ne(&self, other: &Rhs) -> bool {
102 PartialEq::ne(&self[..], &**other)
106 #[stable(feature = "rust1", since = "1.0.0")]
107 impl<'a, A, B, Lhs> PartialEq<[B; $N]> for Lhs where
109 Lhs: Deref<Target=[A]>
112 fn eq(&self, other: &[B; $N]) -> bool {
113 PartialEq::eq(&**self, &other[..])
116 fn ne(&self, other: &[B; $N]) -> bool {
117 PartialEq::ne(&**self, &other[..])
121 #[stable(feature = "rust1", since = "1.0.0")]
122 impl<T:Eq> Eq for [T; $N] { }
124 #[stable(feature = "rust1", since = "1.0.0")]
125 impl<T:PartialOrd> PartialOrd for [T; $N] {
127 fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> {
128 PartialOrd::partial_cmp(&&self[..], &&other[..])
131 fn lt(&self, other: &[T; $N]) -> bool {
132 PartialOrd::lt(&&self[..], &&other[..])
135 fn le(&self, other: &[T; $N]) -> bool {
136 PartialOrd::le(&&self[..], &&other[..])
139 fn ge(&self, other: &[T; $N]) -> bool {
140 PartialOrd::ge(&&self[..], &&other[..])
143 fn gt(&self, other: &[T; $N]) -> bool {
144 PartialOrd::gt(&&self[..], &&other[..])
148 #[stable(feature = "rust1", since = "1.0.0")]
149 impl<T:Ord> Ord for [T; $N] {
151 fn cmp(&self, other: &[T; $N]) -> Ordering {
152 Ord::cmp(&&self[..], &&other[..])
161 10 11 12 13 14 15 16 17 18 19
162 20 21 22 23 24 25 26 27 28 29