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 //! A finite heterogeneous sequence, `(T, U, ..)`
13 //! To access a single element of a tuple one can use the `.0`
14 //! field access syntax.
16 //! Indexing starts from zero, so `.0` returns first value, `.1`
17 //! returns second value, and so on. In general, a tuple with *N*
18 //! elements has field accessors from 0 to *N* - 1.
20 //! If every type inside a tuple implements one of the following
21 //! traits, then a tuple itself also implements it.
35 use option::Option::Some;
37 // FIXME(#19630) Remove this work-around
42 // macro for implementing n-ary tuple functions and operations
43 macro_rules! tuple_impls {
46 $(($idx:tt) -> $T:ident)+
50 #[stable(feature = "rust1", since = "1.0.0")]
51 impl<$($T:Clone),+> Clone for ($($T,)+) {
52 fn clone(&self) -> ($($T,)+) {
53 ($(e!(self.$idx.clone()),)+)
57 #[stable(feature = "rust1", since = "1.0.0")]
58 impl<$($T:PartialEq),+> PartialEq for ($($T,)+) {
60 fn eq(&self, other: &($($T,)+)) -> bool {
61 e!($(self.$idx == other.$idx)&&+)
64 fn ne(&self, other: &($($T,)+)) -> bool {
65 e!($(self.$idx != other.$idx)||+)
69 #[stable(feature = "rust1", since = "1.0.0")]
70 impl<$($T:Eq),+> Eq for ($($T,)+) {}
72 #[stable(feature = "rust1", since = "1.0.0")]
73 impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) {
75 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
76 lexical_partial_cmp!($(self.$idx, other.$idx),+)
79 fn lt(&self, other: &($($T,)+)) -> bool {
80 lexical_ord!(lt, $(self.$idx, other.$idx),+)
83 fn le(&self, other: &($($T,)+)) -> bool {
84 lexical_ord!(le, $(self.$idx, other.$idx),+)
87 fn ge(&self, other: &($($T,)+)) -> bool {
88 lexical_ord!(ge, $(self.$idx, other.$idx),+)
91 fn gt(&self, other: &($($T,)+)) -> bool {
92 lexical_ord!(gt, $(self.$idx, other.$idx),+)
96 #[stable(feature = "rust1", since = "1.0.0")]
97 impl<$($T:Ord),+> Ord for ($($T,)+) {
99 fn cmp(&self, other: &($($T,)+)) -> Ordering {
100 lexical_cmp!($(self.$idx, other.$idx),+)
104 #[stable(feature = "rust1", since = "1.0.0")]
105 impl<$($T:Default),+> Default for ($($T,)+) {
107 fn default() -> ($($T,)+) {
108 ($({ let x: $T = Default::default(); x},)+)
115 // Constructs an expression that performs a lexical ordering using method $rel.
116 // The values are interleaved, so the macro invocation for
117 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
118 // a3, b3)` (and similarly for `lexical_cmp`)
119 macro_rules! lexical_ord {
120 ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
121 if $a != $b { lexical_ord!($rel, $a, $b) }
122 else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
124 ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
127 macro_rules! lexical_partial_cmp {
128 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
129 match ($a).partial_cmp(&$b) {
130 Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
134 ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
137 macro_rules! lexical_cmp {
138 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
139 match ($a).cmp(&$b) {
140 Equal => lexical_cmp!($($rest_a, $rest_b),+),
144 ($a:expr, $b:expr) => { ($a).cmp(&$b) };