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.
41 //! #[allow(deprecated)]
43 //! let pair = ("pi", 3.14f64);
44 //! assert_eq!(pair.val0(), "pi");
45 //! assert_eq!(pair.val1(), 3.14f64);
49 //! Using traits implemented for tuples:
52 //! use std::default::Default;
58 //! let c = b.clone();
61 //! let d : (u32, f32) = Default::default();
62 //! assert_eq!(d, (0u32, 0.0f32));
67 #[unstable = "this is just a documentation module and should not be part \
75 use option::Option::Some;
77 // FIXME(#19630) Remove this work-around
82 // macro for implementing n-ary tuple functions and operations
83 macro_rules! tuple_impls {
86 $(($valN:ident, $refN:ident, $mutN:ident, $idx:tt) -> $T:ident)+
90 #[allow(missing_docs)]
92 pub trait $Tuple<$($T),+> {
94 #[deprecated = "use tuple indexing: `tuple.N`"]
96 #[deprecated = "use tuple indexing: `&tuple.N`"]
97 fn $refN<'a>(&'a self) -> &'a $T;
98 #[deprecated = "use tuple indexing: `&mut tuple.N`"]
99 fn $mutN<'a>(&'a mut self) -> &'a mut $T;
103 impl<$($T),+> $Tuple<$($T),+> for ($($T,)+) {
106 #[allow(unused_variables)]
107 #[deprecated = "use tuple indexing: `tuple.N`"]
108 fn $valN(self) -> $T {
113 #[allow(unused_variables)]
114 #[deprecated = "use tuple indexing: `&tuple.N`"]
115 fn $refN<'a>(&'a self) -> &'a $T {
120 #[allow(unused_variables)]
121 #[deprecated = "use tuple indexing: &mut tuple.N"]
122 fn $mutN<'a>(&'a mut self) -> &'a mut $T {
129 impl<$($T:Clone),+> Clone for ($($T,)+) {
130 fn clone(&self) -> ($($T,)+) {
131 ($(e!(self.$idx.clone()),)+)
136 impl<$($T:PartialEq),+> PartialEq for ($($T,)+) {
138 fn eq(&self, other: &($($T,)+)) -> bool {
139 e!($(self.$idx == other.$idx)&&+)
142 fn ne(&self, other: &($($T,)+)) -> bool {
143 e!($(self.$idx != other.$idx)||+)
148 impl<$($T:Eq),+> Eq for ($($T,)+) {}
151 impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) {
153 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
154 lexical_partial_cmp!($(self.$idx, other.$idx),+)
157 fn lt(&self, other: &($($T,)+)) -> bool {
158 lexical_ord!(lt, $(self.$idx, other.$idx),+)
161 fn le(&self, other: &($($T,)+)) -> bool {
162 lexical_ord!(le, $(self.$idx, other.$idx),+)
165 fn ge(&self, other: &($($T,)+)) -> bool {
166 lexical_ord!(ge, $(self.$idx, other.$idx),+)
169 fn gt(&self, other: &($($T,)+)) -> bool {
170 lexical_ord!(gt, $(self.$idx, other.$idx),+)
175 impl<$($T:Ord),+> Ord for ($($T,)+) {
177 fn cmp(&self, other: &($($T,)+)) -> Ordering {
178 lexical_cmp!($(self.$idx, other.$idx),+)
183 impl<$($T:Default),+> Default for ($($T,)+) {
186 fn default() -> ($($T,)+) {
187 ($({ let x: $T = Default::default(); x},)+)
194 // Constructs an expression that performs a lexical ordering using method $rel.
195 // The values are interleaved, so the macro invocation for
196 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
197 // a3, b3)` (and similarly for `lexical_cmp`)
198 macro_rules! lexical_ord {
199 ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
200 if $a != $b { lexical_ord!($rel, $a, $b) }
201 else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
203 ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
206 macro_rules! lexical_partial_cmp {
207 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
208 match ($a).partial_cmp(&$b) {
209 Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
213 ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
216 macro_rules! lexical_cmp {
217 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
218 match ($a).cmp(&$b) {
219 Equal => lexical_cmp!($($rest_a, $rest_b),+),
223 ($a:expr, $b:expr) => { ($a).cmp(&$b) };
228 (val0, ref0, mut0, 0) -> A
231 (val0, ref0, mut0, 0) -> A
232 (val1, ref1, mut1, 1) -> B
235 (val0, ref0, mut0, 0) -> A
236 (val1, ref1, mut1, 1) -> B
237 (val2, ref2, mut2, 2) -> C
240 (val0, ref0, mut0, 0) -> A
241 (val1, ref1, mut1, 1) -> B
242 (val2, ref2, mut2, 2) -> C
243 (val3, ref3, mut3, 3) -> D
246 (val0, ref0, mut0, 0) -> A
247 (val1, ref1, mut1, 1) -> B
248 (val2, ref2, mut2, 2) -> C
249 (val3, ref3, mut3, 3) -> D
250 (val4, ref4, mut4, 4) -> E
253 (val0, ref0, mut0, 0) -> A
254 (val1, ref1, mut1, 1) -> B
255 (val2, ref2, mut2, 2) -> C
256 (val3, ref3, mut3, 3) -> D
257 (val4, ref4, mut4, 4) -> E
258 (val5, ref5, mut5, 5) -> F
261 (val0, ref0, mut0, 0) -> A
262 (val1, ref1, mut1, 1) -> B
263 (val2, ref2, mut2, 2) -> C
264 (val3, ref3, mut3, 3) -> D
265 (val4, ref4, mut4, 4) -> E
266 (val5, ref5, mut5, 5) -> F
267 (val6, ref6, mut6, 6) -> G
270 (val0, ref0, mut0, 0) -> A
271 (val1, ref1, mut1, 1) -> B
272 (val2, ref2, mut2, 2) -> C
273 (val3, ref3, mut3, 3) -> D
274 (val4, ref4, mut4, 4) -> E
275 (val5, ref5, mut5, 5) -> F
276 (val6, ref6, mut6, 6) -> G
277 (val7, ref7, mut7, 7) -> H
280 (val0, ref0, mut0, 0) -> A
281 (val1, ref1, mut1, 1) -> B
282 (val2, ref2, mut2, 2) -> C
283 (val3, ref3, mut3, 3) -> D
284 (val4, ref4, mut4, 4) -> E
285 (val5, ref5, mut5, 5) -> F
286 (val6, ref6, mut6, 6) -> G
287 (val7, ref7, mut7, 7) -> H
288 (val8, ref8, mut8, 8) -> I
291 (val0, ref0, mut0, 0) -> A
292 (val1, ref1, mut1, 1) -> B
293 (val2, ref2, mut2, 2) -> C
294 (val3, ref3, mut3, 3) -> D
295 (val4, ref4, mut4, 4) -> E
296 (val5, ref5, mut5, 5) -> F
297 (val6, ref6, mut6, 6) -> G
298 (val7, ref7, mut7, 7) -> H
299 (val8, ref8, mut8, 8) -> I
300 (val9, ref9, mut9, 9) -> J
303 (val0, ref0, mut0, 0) -> A
304 (val1, ref1, mut1, 1) -> B
305 (val2, ref2, mut2, 2) -> C
306 (val3, ref3, mut3, 3) -> D
307 (val4, ref4, mut4, 4) -> E
308 (val5, ref5, mut5, 5) -> F
309 (val6, ref6, mut6, 6) -> G
310 (val7, ref7, mut7, 7) -> H
311 (val8, ref8, mut8, 8) -> I
312 (val9, ref9, mut9, 9) -> J
313 (val10, ref10, mut10, 10) -> K
316 (val0, ref0, mut0, 0) -> A
317 (val1, ref1, mut1, 1) -> B
318 (val2, ref2, mut2, 2) -> C
319 (val3, ref3, mut3, 3) -> D
320 (val4, ref4, mut4, 4) -> E
321 (val5, ref5, mut5, 5) -> F
322 (val6, ref6, mut6, 6) -> G
323 (val7, ref7, mut7, 7) -> H
324 (val8, ref8, mut8, 8) -> I
325 (val9, ref9, mut9, 9) -> J
326 (val10, ref10, mut10, 10) -> K
327 (val11, ref11, mut11, 11) -> L