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.
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.
11 //! Operations on tuples
13 //! To access a single element of a tuple one can use the following
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
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.
26 //! If every type inside a tuple implements one of the following
27 //! traits, then a tuple itself also implements it.
38 #[unstable = "this is just a documentation module and should not be part \
46 use option::Option::Some;
48 // FIXME(#19630) Remove this work-around
53 // macro for implementing n-ary tuple functions and operations
54 macro_rules! tuple_impls {
57 $(($valN:ident, $refN:ident, $mutN:ident, $idx:tt) -> $T:ident)+
62 impl<$($T:Clone),+> Clone for ($($T,)+) {
63 fn clone(&self) -> ($($T,)+) {
64 ($(e!(self.$idx.clone()),)+)
69 impl<$($T:PartialEq),+> PartialEq for ($($T,)+) {
71 fn eq(&self, other: &($($T,)+)) -> bool {
72 e!($(self.$idx == other.$idx)&&+)
75 fn ne(&self, other: &($($T,)+)) -> bool {
76 e!($(self.$idx != other.$idx)||+)
81 impl<$($T:Eq),+> Eq for ($($T,)+) {}
84 impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) {
86 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
87 lexical_partial_cmp!($(self.$idx, other.$idx),+)
90 fn lt(&self, other: &($($T,)+)) -> bool {
91 lexical_ord!(lt, $(self.$idx, other.$idx),+)
94 fn le(&self, other: &($($T,)+)) -> bool {
95 lexical_ord!(le, $(self.$idx, other.$idx),+)
98 fn ge(&self, other: &($($T,)+)) -> bool {
99 lexical_ord!(ge, $(self.$idx, other.$idx),+)
102 fn gt(&self, other: &($($T,)+)) -> bool {
103 lexical_ord!(gt, $(self.$idx, other.$idx),+)
108 impl<$($T:Ord),+> Ord for ($($T,)+) {
110 fn cmp(&self, other: &($($T,)+)) -> Ordering {
111 lexical_cmp!($(self.$idx, other.$idx),+)
116 impl<$($T:Default),+> Default for ($($T,)+) {
119 fn default() -> ($($T,)+) {
120 ($({ let x: $T = Default::default(); x},)+)
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),+) }
136 ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
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),+),
146 ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
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),+),
156 ($a:expr, $b:expr) => { ($a).cmp(&$b) };
161 (val0, ref0, mut0, 0) -> A
164 (val0, ref0, mut0, 0) -> A
165 (val1, ref1, mut1, 1) -> B
168 (val0, ref0, mut0, 0) -> A
169 (val1, ref1, mut1, 1) -> B
170 (val2, ref2, mut2, 2) -> C
173 (val0, ref0, mut0, 0) -> A
174 (val1, ref1, mut1, 1) -> B
175 (val2, ref2, mut2, 2) -> C
176 (val3, ref3, mut3, 3) -> D
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
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
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
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
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
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
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
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