]> git.lizzy.rs Git - rust.git/blob - src/libcore/tuple.rs
Merge pull request #20510 from tshepang/patch-6
[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 //! Operations on tuples
12 //!
13 //! To access a single element of a tuple one can use the following
14 //! methods:
15 //!
16 //! * `valN` - returns a value of _N_-th element
17 //! * `refN` - returns a reference to _N_-th element
18 //! * `mutN` - returns a mutable reference to _N_-th element
19 //!
20 //! Indexing starts from zero, so `val0` returns first value, `val1`
21 //! returns second value, and so on. In general, a tuple with _S_
22 //! elements provides aforementioned methods suffixed with numbers
23 //! from `0` to `S-1`. Traits which contain these methods are
24 //! implemented for tuples with up to 12 elements.
25 //!
26 //! If every type inside a tuple implements one of the following
27 //! traits, then a tuple itself also implements it.
28 //!
29 //! * `Clone`
30 //! * `PartialEq`
31 //! * `Eq`
32 //! * `PartialOrd`
33 //! * `Ord`
34 //! * `Default`
35
36 #![stable]
37
38 #[unstable = "this is just a documentation module and should not be part \
39               of the public api"]
40
41 use clone::Clone;
42 use cmp::*;
43 use cmp::Ordering::*;
44 use default::Default;
45 use option::Option;
46 use option::Option::Some;
47
48 // FIXME(#19630) Remove this work-around
49 macro_rules! e {
50     ($e:expr) => { $e }
51 }
52
53 // macro for implementing n-ary tuple functions and operations
54 macro_rules! tuple_impls {
55     ($(
56         $Tuple:ident {
57             $(($valN:ident, $refN:ident, $mutN:ident, $idx:tt) -> $T:ident)+
58         }
59     )+) => {
60         $(
61             #[stable]
62             impl<$($T:Clone),+> Clone for ($($T,)+) {
63                 fn clone(&self) -> ($($T,)+) {
64                     ($(e!(self.$idx.clone()),)+)
65                 }
66             }
67
68             #[stable]
69             impl<$($T:PartialEq),+> PartialEq for ($($T,)+) {
70                 #[inline]
71                 fn eq(&self, other: &($($T,)+)) -> bool {
72                     e!($(self.$idx == other.$idx)&&+)
73                 }
74                 #[inline]
75                 fn ne(&self, other: &($($T,)+)) -> bool {
76                     e!($(self.$idx != other.$idx)||+)
77                 }
78             }
79
80             #[stable]
81             impl<$($T:Eq),+> Eq for ($($T,)+) {}
82
83             #[stable]
84             impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) {
85                 #[inline]
86                 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
87                     lexical_partial_cmp!($(self.$idx, other.$idx),+)
88                 }
89                 #[inline]
90                 fn lt(&self, other: &($($T,)+)) -> bool {
91                     lexical_ord!(lt, $(self.$idx, other.$idx),+)
92                 }
93                 #[inline]
94                 fn le(&self, other: &($($T,)+)) -> bool {
95                     lexical_ord!(le, $(self.$idx, other.$idx),+)
96                 }
97                 #[inline]
98                 fn ge(&self, other: &($($T,)+)) -> bool {
99                     lexical_ord!(ge, $(self.$idx, other.$idx),+)
100                 }
101                 #[inline]
102                 fn gt(&self, other: &($($T,)+)) -> bool {
103                     lexical_ord!(gt, $(self.$idx, other.$idx),+)
104                 }
105             }
106
107             #[stable]
108             impl<$($T:Ord),+> Ord for ($($T,)+) {
109                 #[inline]
110                 fn cmp(&self, other: &($($T,)+)) -> Ordering {
111                     lexical_cmp!($(self.$idx, other.$idx),+)
112                 }
113             }
114
115             #[stable]
116             impl<$($T:Default),+> Default for ($($T,)+) {
117                 #[stable]
118                 #[inline]
119                 fn default() -> ($($T,)+) {
120                     ($({ let x: $T = Default::default(); x},)+)
121                 }
122             }
123         )+
124     }
125 }
126
127 // Constructs an expression that performs a lexical ordering using method $rel.
128 // The values are interleaved, so the macro invocation for
129 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
130 // a3, b3)` (and similarly for `lexical_cmp`)
131 macro_rules! lexical_ord {
132     ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
133         if $a != $b { lexical_ord!($rel, $a, $b) }
134         else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
135     };
136     ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
137 }
138
139 macro_rules! lexical_partial_cmp {
140     ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
141         match ($a).partial_cmp(&$b) {
142             Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
143             ordering   => ordering
144         }
145     };
146     ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
147 }
148
149 macro_rules! lexical_cmp {
150     ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
151         match ($a).cmp(&$b) {
152             Equal => lexical_cmp!($($rest_a, $rest_b),+),
153             ordering   => ordering
154         }
155     };
156     ($a:expr, $b:expr) => { ($a).cmp(&$b) };
157 }
158
159 tuple_impls! {
160     Tuple1 {
161         (val0, ref0, mut0, 0) -> A
162     }
163     Tuple2 {
164         (val0, ref0, mut0, 0) -> A
165         (val1, ref1, mut1, 1) -> B
166     }
167     Tuple3 {
168         (val0, ref0, mut0, 0) -> A
169         (val1, ref1, mut1, 1) -> B
170         (val2, ref2, mut2, 2) -> C
171     }
172     Tuple4 {
173         (val0, ref0, mut0, 0) -> A
174         (val1, ref1, mut1, 1) -> B
175         (val2, ref2, mut2, 2) -> C
176         (val3, ref3, mut3, 3) -> D
177     }
178     Tuple5 {
179         (val0, ref0, mut0, 0) -> A
180         (val1, ref1, mut1, 1) -> B
181         (val2, ref2, mut2, 2) -> C
182         (val3, ref3, mut3, 3) -> D
183         (val4, ref4, mut4, 4) -> E
184     }
185     Tuple6 {
186         (val0, ref0, mut0, 0) -> A
187         (val1, ref1, mut1, 1) -> B
188         (val2, ref2, mut2, 2) -> C
189         (val3, ref3, mut3, 3) -> D
190         (val4, ref4, mut4, 4) -> E
191         (val5, ref5, mut5, 5) -> F
192     }
193     Tuple7 {
194         (val0, ref0, mut0, 0) -> A
195         (val1, ref1, mut1, 1) -> B
196         (val2, ref2, mut2, 2) -> C
197         (val3, ref3, mut3, 3) -> D
198         (val4, ref4, mut4, 4) -> E
199         (val5, ref5, mut5, 5) -> F
200         (val6, ref6, mut6, 6) -> G
201     }
202     Tuple8 {
203         (val0, ref0, mut0, 0) -> A
204         (val1, ref1, mut1, 1) -> B
205         (val2, ref2, mut2, 2) -> C
206         (val3, ref3, mut3, 3) -> D
207         (val4, ref4, mut4, 4) -> E
208         (val5, ref5, mut5, 5) -> F
209         (val6, ref6, mut6, 6) -> G
210         (val7, ref7, mut7, 7) -> H
211     }
212     Tuple9 {
213         (val0, ref0, mut0, 0) -> A
214         (val1, ref1, mut1, 1) -> B
215         (val2, ref2, mut2, 2) -> C
216         (val3, ref3, mut3, 3) -> D
217         (val4, ref4, mut4, 4) -> E
218         (val5, ref5, mut5, 5) -> F
219         (val6, ref6, mut6, 6) -> G
220         (val7, ref7, mut7, 7) -> H
221         (val8, ref8, mut8, 8) -> I
222     }
223     Tuple10 {
224         (val0, ref0, mut0, 0) -> A
225         (val1, ref1, mut1, 1) -> B
226         (val2, ref2, mut2, 2) -> C
227         (val3, ref3, mut3, 3) -> D
228         (val4, ref4, mut4, 4) -> E
229         (val5, ref5, mut5, 5) -> F
230         (val6, ref6, mut6, 6) -> G
231         (val7, ref7, mut7, 7) -> H
232         (val8, ref8, mut8, 8) -> I
233         (val9, ref9, mut9, 9) -> J
234     }
235     Tuple11 {
236         (val0, ref0, mut0, 0) -> A
237         (val1, ref1, mut1, 1) -> B
238         (val2, ref2, mut2, 2) -> C
239         (val3, ref3, mut3, 3) -> D
240         (val4, ref4, mut4, 4) -> E
241         (val5, ref5, mut5, 5) -> F
242         (val6, ref6, mut6, 6) -> G
243         (val7, ref7, mut7, 7) -> H
244         (val8, ref8, mut8, 8) -> I
245         (val9, ref9, mut9, 9) -> J
246         (val10, ref10, mut10, 10) -> K
247     }
248     Tuple12 {
249         (val0, ref0, mut0, 0) -> A
250         (val1, ref1, mut1, 1) -> B
251         (val2, ref2, mut2, 2) -> C
252         (val3, ref3, mut3, 3) -> D
253         (val4, ref4, mut4, 4) -> E
254         (val5, ref5, mut5, 5) -> F
255         (val6, ref6, mut6, 6) -> G
256         (val7, ref7, mut7, 7) -> H
257         (val8, ref8, mut8, 8) -> I
258         (val9, ref9, mut9, 9) -> J
259         (val10, ref10, mut10, 10) -> K
260         (val11, ref11, mut11, 11) -> L
261     }
262 }