]> git.lizzy.rs Git - rust.git/blob - src/libcore/tuple.rs
Changed issue number to 36105
[rust.git] / src / libcore / tuple.rs
1 // Copyright 2012 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 // See src/libstd/primitive_docs.rs for documentation.
12
13 use clone::Clone;
14 use cmp::*;
15 use cmp::Ordering::*;
16 use default::Default;
17 use option::Option;
18 use option::Option::Some;
19
20 // FIXME(#19630) Remove this work-around
21 macro_rules! e {
22     ($e:expr) => { $e }
23 }
24
25 // macro for implementing n-ary tuple functions and operations
26 macro_rules! tuple_impls {
27     ($(
28         $Tuple:ident {
29             $(($idx:tt) -> $T:ident)+
30         }
31     )+) => {
32         $(
33             #[stable(feature = "rust1", since = "1.0.0")]
34             impl<$($T:Clone),+> Clone for ($($T,)+) {
35                 fn clone(&self) -> ($($T,)+) {
36                     ($(e!(self.$idx.clone()),)+)
37                 }
38             }
39
40             #[stable(feature = "rust1", since = "1.0.0")]
41             impl<$($T:PartialEq),+> PartialEq for ($($T,)+) {
42                 #[inline]
43                 fn eq(&self, other: &($($T,)+)) -> bool {
44                     e!($(self.$idx == other.$idx)&&+)
45                 }
46                 #[inline]
47                 fn ne(&self, other: &($($T,)+)) -> bool {
48                     e!($(self.$idx != other.$idx)||+)
49                 }
50             }
51
52             #[stable(feature = "rust1", since = "1.0.0")]
53             impl<$($T:Eq),+> Eq for ($($T,)+) {}
54
55             #[stable(feature = "rust1", since = "1.0.0")]
56             impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) {
57                 #[inline]
58                 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
59                     lexical_partial_cmp!($(self.$idx, other.$idx),+)
60                 }
61                 #[inline]
62                 fn lt(&self, other: &($($T,)+)) -> bool {
63                     lexical_ord!(lt, $(self.$idx, other.$idx),+)
64                 }
65                 #[inline]
66                 fn le(&self, other: &($($T,)+)) -> bool {
67                     lexical_ord!(le, $(self.$idx, other.$idx),+)
68                 }
69                 #[inline]
70                 fn ge(&self, other: &($($T,)+)) -> bool {
71                     lexical_ord!(ge, $(self.$idx, other.$idx),+)
72                 }
73                 #[inline]
74                 fn gt(&self, other: &($($T,)+)) -> bool {
75                     lexical_ord!(gt, $(self.$idx, other.$idx),+)
76                 }
77             }
78
79             #[stable(feature = "rust1", since = "1.0.0")]
80             impl<$($T:Ord),+> Ord for ($($T,)+) {
81                 #[inline]
82                 fn cmp(&self, other: &($($T,)+)) -> Ordering {
83                     lexical_cmp!($(self.$idx, other.$idx),+)
84                 }
85             }
86
87             #[stable(feature = "rust1", since = "1.0.0")]
88             impl<$($T:Default),+> Default for ($($T,)+) {
89                 #[inline]
90                 fn default() -> ($($T,)+) {
91                     ($({ let x: $T = Default::default(); x},)+)
92                 }
93             }
94         )+
95     }
96 }
97
98 // Constructs an expression that performs a lexical ordering using method $rel.
99 // The values are interleaved, so the macro invocation for
100 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
101 // a3, b3)` (and similarly for `lexical_cmp`)
102 macro_rules! lexical_ord {
103     ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
104         if $a != $b { lexical_ord!($rel, $a, $b) }
105         else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
106     };
107     ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
108 }
109
110 macro_rules! lexical_partial_cmp {
111     ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
112         match ($a).partial_cmp(&$b) {
113             Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
114             ordering   => ordering
115         }
116     };
117     ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
118 }
119
120 macro_rules! lexical_cmp {
121     ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
122         match ($a).cmp(&$b) {
123             Equal => lexical_cmp!($($rest_a, $rest_b),+),
124             ordering   => ordering
125         }
126     };
127     ($a:expr, $b:expr) => { ($a).cmp(&$b) };
128 }
129
130 tuple_impls! {
131     Tuple1 {
132         (0) -> A
133     }
134     Tuple2 {
135         (0) -> A
136         (1) -> B
137     }
138     Tuple3 {
139         (0) -> A
140         (1) -> B
141         (2) -> C
142     }
143     Tuple4 {
144         (0) -> A
145         (1) -> B
146         (2) -> C
147         (3) -> D
148     }
149     Tuple5 {
150         (0) -> A
151         (1) -> B
152         (2) -> C
153         (3) -> D
154         (4) -> E
155     }
156     Tuple6 {
157         (0) -> A
158         (1) -> B
159         (2) -> C
160         (3) -> D
161         (4) -> E
162         (5) -> F
163     }
164     Tuple7 {
165         (0) -> A
166         (1) -> B
167         (2) -> C
168         (3) -> D
169         (4) -> E
170         (5) -> F
171         (6) -> G
172     }
173     Tuple8 {
174         (0) -> A
175         (1) -> B
176         (2) -> C
177         (3) -> D
178         (4) -> E
179         (5) -> F
180         (6) -> G
181         (7) -> H
182     }
183     Tuple9 {
184         (0) -> A
185         (1) -> B
186         (2) -> C
187         (3) -> D
188         (4) -> E
189         (5) -> F
190         (6) -> G
191         (7) -> H
192         (8) -> I
193     }
194     Tuple10 {
195         (0) -> A
196         (1) -> B
197         (2) -> C
198         (3) -> D
199         (4) -> E
200         (5) -> F
201         (6) -> G
202         (7) -> H
203         (8) -> I
204         (9) -> J
205     }
206     Tuple11 {
207         (0) -> A
208         (1) -> B
209         (2) -> C
210         (3) -> D
211         (4) -> E
212         (5) -> F
213         (6) -> G
214         (7) -> H
215         (8) -> I
216         (9) -> J
217         (10) -> K
218     }
219     Tuple12 {
220         (0) -> A
221         (1) -> B
222         (2) -> C
223         (3) -> D
224         (4) -> E
225         (5) -> F
226         (6) -> G
227         (7) -> H
228         (8) -> I
229         (9) -> J
230         (10) -> K
231         (11) -> L
232     }
233 }