]> git.lizzy.rs Git - rust.git/blob - src/libcore/tuple.rs
Fixes issue #43205: ICE in Rvalue::Len evaluation.
[rust.git] / src / libcore / 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 // See src/libstd/primitive_docs.rs for documentation.
12
13 use cmp::*;
14 use cmp::Ordering::*;
15
16 // macro for implementing n-ary tuple functions and operations
17 macro_rules! tuple_impls {
18     ($(
19         $Tuple:ident {
20             $(($idx:tt) -> $T:ident)+
21         }
22     )+) => {
23         $(
24             #[stable(feature = "rust1", since = "1.0.0")]
25             #[cfg(stage0)]
26             impl<$($T:Clone),+> Clone for ($($T,)+) {
27                 fn clone(&self) -> ($($T,)+) {
28                     ($(self.$idx.clone(),)+)
29                 }
30             }
31
32             #[stable(feature = "rust1", since = "1.0.0")]
33             impl<$($T:PartialEq),+> PartialEq for ($($T,)+) where last_type!($($T,)+): ?Sized {
34                 #[inline]
35                 fn eq(&self, other: &($($T,)+)) -> bool {
36                     $(self.$idx == other.$idx)&&+
37                 }
38                 #[inline]
39                 fn ne(&self, other: &($($T,)+)) -> bool {
40                     $(self.$idx != other.$idx)||+
41                 }
42             }
43
44             #[stable(feature = "rust1", since = "1.0.0")]
45             impl<$($T:Eq),+> Eq for ($($T,)+) where last_type!($($T,)+): ?Sized {}
46
47             #[stable(feature = "rust1", since = "1.0.0")]
48             impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+)
49                     where last_type!($($T,)+): ?Sized {
50                 #[inline]
51                 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
52                     lexical_partial_cmp!($(self.$idx, other.$idx),+)
53                 }
54                 #[inline]
55                 fn lt(&self, other: &($($T,)+)) -> bool {
56                     lexical_ord!(lt, $(self.$idx, other.$idx),+)
57                 }
58                 #[inline]
59                 fn le(&self, other: &($($T,)+)) -> bool {
60                     lexical_ord!(le, $(self.$idx, other.$idx),+)
61                 }
62                 #[inline]
63                 fn ge(&self, other: &($($T,)+)) -> bool {
64                     lexical_ord!(ge, $(self.$idx, other.$idx),+)
65                 }
66                 #[inline]
67                 fn gt(&self, other: &($($T,)+)) -> bool {
68                     lexical_ord!(gt, $(self.$idx, other.$idx),+)
69                 }
70             }
71
72             #[stable(feature = "rust1", since = "1.0.0")]
73             impl<$($T:Ord),+> Ord for ($($T,)+) where last_type!($($T,)+): ?Sized {
74                 #[inline]
75                 fn cmp(&self, other: &($($T,)+)) -> Ordering {
76                     lexical_cmp!($(self.$idx, other.$idx),+)
77                 }
78             }
79
80             #[stable(feature = "rust1", since = "1.0.0")]
81             impl<$($T:Default),+> Default for ($($T,)+) {
82                 #[inline]
83                 fn default() -> ($($T,)+) {
84                     ($({ let x: $T = Default::default(); x},)+)
85                 }
86             }
87         )+
88     }
89 }
90
91 // Constructs an expression that performs a lexical ordering using method $rel.
92 // The values are interleaved, so the macro invocation for
93 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
94 // a3, b3)` (and similarly for `lexical_cmp`)
95 macro_rules! lexical_ord {
96     ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
97         if $a != $b { lexical_ord!($rel, $a, $b) }
98         else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
99     };
100     ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
101 }
102
103 macro_rules! lexical_partial_cmp {
104     ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
105         match ($a).partial_cmp(&$b) {
106             Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
107             ordering   => ordering
108         }
109     };
110     ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
111 }
112
113 macro_rules! lexical_cmp {
114     ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
115         match ($a).cmp(&$b) {
116             Equal => lexical_cmp!($($rest_a, $rest_b),+),
117             ordering   => ordering
118         }
119     };
120     ($a:expr, $b:expr) => { ($a).cmp(&$b) };
121 }
122
123 macro_rules! last_type {
124     ($a:ident,) => { $a };
125     ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
126 }
127
128 tuple_impls! {
129     Tuple1 {
130         (0) -> A
131     }
132     Tuple2 {
133         (0) -> A
134         (1) -> B
135     }
136     Tuple3 {
137         (0) -> A
138         (1) -> B
139         (2) -> C
140     }
141     Tuple4 {
142         (0) -> A
143         (1) -> B
144         (2) -> C
145         (3) -> D
146     }
147     Tuple5 {
148         (0) -> A
149         (1) -> B
150         (2) -> C
151         (3) -> D
152         (4) -> E
153     }
154     Tuple6 {
155         (0) -> A
156         (1) -> B
157         (2) -> C
158         (3) -> D
159         (4) -> E
160         (5) -> F
161     }
162     Tuple7 {
163         (0) -> A
164         (1) -> B
165         (2) -> C
166         (3) -> D
167         (4) -> E
168         (5) -> F
169         (6) -> G
170     }
171     Tuple8 {
172         (0) -> A
173         (1) -> B
174         (2) -> C
175         (3) -> D
176         (4) -> E
177         (5) -> F
178         (6) -> G
179         (7) -> H
180     }
181     Tuple9 {
182         (0) -> A
183         (1) -> B
184         (2) -> C
185         (3) -> D
186         (4) -> E
187         (5) -> F
188         (6) -> G
189         (7) -> H
190         (8) -> I
191     }
192     Tuple10 {
193         (0) -> A
194         (1) -> B
195         (2) -> C
196         (3) -> D
197         (4) -> E
198         (5) -> F
199         (6) -> G
200         (7) -> H
201         (8) -> I
202         (9) -> J
203     }
204     Tuple11 {
205         (0) -> A
206         (1) -> B
207         (2) -> C
208         (3) -> D
209         (4) -> E
210         (5) -> F
211         (6) -> G
212         (7) -> H
213         (8) -> I
214         (9) -> J
215         (10) -> K
216     }
217     Tuple12 {
218         (0) -> A
219         (1) -> B
220         (2) -> C
221         (3) -> D
222         (4) -> E
223         (5) -> F
224         (6) -> G
225         (7) -> H
226         (8) -> I
227         (9) -> J
228         (10) -> K
229         (11) -> L
230     }
231 }