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