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