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 ($cloneable_trait:ident, $immutable_trait:ident) {
84 $(($get_fn:ident, $get_ref_fn:ident) -> $T:ident {
85 $get_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 $cloneable_trait<$($T),+> {
97 $(fn $get_fn(&self) -> $T;)+
100 impl<$($T:Clone),+> $cloneable_trait<$($T),+> for ($($T,)+) {
103 fn $get_fn(&self) -> $T {
104 self.$get_ref_fn().clone()
109 pub trait $immutable_trait<$($T),+> {
110 $(fn $get_ref_fn<'a>(&'a self) -> &'a $T;)+
113 impl<$($T),+> $immutable_trait<$($T),+> for ($($T,)+) {
116 fn $get_ref_fn<'a>(&'a self) -> &'a $T {
117 match *self { $get_pattern => $ret }
122 impl<$($T:Clone),+> Clone for ($($T,)+) {
123 fn clone(&self) -> ($($T,)+) {
124 ($(self.$get_ref_fn().clone(),)+)
129 impl<$($T:Eq),+> Eq for ($($T,)+) {
131 fn eq(&self, other: &($($T,)+)) -> bool {
132 $(*self.$get_ref_fn() == *other.$get_ref_fn())&&+
135 fn ne(&self, other: &($($T,)+)) -> bool {
136 $(*self.$get_ref_fn() != *other.$get_ref_fn())||+
141 impl<$($T:TotalEq),+> TotalEq for ($($T,)+) {
143 fn equals(&self, other: &($($T,)+)) -> bool {
144 $(self.$get_ref_fn().equals(other.$get_ref_fn()))&&+
149 impl<$($T:Ord + Eq),+> Ord for ($($T,)+) {
151 fn lt(&self, other: &($($T,)+)) -> bool {
152 lexical_ord!(lt, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
155 fn le(&self, other: &($($T,)+)) -> bool {
156 lexical_ord!(le, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
159 fn ge(&self, other: &($($T,)+)) -> bool {
160 lexical_ord!(ge, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
163 fn gt(&self, other: &($($T,)+)) -> bool {
164 lexical_ord!(gt, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
169 impl<$($T:TotalOrd),+> TotalOrd for ($($T,)+) {
171 fn cmp(&self, other: &($($T,)+)) -> Ordering {
172 lexical_cmp!($(self.$get_ref_fn(), other.$get_ref_fn()),+)
177 impl<$($T:Default),+> Default for ($($T,)+) {
179 fn default() -> ($($T,)+) {
180 ($({ let x: $T = Default::default(); x},)+)
185 impl<$($T:Zero),+> Zero for ($($T,)+) {
187 fn zero() -> ($($T,)+) {
188 ($({ let x: $T = Zero::zero(); x},)+)
191 fn is_zero(&self) -> bool {
192 $(self.$get_ref_fn().is_zero())&&+
200 // Constructs an expression that performs a lexical ordering using method $rel.
201 // The values are interleaved, so the macro invocation for
202 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
203 // a3, b3)` (and similarly for `lexical_cmp`)
204 macro_rules! lexical_ord {
205 ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
206 if *$a != *$b { lexical_ord!($rel, $a, $b) }
207 else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
209 ($rel: ident, $a:expr, $b:expr) => { (*$a) . $rel ($b) };
212 macro_rules! lexical_cmp {
213 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
215 Equal => lexical_cmp!($($rest_a, $rest_b),+),
219 ($a:expr, $b:expr) => { ($a).cmp($b) };
224 (CloneableTuple1, ImmutableTuple1) {
225 (n0, n0_ref) -> A { (ref a,) => a }
228 (CloneableTuple2, ImmutableTuple2) {
229 (n0, n0_ref) -> A { (ref a,_) => a }
230 (n1, n1_ref) -> B { (_,ref b) => b }
233 (CloneableTuple3, ImmutableTuple3) {
234 (n0, n0_ref) -> A { (ref a,_,_) => a }
235 (n1, n1_ref) -> B { (_,ref b,_) => b }
236 (n2, n2_ref) -> C { (_,_,ref c) => c }
239 (CloneableTuple4, ImmutableTuple4) {
240 (n0, n0_ref) -> A { (ref a,_,_,_) => a }
241 (n1, n1_ref) -> B { (_,ref b,_,_) => b }
242 (n2, n2_ref) -> C { (_,_,ref c,_) => c }
243 (n3, n3_ref) -> D { (_,_,_,ref d) => d }
246 (CloneableTuple5, ImmutableTuple5) {
247 (n0, n0_ref) -> A { (ref a,_,_,_,_) => a }
248 (n1, n1_ref) -> B { (_,ref b,_,_,_) => b }
249 (n2, n2_ref) -> C { (_,_,ref c,_,_) => c }
250 (n3, n3_ref) -> D { (_,_,_,ref d,_) => d }
251 (n4, n4_ref) -> E { (_,_,_,_,ref e) => e }
254 (CloneableTuple6, ImmutableTuple6) {
255 (n0, n0_ref) -> A { (ref a,_,_,_,_,_) => a }
256 (n1, n1_ref) -> B { (_,ref b,_,_,_,_) => b }
257 (n2, n2_ref) -> C { (_,_,ref c,_,_,_) => c }
258 (n3, n3_ref) -> D { (_,_,_,ref d,_,_) => d }
259 (n4, n4_ref) -> E { (_,_,_,_,ref e,_) => e }
260 (n5, n5_ref) -> F { (_,_,_,_,_,ref f) => f }
263 (CloneableTuple7, ImmutableTuple7) {
264 (n0, n0_ref) -> A { (ref a,_,_,_,_,_,_) => a }
265 (n1, n1_ref) -> B { (_,ref b,_,_,_,_,_) => b }
266 (n2, n2_ref) -> C { (_,_,ref c,_,_,_,_) => c }
267 (n3, n3_ref) -> D { (_,_,_,ref d,_,_,_) => d }
268 (n4, n4_ref) -> E { (_,_,_,_,ref e,_,_) => e }
269 (n5, n5_ref) -> F { (_,_,_,_,_,ref f,_) => f }
270 (n6, n6_ref) -> G { (_,_,_,_,_,_,ref g) => g }
273 (CloneableTuple8, ImmutableTuple8) {
274 (n0, n0_ref) -> A { (ref a,_,_,_,_,_,_,_) => a }
275 (n1, n1_ref) -> B { (_,ref b,_,_,_,_,_,_) => b }
276 (n2, n2_ref) -> C { (_,_,ref c,_,_,_,_,_) => c }
277 (n3, n3_ref) -> D { (_,_,_,ref d,_,_,_,_) => d }
278 (n4, n4_ref) -> E { (_,_,_,_,ref e,_,_,_) => e }
279 (n5, n5_ref) -> F { (_,_,_,_,_,ref f,_,_) => f }
280 (n6, n6_ref) -> G { (_,_,_,_,_,_,ref g,_) => g }
281 (n7, n7_ref) -> H { (_,_,_,_,_,_,_,ref h) => h }
284 (CloneableTuple9, ImmutableTuple9) {
285 (n0, n0_ref) -> A { (ref a,_,_,_,_,_,_,_,_) => a }
286 (n1, n1_ref) -> B { (_,ref b,_,_,_,_,_,_,_) => b }
287 (n2, n2_ref) -> C { (_,_,ref c,_,_,_,_,_,_) => c }
288 (n3, n3_ref) -> D { (_,_,_,ref d,_,_,_,_,_) => d }
289 (n4, n4_ref) -> E { (_,_,_,_,ref e,_,_,_,_) => e }
290 (n5, n5_ref) -> F { (_,_,_,_,_,ref f,_,_,_) => f }
291 (n6, n6_ref) -> G { (_,_,_,_,_,_,ref g,_,_) => g }
292 (n7, n7_ref) -> H { (_,_,_,_,_,_,_,ref h,_) => h }
293 (n8, n8_ref) -> I { (_,_,_,_,_,_,_,_,ref i) => i }
296 (CloneableTuple10, ImmutableTuple10) {
297 (n0, n0_ref) -> A { (ref a,_,_,_,_,_,_,_,_,_) => a }
298 (n1, n1_ref) -> B { (_,ref b,_,_,_,_,_,_,_,_) => b }
299 (n2, n2_ref) -> C { (_,_,ref c,_,_,_,_,_,_,_) => c }
300 (n3, n3_ref) -> D { (_,_,_,ref d,_,_,_,_,_,_) => d }
301 (n4, n4_ref) -> E { (_,_,_,_,ref e,_,_,_,_,_) => e }
302 (n5, n5_ref) -> F { (_,_,_,_,_,ref f,_,_,_,_) => f }
303 (n6, n6_ref) -> G { (_,_,_,_,_,_,ref g,_,_,_) => g }
304 (n7, n7_ref) -> H { (_,_,_,_,_,_,_,ref h,_,_) => h }
305 (n8, n8_ref) -> I { (_,_,_,_,_,_,_,_,ref i,_) => i }
306 (n9, n9_ref) -> J { (_,_,_,_,_,_,_,_,_,ref j) => j }
309 (CloneableTuple11, ImmutableTuple11) {
310 (n0, n0_ref) -> A { (ref a,_,_,_,_,_,_,_,_,_,_) => a }
311 (n1, n1_ref) -> B { (_,ref b,_,_,_,_,_,_,_,_,_) => b }
312 (n2, n2_ref) -> C { (_,_,ref c,_,_,_,_,_,_,_,_) => c }
313 (n3, n3_ref) -> D { (_,_,_,ref d,_,_,_,_,_,_,_) => d }
314 (n4, n4_ref) -> E { (_,_,_,_,ref e,_,_,_,_,_,_) => e }
315 (n5, n5_ref) -> F { (_,_,_,_,_,ref f,_,_,_,_,_) => f }
316 (n6, n6_ref) -> G { (_,_,_,_,_,_,ref g,_,_,_,_) => g }
317 (n7, n7_ref) -> H { (_,_,_,_,_,_,_,ref h,_,_,_) => h }
318 (n8, n8_ref) -> I { (_,_,_,_,_,_,_,_,ref i,_,_) => i }
319 (n9, n9_ref) -> J { (_,_,_,_,_,_,_,_,_,ref j,_) => j }
320 (n10, n10_ref) -> K { (_,_,_,_,_,_,_,_,_,_,ref k) => k }
323 (CloneableTuple12, ImmutableTuple12) {
324 (n0, n0_ref) -> A { (ref a,_,_,_,_,_,_,_,_,_,_,_) => a }
325 (n1, n1_ref) -> B { (_,ref b,_,_,_,_,_,_,_,_,_,_) => b }
326 (n2, n2_ref) -> C { (_,_,ref c,_,_,_,_,_,_,_,_,_) => c }
327 (n3, n3_ref) -> D { (_,_,_,ref d,_,_,_,_,_,_,_,_) => d }
328 (n4, n4_ref) -> E { (_,_,_,_,ref e,_,_,_,_,_,_,_) => e }
329 (n5, n5_ref) -> F { (_,_,_,_,_,ref f,_,_,_,_,_,_) => f }
330 (n6, n6_ref) -> G { (_,_,_,_,_,_,ref g,_,_,_,_,_) => g }
331 (n7, n7_ref) -> H { (_,_,_,_,_,_,_,ref h,_,_,_,_) => h }
332 (n8, n8_ref) -> I { (_,_,_,_,_,_,_,_,ref i,_,_,_) => i }
333 (n9, n9_ref) -> J { (_,_,_,_,_,_,_,_,_,ref j,_,_) => j }
334 (n10, n10_ref) -> K { (_,_,_,_,_,_,_,_,_,_,ref k,_) => k }
335 (n11, n11_ref) -> L { (_,_,_,_,_,_,_,_,_,_,_,ref l) => l }
346 fn test_tuple_ref() {
347 let x = (~"foo", ~"bar");
348 assert_eq!(x.first_ref(), &~"foo");
349 assert_eq!(x.second_ref(), &~"bar");
354 assert_eq!((948, 4039.48).first(), 948);
355 assert_eq!((34.5, ~"foo").second(), ~"foo");
356 assert_eq!(('a', 2).swap(), (2, 'a'));
363 assert_eq!(a.first(), b.first());
364 assert_eq!(a.second(), b.second());
369 let t = (0u8, 1u16, 2u32, 3u64, 4u, 5i8, 6i16, 7i32, 8i64, 9i, 10f32, 11f64);
370 assert_eq!(t.n0(), 0u8);
371 assert_eq!(t.n1(), 1u16);
372 assert_eq!(t.n2(), 2u32);
373 assert_eq!(t.n3(), 3u64);
374 assert_eq!(t.n4(), 4u);
375 assert_eq!(t.n5(), 5i8);
376 assert_eq!(t.n6(), 6i16);
377 assert_eq!(t.n7(), 7i32);
378 assert_eq!(t.n8(), 8i64);
379 assert_eq!(t.n9(), 9i);
380 assert_eq!(t.n10(), 10f32);
381 assert_eq!(t.n11(), 11f64);
383 assert_eq!(t.n0_ref(), &0u8);
384 assert_eq!(t.n1_ref(), &1u16);
385 assert_eq!(t.n2_ref(), &2u32);
386 assert_eq!(t.n3_ref(), &3u64);
387 assert_eq!(t.n4_ref(), &4u);
388 assert_eq!(t.n5_ref(), &5i8);
389 assert_eq!(t.n6_ref(), &6i16);
390 assert_eq!(t.n7_ref(), &7i32);
391 assert_eq!(t.n8_ref(), &8i64);
392 assert_eq!(t.n9_ref(), &9i);
393 assert_eq!(t.n10_ref(), &10f32);
394 assert_eq!(t.n11_ref(), &11f64);
398 fn test_tuple_cmp() {
399 let (small, big) = ((1u, 2u, 3u), (3u, 2u, 1u));
404 assert_eq!(small, small);
405 assert_eq!(big, big);
406 assert!(small != big);
407 assert!(big != small);
410 assert!(small < big);
411 assert!(!(small < small));
412 assert!(!(big < small));
413 assert!(!(big < big));
415 assert!(small <= small);
418 assert!(big > small);
419 assert!(small >= small);
420 assert!(big >= small);
423 assert!(!((1.0, 2.0) < (nan, 3.0)));
424 assert!(!((1.0, 2.0) <= (nan, 3.0)));
425 assert!(!((1.0, 2.0) > (nan, 3.0)));
426 assert!(!((1.0, 2.0) >= (nan, 3.0)));
427 assert!(((1.0, 2.0) < (2.0, nan)));
428 assert!(!((2.0, 2.0) < (2.0, nan)));
431 assert!(small.equals(&small));
432 assert!(big.equals(&big));
433 assert!(!small.equals(&big));
434 assert!(!big.equals(&small));
437 assert_eq!(small.cmp(&small), Equal);
438 assert_eq!(big.cmp(&big), Equal);
439 assert_eq!(small.cmp(&big), Less);
440 assert_eq!(big.cmp(&small), Greater);