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 #[allow(missing_doc)];
17 pub use self::inner::*;
19 /// Method extensions to pairs where both types satisfy the `Clone` bound
20 pub trait CopyableTuple<T, U> {
21 /// Return the first element of self
23 /// Return the second element of self
24 fn second(&self) -> U;
25 /// Return the results of swapping the two elements of self
26 fn swap(&self) -> (U, T);
29 impl<T:Clone,U:Clone> CopyableTuple<T, U> for (T, U) {
30 /// Return the first element of self
32 fn first(&self) -> T {
34 (ref t, _) => (*t).clone(),
38 /// Return the second element of self
40 fn second(&self) -> U {
42 (_, ref u) => (*u).clone(),
46 /// Return the results of swapping the two elements of self
48 fn swap(&self) -> (U, T) {
49 match (*self).clone() {
55 /// Method extensions for pairs where the types don't necessarily satisfy the
57 pub trait ImmutableTuple<T, U> {
58 /// Return a reference to the first element of self
59 fn first_ref<'a>(&'a self) -> &'a T;
60 /// Return a reference to the second element of self
61 fn second_ref<'a>(&'a self) -> &'a U;
64 impl<T, U> ImmutableTuple<T, U> for (T, U) {
66 fn first_ref<'a>(&'a self) -> &'a T {
72 fn second_ref<'a>(&'a self) -> &'a U {
79 // macro for implementing n-ary tuple functions and operations
81 macro_rules! tuple_impls {
83 ($move_trait:ident, $immutable_trait:ident) {
84 $(($get_fn:ident, $get_ref_fn:ident) -> $T:ident {
85 $move_pattern:pat, $ref_pattern:pat => $ret:expr
91 #[cfg(not(test))] use cmp::*;
92 #[cfg(not(test))] use default::Default;
93 #[cfg(not(test))] use num::Zero;
96 pub trait $move_trait<$($T),+> {
97 $(fn $get_fn(self) -> $T;)+
100 impl<$($T),+> $move_trait<$($T),+> for ($($T,)+) {
103 fn $get_fn(self) -> $T {
104 let $move_pattern = self;
110 pub trait $immutable_trait<$($T),+> {
111 $(fn $get_ref_fn<'a>(&'a self) -> &'a $T;)+
114 impl<$($T),+> $immutable_trait<$($T),+> for ($($T,)+) {
117 fn $get_ref_fn<'a>(&'a self) -> &'a $T {
118 let $ref_pattern = *self;
124 impl<$($T:Clone),+> Clone for ($($T,)+) {
125 fn clone(&self) -> ($($T,)+) {
126 ($(self.$get_ref_fn().clone(),)+)
131 impl<$($T:Eq),+> Eq for ($($T,)+) {
133 fn eq(&self, other: &($($T,)+)) -> bool {
134 $(*self.$get_ref_fn() == *other.$get_ref_fn())&&+
137 fn ne(&self, other: &($($T,)+)) -> bool {
138 $(*self.$get_ref_fn() != *other.$get_ref_fn())||+
143 impl<$($T:TotalEq),+> TotalEq for ($($T,)+) {
145 fn equals(&self, other: &($($T,)+)) -> bool {
146 $(self.$get_ref_fn().equals(other.$get_ref_fn()))&&+
151 impl<$($T:Ord + Eq),+> Ord for ($($T,)+) {
153 fn lt(&self, other: &($($T,)+)) -> bool {
154 lexical_ord!(lt, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
157 fn le(&self, other: &($($T,)+)) -> bool {
158 lexical_ord!(le, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
161 fn ge(&self, other: &($($T,)+)) -> bool {
162 lexical_ord!(ge, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
165 fn gt(&self, other: &($($T,)+)) -> bool {
166 lexical_ord!(gt, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
171 impl<$($T:TotalOrd),+> TotalOrd for ($($T,)+) {
173 fn cmp(&self, other: &($($T,)+)) -> Ordering {
174 lexical_cmp!($(self.$get_ref_fn(), other.$get_ref_fn()),+)
179 impl<$($T:Default),+> Default for ($($T,)+) {
181 fn default() -> ($($T,)+) {
182 ($({ let x: $T = Default::default(); x},)+)
187 impl<$($T:Zero),+> Zero for ($($T,)+) {
189 fn zero() -> ($($T,)+) {
190 ($({ let x: $T = Zero::zero(); x},)+)
193 fn is_zero(&self) -> bool {
194 $(self.$get_ref_fn().is_zero())&&+
202 // Constructs an expression that performs a lexical ordering using method $rel.
203 // The values are interleaved, so the macro invocation for
204 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
205 // a3, b3)` (and similarly for `lexical_cmp`)
206 macro_rules! lexical_ord {
207 ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
208 if *$a != *$b { lexical_ord!($rel, $a, $b) }
209 else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
211 ($rel: ident, $a:expr, $b:expr) => { (*$a) . $rel ($b) };
214 macro_rules! lexical_cmp {
215 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
217 Equal => lexical_cmp!($($rest_a, $rest_b),+),
221 ($a:expr, $b:expr) => { ($a).cmp($b) };
226 (Tuple1, ImmutableTuple1) {
227 (n0, n0_ref) -> A { (a,), (ref a,) => a }
230 (Tuple2, ImmutableTuple2) {
231 (n0, n0_ref) -> A { (a,_), (ref a,_) => a }
232 (n1, n1_ref) -> B { (_,b), (_,ref b) => b }
235 (Tuple3, ImmutableTuple3) {
236 (n0, n0_ref) -> A { (a,_,_), (ref a,_,_) => a }
237 (n1, n1_ref) -> B { (_,b,_), (_,ref b,_) => b }
238 (n2, n2_ref) -> C { (_,_,c), (_,_,ref c) => c }
241 (Tuple4, ImmutableTuple4) {
242 (n0, n0_ref) -> A { (a,_,_,_), (ref a,_,_,_) => a }
243 (n1, n1_ref) -> B { (_,b,_,_), (_,ref b,_,_) => b }
244 (n2, n2_ref) -> C { (_,_,c,_), (_,_,ref c,_) => c }
245 (n3, n3_ref) -> D { (_,_,_,d), (_,_,_,ref d) => d }
248 (Tuple5, ImmutableTuple5) {
249 (n0, n0_ref) -> A { (a,_,_,_,_), (ref a,_,_,_,_) => a }
250 (n1, n1_ref) -> B { (_,b,_,_,_), (_,ref b,_,_,_) => b }
251 (n2, n2_ref) -> C { (_,_,c,_,_), (_,_,ref c,_,_) => c }
252 (n3, n3_ref) -> D { (_,_,_,d,_), (_,_,_,ref d,_) => d }
253 (n4, n4_ref) -> E { (_,_,_,_,e), (_,_,_,_,ref e) => e }
256 (Tuple6, ImmutableTuple6) {
257 (n0, n0_ref) -> A { (a,_,_,_,_,_), (ref a,_,_,_,_,_) => a }
258 (n1, n1_ref) -> B { (_,b,_,_,_,_), (_,ref b,_,_,_,_) => b }
259 (n2, n2_ref) -> C { (_,_,c,_,_,_), (_,_,ref c,_,_,_) => c }
260 (n3, n3_ref) -> D { (_,_,_,d,_,_), (_,_,_,ref d,_,_) => d }
261 (n4, n4_ref) -> E { (_,_,_,_,e,_), (_,_,_,_,ref e,_) => e }
262 (n5, n5_ref) -> F { (_,_,_,_,_,f), (_,_,_,_,_,ref f) => f }
265 (Tuple7, ImmutableTuple7) {
266 (n0, n0_ref) -> A { (a,_,_,_,_,_,_), (ref a,_,_,_,_,_,_) => a }
267 (n1, n1_ref) -> B { (_,b,_,_,_,_,_), (_,ref b,_,_,_,_,_) => b }
268 (n2, n2_ref) -> C { (_,_,c,_,_,_,_), (_,_,ref c,_,_,_,_) => c }
269 (n3, n3_ref) -> D { (_,_,_,d,_,_,_), (_,_,_,ref d,_,_,_) => d }
270 (n4, n4_ref) -> E { (_,_,_,_,e,_,_), (_,_,_,_,ref e,_,_) => e }
271 (n5, n5_ref) -> F { (_,_,_,_,_,f,_), (_,_,_,_,_,ref f,_) => f }
272 (n6, n6_ref) -> G { (_,_,_,_,_,_,g), (_,_,_,_,_,_,ref g) => g }
275 (Tuple8, ImmutableTuple8) {
276 (n0, n0_ref) -> A { (a,_,_,_,_,_,_,_), (ref a,_,_,_,_,_,_,_) => a }
277 (n1, n1_ref) -> B { (_,b,_,_,_,_,_,_), (_,ref b,_,_,_,_,_,_) => b }
278 (n2, n2_ref) -> C { (_,_,c,_,_,_,_,_), (_,_,ref c,_,_,_,_,_) => c }
279 (n3, n3_ref) -> D { (_,_,_,d,_,_,_,_), (_,_,_,ref d,_,_,_,_) => d }
280 (n4, n4_ref) -> E { (_,_,_,_,e,_,_,_), (_,_,_,_,ref e,_,_,_) => e }
281 (n5, n5_ref) -> F { (_,_,_,_,_,f,_,_), (_,_,_,_,_,ref f,_,_) => f }
282 (n6, n6_ref) -> G { (_,_,_,_,_,_,g,_), (_,_,_,_,_,_,ref g,_) => g }
283 (n7, n7_ref) -> H { (_,_,_,_,_,_,_,h), (_,_,_,_,_,_,_,ref h) => h }
286 (Tuple9, ImmutableTuple9) {
287 (n0, n0_ref) -> A { (a,_,_,_,_,_,_,_,_), (ref a,_,_,_,_,_,_,_,_) => a }
288 (n1, n1_ref) -> B { (_,b,_,_,_,_,_,_,_), (_,ref b,_,_,_,_,_,_,_) => b }
289 (n2, n2_ref) -> C { (_,_,c,_,_,_,_,_,_), (_,_,ref c,_,_,_,_,_,_) => c }
290 (n3, n3_ref) -> D { (_,_,_,d,_,_,_,_,_), (_,_,_,ref d,_,_,_,_,_) => d }
291 (n4, n4_ref) -> E { (_,_,_,_,e,_,_,_,_), (_,_,_,_,ref e,_,_,_,_) => e }
292 (n5, n5_ref) -> F { (_,_,_,_,_,f,_,_,_), (_,_,_,_,_,ref f,_,_,_) => f }
293 (n6, n6_ref) -> G { (_,_,_,_,_,_,g,_,_), (_,_,_,_,_,_,ref g,_,_) => g }
294 (n7, n7_ref) -> H { (_,_,_,_,_,_,_,h,_), (_,_,_,_,_,_,_,ref h,_) => h }
295 (n8, n8_ref) -> I { (_,_,_,_,_,_,_,_,i), (_,_,_,_,_,_,_,_,ref i) => i }
298 (Tuple10, ImmutableTuple10) {
299 (n0, n0_ref) -> A { (a,_,_,_,_,_,_,_,_,_), (ref a,_,_,_,_,_,_,_,_,_) => a }
300 (n1, n1_ref) -> B { (_,b,_,_,_,_,_,_,_,_), (_,ref b,_,_,_,_,_,_,_,_) => b }
301 (n2, n2_ref) -> C { (_,_,c,_,_,_,_,_,_,_), (_,_,ref c,_,_,_,_,_,_,_) => c }
302 (n3, n3_ref) -> D { (_,_,_,d,_,_,_,_,_,_), (_,_,_,ref d,_,_,_,_,_,_) => d }
303 (n4, n4_ref) -> E { (_,_,_,_,e,_,_,_,_,_), (_,_,_,_,ref e,_,_,_,_,_) => e }
304 (n5, n5_ref) -> F { (_,_,_,_,_,f,_,_,_,_), (_,_,_,_,_,ref f,_,_,_,_) => f }
305 (n6, n6_ref) -> G { (_,_,_,_,_,_,g,_,_,_), (_,_,_,_,_,_,ref g,_,_,_) => g }
306 (n7, n7_ref) -> H { (_,_,_,_,_,_,_,h,_,_), (_,_,_,_,_,_,_,ref h,_,_) => h }
307 (n8, n8_ref) -> I { (_,_,_,_,_,_,_,_,i,_), (_,_,_,_,_,_,_,_,ref i,_) => i }
308 (n9, n9_ref) -> J { (_,_,_,_,_,_,_,_,_,j), (_,_,_,_,_,_,_,_,_,ref j) => j }
311 (Tuple11, ImmutableTuple11) {
312 (n0, n0_ref) -> A { (a,_,_,_,_,_,_,_,_,_,_), (ref a,_,_,_,_,_,_,_,_,_,_) => a }
313 (n1, n1_ref) -> B { (_,b,_,_,_,_,_,_,_,_,_), (_,ref b,_,_,_,_,_,_,_,_,_) => b }
314 (n2, n2_ref) -> C { (_,_,c,_,_,_,_,_,_,_,_), (_,_,ref c,_,_,_,_,_,_,_,_) => c }
315 (n3, n3_ref) -> D { (_,_,_,d,_,_,_,_,_,_,_), (_,_,_,ref d,_,_,_,_,_,_,_) => d }
316 (n4, n4_ref) -> E { (_,_,_,_,e,_,_,_,_,_,_), (_,_,_,_,ref e,_,_,_,_,_,_) => e }
317 (n5, n5_ref) -> F { (_,_,_,_,_,f,_,_,_,_,_), (_,_,_,_,_,ref f,_,_,_,_,_) => f }
318 (n6, n6_ref) -> G { (_,_,_,_,_,_,g,_,_,_,_), (_,_,_,_,_,_,ref g,_,_,_,_) => g }
319 (n7, n7_ref) -> H { (_,_,_,_,_,_,_,h,_,_,_), (_,_,_,_,_,_,_,ref h,_,_,_) => h }
320 (n8, n8_ref) -> I { (_,_,_,_,_,_,_,_,i,_,_), (_,_,_,_,_,_,_,_,ref i,_,_) => i }
321 (n9, n9_ref) -> J { (_,_,_,_,_,_,_,_,_,j,_), (_,_,_,_,_,_,_,_,_,ref j,_) => j }
322 (n10, n10_ref) -> K { (_,_,_,_,_,_,_,_,_,_,k), (_,_,_,_,_,_,_,_,_,_,ref k) => k }
325 (Tuple12, ImmutableTuple12) {
326 (n0, n0_ref) -> A { (a,_,_,_,_,_,_,_,_,_,_,_), (ref a,_,_,_,_,_,_,_,_,_,_,_) => a }
327 (n1, n1_ref) -> B { (_,b,_,_,_,_,_,_,_,_,_,_), (_,ref b,_,_,_,_,_,_,_,_,_,_) => b }
328 (n2, n2_ref) -> C { (_,_,c,_,_,_,_,_,_,_,_,_), (_,_,ref c,_,_,_,_,_,_,_,_,_) => c }
329 (n3, n3_ref) -> D { (_,_,_,d,_,_,_,_,_,_,_,_), (_,_,_,ref d,_,_,_,_,_,_,_,_) => d }
330 (n4, n4_ref) -> E { (_,_,_,_,e,_,_,_,_,_,_,_), (_,_,_,_,ref e,_,_,_,_,_,_,_) => e }
331 (n5, n5_ref) -> F { (_,_,_,_,_,f,_,_,_,_,_,_), (_,_,_,_,_,ref f,_,_,_,_,_,_) => f }
332 (n6, n6_ref) -> G { (_,_,_,_,_,_,g,_,_,_,_,_), (_,_,_,_,_,_,ref g,_,_,_,_,_) => g }
333 (n7, n7_ref) -> H { (_,_,_,_,_,_,_,h,_,_,_,_), (_,_,_,_,_,_,_,ref h,_,_,_,_) => h }
334 (n8, n8_ref) -> I { (_,_,_,_,_,_,_,_,i,_,_,_), (_,_,_,_,_,_,_,_,ref i,_,_,_) => i }
335 (n9, n9_ref) -> J { (_,_,_,_,_,_,_,_,_,j,_,_), (_,_,_,_,_,_,_,_,_,ref j,_,_) => j }
336 (n10, n10_ref) -> K { (_,_,_,_,_,_,_,_,_,_,k,_), (_,_,_,_,_,_,_,_,_,_,ref k,_) => k }
337 (n11, n11_ref) -> L { (_,_,_,_,_,_,_,_,_,_,_,l), (_,_,_,_,_,_,_,_,_,_,_,ref l) => l }
348 fn test_tuple_ref() {
349 let x = (~"foo", ~"bar");
350 assert_eq!(x.first_ref(), &~"foo");
351 assert_eq!(x.second_ref(), &~"bar");
356 assert_eq!((948, 4039.48).first(), 948);
357 assert_eq!((34.5, ~"foo").second(), ~"foo");
358 assert_eq!(('a', 2).swap(), (2, 'a'));
365 assert_eq!(a.first(), b.first());
366 assert_eq!(a.second(), b.second());
371 let t = (0u8, 1u16, 2u32, 3u64, 4u, 5i8, 6i16, 7i32, 8i64, 9i, 10f32, 11f64);
372 assert_eq!(t.n0(), 0u8);
373 assert_eq!(t.n1(), 1u16);
374 assert_eq!(t.n2(), 2u32);
375 assert_eq!(t.n3(), 3u64);
376 assert_eq!(t.n4(), 4u);
377 assert_eq!(t.n5(), 5i8);
378 assert_eq!(t.n6(), 6i16);
379 assert_eq!(t.n7(), 7i32);
380 assert_eq!(t.n8(), 8i64);
381 assert_eq!(t.n9(), 9i);
382 assert_eq!(t.n10(), 10f32);
383 assert_eq!(t.n11(), 11f64);
385 assert_eq!(t.n0_ref(), &0u8);
386 assert_eq!(t.n1_ref(), &1u16);
387 assert_eq!(t.n2_ref(), &2u32);
388 assert_eq!(t.n3_ref(), &3u64);
389 assert_eq!(t.n4_ref(), &4u);
390 assert_eq!(t.n5_ref(), &5i8);
391 assert_eq!(t.n6_ref(), &6i16);
392 assert_eq!(t.n7_ref(), &7i32);
393 assert_eq!(t.n8_ref(), &8i64);
394 assert_eq!(t.n9_ref(), &9i);
395 assert_eq!(t.n10_ref(), &10f32);
396 assert_eq!(t.n11_ref(), &11f64);
400 fn test_tuple_cmp() {
401 let (small, big) = ((1u, 2u, 3u), (3u, 2u, 1u));
406 assert_eq!(small, small);
407 assert_eq!(big, big);
408 assert!(small != big);
409 assert!(big != small);
412 assert!(small < big);
413 assert!(!(small < small));
414 assert!(!(big < small));
415 assert!(!(big < big));
417 assert!(small <= small);
420 assert!(big > small);
421 assert!(small >= small);
422 assert!(big >= small);
425 assert!(!((1.0, 2.0) < (nan, 3.0)));
426 assert!(!((1.0, 2.0) <= (nan, 3.0)));
427 assert!(!((1.0, 2.0) > (nan, 3.0)));
428 assert!(!((1.0, 2.0) >= (nan, 3.0)));
429 assert!(((1.0, 2.0) < (2.0, nan)));
430 assert!(!((2.0, 2.0) < (2.0, nan)));
433 assert!(small.equals(&small));
434 assert!(big.equals(&big));
435 assert!(!small.equals(&big));
436 assert!(!big.equals(&small));
439 assert_eq!(small.cmp(&small), Equal);
440 assert_eq!(big.cmp(&big), Equal);
441 assert_eq!(small.cmp(&big), Less);
442 assert_eq!(big.cmp(&small), Greater);