]> git.lizzy.rs Git - rust.git/blob - src/libstd/tuple.rs
auto merge of #10519 : nikomatsakis/rust/issue-8624-borrowck-overly-permissive, r...
[rust.git] / src / libstd / 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 #[allow(missing_doc)];
14
15 use clone::Clone;
16
17 pub use self::inner::*;
18
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
22     fn first(&self) -> T;
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);
27 }
28
29 impl<T:Clone,U:Clone> CopyableTuple<T, U> for (T, U) {
30     /// Return the first element of self
31     #[inline]
32     fn first(&self) -> T {
33         match *self {
34             (ref t, _) => (*t).clone(),
35         }
36     }
37
38     /// Return the second element of self
39     #[inline]
40     fn second(&self) -> U {
41         match *self {
42             (_, ref u) => (*u).clone(),
43         }
44     }
45
46     /// Return the results of swapping the two elements of self
47     #[inline]
48     fn swap(&self) -> (U, T) {
49         match (*self).clone() {
50             (t, u) => (u, t),
51         }
52     }
53 }
54
55 /// Method extensions for pairs where the types don't necessarily satisfy the
56 /// `Clone` bound
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;
62 }
63
64 impl<T, U> ImmutableTuple<T, U> for (T, U) {
65     #[inline]
66     fn first_ref<'a>(&'a self) -> &'a T {
67         match *self {
68             (ref t, _) => t,
69         }
70     }
71     #[inline]
72     fn second_ref<'a>(&'a self) -> &'a U {
73         match *self {
74             (_, ref u) => u,
75         }
76     }
77 }
78
79 // macro for implementing n-ary tuple functions and operations
80
81 macro_rules! tuple_impls {
82     ($(
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
86             })+
87         }
88     )+) => {
89         pub mod inner {
90             use clone::Clone;
91             #[cfg(not(test))] use cmp::*;
92             #[cfg(not(test))] use default::Default;
93             #[cfg(not(test))] use num::Zero;
94
95             $(
96                 pub trait $move_trait<$($T),+> {
97                     $(fn $get_fn(self) -> $T;)+
98                 }
99
100                 impl<$($T),+> $move_trait<$($T),+> for ($($T,)+) {
101                     $(
102                         #[inline]
103                         fn $get_fn(self) -> $T {
104                             let $move_pattern = self;
105                             $ret
106                         }
107                     )+
108                 }
109
110                 pub trait $immutable_trait<$($T),+> {
111                     $(fn $get_ref_fn<'a>(&'a self) -> &'a $T;)+
112                 }
113
114                 impl<$($T),+> $immutable_trait<$($T),+> for ($($T,)+) {
115                     $(
116                         #[inline]
117                         fn $get_ref_fn<'a>(&'a self) -> &'a $T {
118                             let $ref_pattern = *self;
119                             $ret
120                         }
121                     )+
122                 }
123
124                 impl<$($T:Clone),+> Clone for ($($T,)+) {
125                     fn clone(&self) -> ($($T,)+) {
126                         ($(self.$get_ref_fn().clone(),)+)
127                     }
128                 }
129
130                 #[cfg(not(test))]
131                 impl<$($T:Eq),+> Eq for ($($T,)+) {
132                     #[inline]
133                     fn eq(&self, other: &($($T,)+)) -> bool {
134                         $(*self.$get_ref_fn() == *other.$get_ref_fn())&&+
135                     }
136                     #[inline]
137                     fn ne(&self, other: &($($T,)+)) -> bool {
138                         $(*self.$get_ref_fn() != *other.$get_ref_fn())||+
139                     }
140                 }
141
142                 #[cfg(not(test))]
143                 impl<$($T:TotalEq),+> TotalEq for ($($T,)+) {
144                     #[inline]
145                     fn equals(&self, other: &($($T,)+)) -> bool {
146                         $(self.$get_ref_fn().equals(other.$get_ref_fn()))&&+
147                     }
148                 }
149
150                 #[cfg(not(test))]
151                 impl<$($T:Ord + Eq),+> Ord for ($($T,)+) {
152                     #[inline]
153                     fn lt(&self, other: &($($T,)+)) -> bool {
154                         lexical_ord!(lt, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
155                     }
156                     #[inline]
157                     fn le(&self, other: &($($T,)+)) -> bool {
158                         lexical_ord!(le, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
159                     }
160                     #[inline]
161                     fn ge(&self, other: &($($T,)+)) -> bool {
162                         lexical_ord!(ge, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
163                     }
164                     #[inline]
165                     fn gt(&self, other: &($($T,)+)) -> bool {
166                         lexical_ord!(gt, $(self.$get_ref_fn(), other.$get_ref_fn()),+)
167                     }
168                 }
169
170                 #[cfg(not(test))]
171                 impl<$($T:TotalOrd),+> TotalOrd for ($($T,)+) {
172                     #[inline]
173                     fn cmp(&self, other: &($($T,)+)) -> Ordering {
174                         lexical_cmp!($(self.$get_ref_fn(), other.$get_ref_fn()),+)
175                     }
176                 }
177
178                 #[cfg(not(test))]
179                 impl<$($T:Default),+> Default for ($($T,)+) {
180                     #[inline]
181                     fn default() -> ($($T,)+) {
182                         ($({ let x: $T = Default::default(); x},)+)
183                     }
184                 }
185
186                 #[cfg(not(test))]
187                 impl<$($T:Zero),+> Zero for ($($T,)+) {
188                     #[inline]
189                     fn zero() -> ($($T,)+) {
190                         ($({ let x: $T = Zero::zero(); x},)+)
191                     }
192                     #[inline]
193                     fn is_zero(&self) -> bool {
194                         $(self.$get_ref_fn().is_zero())&&+
195                     }
196                 }
197             )+
198         }
199     }
200 }
201
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),+) }
210     };
211     ($rel: ident, $a:expr, $b:expr) => { (*$a) . $rel ($b) };
212 }
213
214 macro_rules! lexical_cmp {
215     ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
216         match ($a).cmp($b) {
217             Equal => lexical_cmp!($($rest_a, $rest_b),+),
218             ordering   => ordering
219         }
220     };
221     ($a:expr, $b:expr) => { ($a).cmp($b) };
222 }
223
224
225 tuple_impls! {
226     (Tuple1, ImmutableTuple1) {
227         (n0, n0_ref) -> A { (a,), (ref a,) => a }
228     }
229
230     (Tuple2, ImmutableTuple2) {
231         (n0, n0_ref) -> A { (a,_), (ref a,_) => a }
232         (n1, n1_ref) -> B { (_,b), (_,ref b) => b }
233     }
234
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 }
239     }
240
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 }
246     }
247
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 }
254     }
255
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 }
263     }
264
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 }
273     }
274
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 }
284     }
285
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 }
296     }
297
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 }
309     }
310
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 }
323     }
324
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 }
338     }
339 }
340
341 #[cfg(test)]
342 mod tests {
343     use super::*;
344     use clone::Clone;
345     use cmp::*;
346
347     #[test]
348     fn test_tuple_ref() {
349         let x = (~"foo", ~"bar");
350         assert_eq!(x.first_ref(), &~"foo");
351         assert_eq!(x.second_ref(), &~"bar");
352     }
353
354     #[test]
355     fn test_tuple() {
356         assert_eq!((948, 4039.48).first(), 948);
357         assert_eq!((34.5, ~"foo").second(), ~"foo");
358         assert_eq!(('a', 2).swap(), (2, 'a'));
359     }
360
361     #[test]
362     fn test_clone() {
363         let a = (1, ~"2");
364         let b = a.clone();
365         assert_eq!(a.first(), b.first());
366         assert_eq!(a.second(), b.second());
367     }
368
369     #[test]
370     fn test_n_tuple() {
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);
384
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);
397     }
398
399     #[test]
400     fn test_tuple_cmp() {
401         let (small, big) = ((1u, 2u, 3u), (3u, 2u, 1u));
402
403         let nan = 0.0/0.0;
404
405         // Eq
406         assert_eq!(small, small);
407         assert_eq!(big, big);
408         assert!(small != big);
409         assert!(big != small);
410
411         // Ord
412         assert!(small < big);
413         assert!(!(small < small));
414         assert!(!(big < small));
415         assert!(!(big < big));
416
417         assert!(small <= small);
418         assert!(big <= big);
419
420         assert!(big > small);
421         assert!(small >= small);
422         assert!(big >= small);
423         assert!(big >= big);
424
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)));
431
432         // TotalEq
433         assert!(small.equals(&small));
434         assert!(big.equals(&big));
435         assert!(!small.equals(&big));
436         assert!(!big.equals(&small));
437
438         // TotalOrd
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);
443     }
444 }