]> git.lizzy.rs Git - rust.git/blob - src/libcore/array.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / libcore / array.rs
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 //! Implementations of things like `Eq` for fixed-length arrays
12 //! up to a certain length. Eventually we should able to generalize
13 //! to all lengths.
14
15 #![unstable(feature = "core")] // not yet reviewed
16
17 use clone::Clone;
18 use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
19 use fmt;
20 use hash::{Hash, self};
21 use iter::IntoIterator;
22 use marker::Copy;
23 use ops::Deref;
24 use option::Option;
25 use slice::{Iter, IterMut, SliceExt};
26
27 // macro for implementing n-ary tuple functions and operations
28 macro_rules! array_impls {
29     ($($N:expr)+) => {
30         $(
31             #[stable(feature = "rust1", since = "1.0.0")]
32             impl<T:Copy> Clone for [T; $N] {
33                 fn clone(&self) -> [T; $N] {
34                     *self
35                 }
36             }
37
38             #[cfg(stage0)]
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)
42                 }
43             }
44             #[cfg(not(stage0))]
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)
49                 }
50             }
51
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)
56                 }
57             }
58
59             #[stable(feature = "rust1", since = "1.0.0")]
60             impl<'a, T> IntoIterator for &'a [T; $N] {
61                 type Item = &'a T;
62                 type IntoIter = Iter<'a, T>;
63
64                 fn into_iter(self) -> Iter<'a, T> {
65                     self.iter()
66                 }
67             }
68
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>;
73
74                 fn into_iter(self) -> IterMut<'a, T> {
75                     self.iter_mut()
76                 }
77             }
78
79             #[stable(feature = "rust1", since = "1.0.0")]
80             impl<A, B> PartialEq<[B; $N]> for [A; $N] where A: PartialEq<B> {
81                 #[inline]
82                 fn eq(&self, other: &[B; $N]) -> bool {
83                     &self[..] == &other[..]
84                 }
85                 #[inline]
86                 fn ne(&self, other: &[B; $N]) -> bool {
87                     &self[..] != &other[..]
88                 }
89             }
90
91             #[stable(feature = "rust1", since = "1.0.0")]
92             impl<'a, A, B, Rhs> PartialEq<Rhs> for [A; $N] where
93                 A: PartialEq<B>,
94                 Rhs: Deref<Target=[B]>,
95             {
96                 #[inline(always)]
97                 fn eq(&self, other: &Rhs) -> bool {
98                     PartialEq::eq(&self[..], &**other)
99                 }
100                 #[inline(always)]
101                 fn ne(&self, other: &Rhs) -> bool {
102                     PartialEq::ne(&self[..], &**other)
103                 }
104             }
105
106             #[stable(feature = "rust1", since = "1.0.0")]
107             impl<'a, A, B, Lhs> PartialEq<[B; $N]> for Lhs where
108                 A: PartialEq<B>,
109                 Lhs: Deref<Target=[A]>
110             {
111                 #[inline(always)]
112                 fn eq(&self, other: &[B; $N]) -> bool {
113                     PartialEq::eq(&**self, &other[..])
114                 }
115                 #[inline(always)]
116                 fn ne(&self, other: &[B; $N]) -> bool {
117                     PartialEq::ne(&**self, &other[..])
118                 }
119             }
120
121             #[stable(feature = "rust1", since = "1.0.0")]
122             impl<T:Eq> Eq for [T; $N] { }
123
124             #[stable(feature = "rust1", since = "1.0.0")]
125             impl<T:PartialOrd> PartialOrd for [T; $N] {
126                 #[inline]
127                 fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> {
128                     PartialOrd::partial_cmp(&&self[..], &&other[..])
129                 }
130                 #[inline]
131                 fn lt(&self, other: &[T; $N]) -> bool {
132                     PartialOrd::lt(&&self[..], &&other[..])
133                 }
134                 #[inline]
135                 fn le(&self, other: &[T; $N]) -> bool {
136                     PartialOrd::le(&&self[..], &&other[..])
137                 }
138                 #[inline]
139                 fn ge(&self, other: &[T; $N]) -> bool {
140                     PartialOrd::ge(&&self[..], &&other[..])
141                 }
142                 #[inline]
143                 fn gt(&self, other: &[T; $N]) -> bool {
144                     PartialOrd::gt(&&self[..], &&other[..])
145                 }
146             }
147
148             #[stable(feature = "rust1", since = "1.0.0")]
149             impl<T:Ord> Ord for [T; $N] {
150                 #[inline]
151                 fn cmp(&self, other: &[T; $N]) -> Ordering {
152                     Ord::cmp(&&self[..], &&other[..])
153                 }
154             }
155         )+
156     }
157 }
158
159 array_impls! {
160      0  1  2  3  4  5  6  7  8  9
161     10 11 12 13 14 15 16 17 18 19
162     20 21 22 23 24 25 26 27 28 29
163     30 31 32
164 }