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.
30 #![stable(feature = "rust1", since = "1.0.0")]
37 use option::Option::Some;
39 // FIXME(#19630) Remove this work-around
44 // macro for implementing n-ary tuple functions and operations
45 macro_rules! tuple_impls {
48 $(($idx:tt) -> $T:ident)+
52 #[stable(feature = "rust1", since = "1.0.0")]
53 impl<$($T:Clone),+> Clone for ($($T,)+) {
54 fn clone(&self) -> ($($T,)+) {
55 ($(e!(self.$idx.clone()),)+)
59 #[stable(feature = "rust1", since = "1.0.0")]
60 impl<$($T:PartialEq),+> PartialEq for ($($T,)+) {
62 fn eq(&self, other: &($($T,)+)) -> bool {
63 e!($(self.$idx == other.$idx)&&+)
66 fn ne(&self, other: &($($T,)+)) -> bool {
67 e!($(self.$idx != other.$idx)||+)
71 #[stable(feature = "rust1", since = "1.0.0")]
72 impl<$($T:Eq),+> Eq for ($($T,)+) {}
74 #[stable(feature = "rust1", since = "1.0.0")]
75 impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) {
77 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
78 lexical_partial_cmp!($(self.$idx, other.$idx),+)
81 fn lt(&self, other: &($($T,)+)) -> bool {
82 lexical_ord!(lt, $(self.$idx, other.$idx),+)
85 fn le(&self, other: &($($T,)+)) -> bool {
86 lexical_ord!(le, $(self.$idx, other.$idx),+)
89 fn ge(&self, other: &($($T,)+)) -> bool {
90 lexical_ord!(ge, $(self.$idx, other.$idx),+)
93 fn gt(&self, other: &($($T,)+)) -> bool {
94 lexical_ord!(gt, $(self.$idx, other.$idx),+)
98 #[stable(feature = "rust1", since = "1.0.0")]
99 impl<$($T:Ord),+> Ord for ($($T,)+) {
101 fn cmp(&self, other: &($($T,)+)) -> Ordering {
102 lexical_cmp!($(self.$idx, other.$idx),+)
106 #[stable(feature = "rust1", since = "1.0.0")]
107 impl<$($T:Default),+> Default for ($($T,)+) {
108 #[stable(feature = "rust1", since = "1.0.0")]
110 fn default() -> ($($T,)+) {
111 ($({ let x: $T = Default::default(); x},)+)
118 // Constructs an expression that performs a lexical ordering using method $rel.
119 // The values are interleaved, so the macro invocation for
120 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
121 // a3, b3)` (and similarly for `lexical_cmp`)
122 macro_rules! lexical_ord {
123 ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
124 if $a != $b { lexical_ord!($rel, $a, $b) }
125 else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
127 ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
130 macro_rules! lexical_partial_cmp {
131 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
132 match ($a).partial_cmp(&$b) {
133 Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
137 ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
140 macro_rules! lexical_cmp {
141 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
142 match ($a).cmp(&$b) {
143 Equal => lexical_cmp!($($rest_a, $rest_b),+),
147 ($a:expr, $b:expr) => { ($a).cmp(&$b) };