]> git.lizzy.rs Git - rust.git/blob - src/libcore/array.rs
Rollup merge of #20991 - mneumann:llvm-dragonfly-take2, r=alexcrichton
[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] // not yet reviewed
16
17 use clone::Clone;
18 use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
19 use fmt;
20 use hash::{Hash, Hasher, self};
21 use marker::Copy;
22 use ops::{Deref, FullRange};
23 use option::Option;
24
25 // macro for implementing n-ary tuple functions and operations
26 macro_rules! array_impls {
27     ($($N:expr)+) => {
28         $(
29             #[stable]
30             impl<T:Copy> Clone for [T; $N] {
31                 fn clone(&self) -> [T; $N] {
32                     *self
33                 }
34             }
35
36             impl<S: hash::Writer + Hasher, T: Hash<S>> Hash<S> for [T; $N] {
37                 fn hash(&self, state: &mut S) {
38                     Hash::hash(&self[], state)
39                 }
40             }
41
42             #[unstable = "waiting for Show to stabilize"]
43             impl<T:fmt::Show> fmt::Show for [T; $N] {
44                 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45                     fmt::Show::fmt(&&self[], f)
46                 }
47             }
48
49             #[stable]
50             impl<A, B> PartialEq<[B; $N]> for [A; $N] where A: PartialEq<B> {
51                 #[inline]
52                 fn eq(&self, other: &[B; $N]) -> bool {
53                     &self[] == &other[]
54                 }
55                 #[inline]
56                 fn ne(&self, other: &[B; $N]) -> bool {
57                     &self[] != &other[]
58                 }
59             }
60
61             #[stable]
62             impl<'a, A, B, Rhs> PartialEq<Rhs> for [A; $N] where
63                 A: PartialEq<B>,
64                 Rhs: Deref<Target=[B]>,
65             {
66                 #[inline(always)]
67                 fn eq(&self, other: &Rhs) -> bool {
68                     PartialEq::eq(&self[], &**other)
69                 }
70                 #[inline(always)]
71                 fn ne(&self, other: &Rhs) -> bool {
72                     PartialEq::ne(&self[], &**other)
73                 }
74             }
75
76             #[stable]
77             impl<'a, A, B, Lhs> PartialEq<[B; $N]> for Lhs where
78                 A: PartialEq<B>,
79                 Lhs: Deref<Target=[A]>
80             {
81                 #[inline(always)]
82                 fn eq(&self, other: &[B; $N]) -> bool {
83                     PartialEq::eq(&**self, &other[])
84                 }
85                 #[inline(always)]
86                 fn ne(&self, other: &[B; $N]) -> bool {
87                     PartialEq::ne(&**self, &other[])
88                 }
89             }
90
91             #[stable]
92             impl<T:Eq> Eq for [T; $N] { }
93
94             #[stable]
95             impl<T:PartialOrd> PartialOrd for [T; $N] {
96                 #[inline]
97                 fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> {
98                     PartialOrd::partial_cmp(&&self[], &&other[])
99                 }
100                 #[inline]
101                 fn lt(&self, other: &[T; $N]) -> bool {
102                     PartialOrd::lt(&&self[], &&other[])
103                 }
104                 #[inline]
105                 fn le(&self, other: &[T; $N]) -> bool {
106                     PartialOrd::le(&&self[], &&other[])
107                 }
108                 #[inline]
109                 fn ge(&self, other: &[T; $N]) -> bool {
110                     PartialOrd::ge(&&self[], &&other[])
111                 }
112                 #[inline]
113                 fn gt(&self, other: &[T; $N]) -> bool {
114                     PartialOrd::gt(&&self[], &&other[])
115                 }
116             }
117
118             #[stable]
119             impl<T:Ord> Ord for [T; $N] {
120                 #[inline]
121                 fn cmp(&self, other: &[T; $N]) -> Ordering {
122                     Ord::cmp(&&self[], &&other[])
123                 }
124             }
125         )+
126     }
127 }
128
129 array_impls! {
130      0  1  2  3  4  5  6  7  8  9
131     10 11 12 13 14 15 16 17 18 19
132     20 21 22 23 24 25 26 27 28 29
133     30 31 32
134 }