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 //! To access a single element of a tuple one can use the following
16 //! * `valN` - returns a value of _N_-th element
17 //! * `refN` - returns a reference to _N_-th element
18 //! * `mutN` - returns a mutable reference to _N_-th element
20 //! Indexing starts from zero, so `val0` returns first value, `val1`
21 //! returns second value, and so on. In general, a tuple with _S_
22 //! elements provides aforementioned methods suffixed with numbers
23 //! from `0` to `S-1`. Traits which contain these methods are
24 //! implemented for tuples with up to 12 elements.
26 //! If every type inside a tuple implements one of the following
27 //! traits, then a tuple itself also implements it.
38 #[unstable = "this is just a documentation module and should not be part \
46 use option::Option::Some;
48 // FIXME(#19630) Remove this work-around
53 // macro for implementing n-ary tuple functions and operations
54 macro_rules! tuple_impls {
57 $(($valN:ident, $refN:ident, $mutN:ident, $idx:tt) -> $T:ident)+
61 #[allow(missing_docs)]
63 pub trait $Tuple<$($T),+> {
65 #[deprecated = "use tuple indexing: `tuple.N`"]
67 #[deprecated = "use tuple indexing: `&tuple.N`"]
68 fn $refN<'a>(&'a self) -> &'a $T;
69 #[deprecated = "use tuple indexing: `&mut tuple.N`"]
70 fn $mutN<'a>(&'a mut self) -> &'a mut $T;
74 impl<$($T),+> $Tuple<$($T),+> for ($($T,)+) {
77 #[allow(unused_variables)]
78 #[deprecated = "use tuple indexing: `tuple.N`"]
79 fn $valN(self) -> $T {
84 #[allow(unused_variables)]
85 #[deprecated = "use tuple indexing: `&tuple.N`"]
86 fn $refN<'a>(&'a self) -> &'a $T {
91 #[allow(unused_variables)]
92 #[deprecated = "use tuple indexing: &mut tuple.N"]
93 fn $mutN<'a>(&'a mut self) -> &'a mut $T {
100 impl<$($T:Clone),+> Clone for ($($T,)+) {
101 fn clone(&self) -> ($($T,)+) {
102 ($(e!(self.$idx.clone()),)+)
107 impl<$($T:PartialEq),+> PartialEq for ($($T,)+) {
109 fn eq(&self, other: &($($T,)+)) -> bool {
110 e!($(self.$idx == other.$idx)&&+)
113 fn ne(&self, other: &($($T,)+)) -> bool {
114 e!($(self.$idx != other.$idx)||+)
119 impl<$($T:Eq),+> Eq for ($($T,)+) {}
122 impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) {
124 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
125 lexical_partial_cmp!($(self.$idx, other.$idx),+)
128 fn lt(&self, other: &($($T,)+)) -> bool {
129 lexical_ord!(lt, $(self.$idx, other.$idx),+)
132 fn le(&self, other: &($($T,)+)) -> bool {
133 lexical_ord!(le, $(self.$idx, other.$idx),+)
136 fn ge(&self, other: &($($T,)+)) -> bool {
137 lexical_ord!(ge, $(self.$idx, other.$idx),+)
140 fn gt(&self, other: &($($T,)+)) -> bool {
141 lexical_ord!(gt, $(self.$idx, other.$idx),+)
146 impl<$($T:Ord),+> Ord for ($($T,)+) {
148 fn cmp(&self, other: &($($T,)+)) -> Ordering {
149 lexical_cmp!($(self.$idx, other.$idx),+)
154 impl<$($T:Default),+> Default for ($($T,)+) {
157 fn default() -> ($($T,)+) {
158 ($({ let x: $T = Default::default(); x},)+)
165 // Constructs an expression that performs a lexical ordering using method $rel.
166 // The values are interleaved, so the macro invocation for
167 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
168 // a3, b3)` (and similarly for `lexical_cmp`)
169 macro_rules! lexical_ord {
170 ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
171 if $a != $b { lexical_ord!($rel, $a, $b) }
172 else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
174 ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
177 macro_rules! lexical_partial_cmp {
178 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
179 match ($a).partial_cmp(&$b) {
180 Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
184 ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
187 macro_rules! lexical_cmp {
188 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
189 match ($a).cmp(&$b) {
190 Equal => lexical_cmp!($($rest_a, $rest_b),+),
194 ($a:expr, $b:expr) => { ($a).cmp(&$b) };
199 (val0, ref0, mut0, 0) -> A
202 (val0, ref0, mut0, 0) -> A
203 (val1, ref1, mut1, 1) -> B
206 (val0, ref0, mut0, 0) -> A
207 (val1, ref1, mut1, 1) -> B
208 (val2, ref2, mut2, 2) -> C
211 (val0, ref0, mut0, 0) -> A
212 (val1, ref1, mut1, 1) -> B
213 (val2, ref2, mut2, 2) -> C
214 (val3, ref3, mut3, 3) -> D
217 (val0, ref0, mut0, 0) -> A
218 (val1, ref1, mut1, 1) -> B
219 (val2, ref2, mut2, 2) -> C
220 (val3, ref3, mut3, 3) -> D
221 (val4, ref4, mut4, 4) -> E
224 (val0, ref0, mut0, 0) -> A
225 (val1, ref1, mut1, 1) -> B
226 (val2, ref2, mut2, 2) -> C
227 (val3, ref3, mut3, 3) -> D
228 (val4, ref4, mut4, 4) -> E
229 (val5, ref5, mut5, 5) -> F
232 (val0, ref0, mut0, 0) -> A
233 (val1, ref1, mut1, 1) -> B
234 (val2, ref2, mut2, 2) -> C
235 (val3, ref3, mut3, 3) -> D
236 (val4, ref4, mut4, 4) -> E
237 (val5, ref5, mut5, 5) -> F
238 (val6, ref6, mut6, 6) -> G
241 (val0, ref0, mut0, 0) -> A
242 (val1, ref1, mut1, 1) -> B
243 (val2, ref2, mut2, 2) -> C
244 (val3, ref3, mut3, 3) -> D
245 (val4, ref4, mut4, 4) -> E
246 (val5, ref5, mut5, 5) -> F
247 (val6, ref6, mut6, 6) -> G
248 (val7, ref7, mut7, 7) -> H
251 (val0, ref0, mut0, 0) -> A
252 (val1, ref1, mut1, 1) -> B
253 (val2, ref2, mut2, 2) -> C
254 (val3, ref3, mut3, 3) -> D
255 (val4, ref4, mut4, 4) -> E
256 (val5, ref5, mut5, 5) -> F
257 (val6, ref6, mut6, 6) -> G
258 (val7, ref7, mut7, 7) -> H
259 (val8, ref8, mut8, 8) -> I
262 (val0, ref0, mut0, 0) -> A
263 (val1, ref1, mut1, 1) -> B
264 (val2, ref2, mut2, 2) -> C
265 (val3, ref3, mut3, 3) -> D
266 (val4, ref4, mut4, 4) -> E
267 (val5, ref5, mut5, 5) -> F
268 (val6, ref6, mut6, 6) -> G
269 (val7, ref7, mut7, 7) -> H
270 (val8, ref8, mut8, 8) -> I
271 (val9, ref9, mut9, 9) -> J
274 (val0, ref0, mut0, 0) -> A
275 (val1, ref1, mut1, 1) -> B
276 (val2, ref2, mut2, 2) -> C
277 (val3, ref3, mut3, 3) -> D
278 (val4, ref4, mut4, 4) -> E
279 (val5, ref5, mut5, 5) -> F
280 (val6, ref6, mut6, 6) -> G
281 (val7, ref7, mut7, 7) -> H
282 (val8, ref8, mut8, 8) -> I
283 (val9, ref9, mut9, 9) -> J
284 (val10, ref10, mut10, 10) -> K
287 (val0, ref0, mut0, 0) -> A
288 (val1, ref1, mut1, 1) -> B
289 (val2, ref2, mut2, 2) -> C
290 (val3, ref3, mut3, 3) -> D
291 (val4, ref4, mut4, 4) -> E
292 (val5, ref5, mut5, 5) -> F
293 (val6, ref6, mut6, 6) -> G
294 (val7, ref7, mut7, 7) -> H
295 (val8, ref8, mut8, 8) -> I
296 (val9, ref9, mut9, 9) -> J
297 (val10, ref10, mut10, 10) -> K
298 (val11, ref11, mut11, 11) -> L