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