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 // See src/libstd/primitive_docs.rs for documentation.
16 // macro for implementing n-ary tuple functions and operations
17 macro_rules! tuple_impls {
20 $(($idx:tt) -> $T:ident)+
24 #[stable(feature = "rust1", since = "1.0.0")]
26 impl<$($T:Clone),+> Clone for ($($T,)+) {
27 fn clone(&self) -> ($($T,)+) {
28 ($(self.$idx.clone(),)+)
32 #[stable(feature = "rust1", since = "1.0.0")]
33 impl<$($T:PartialEq),+> PartialEq for ($($T,)+) where last_type!($($T,)+): ?Sized {
35 fn eq(&self, other: &($($T,)+)) -> bool {
36 $(self.$idx == other.$idx)&&+
39 fn ne(&self, other: &($($T,)+)) -> bool {
40 $(self.$idx != other.$idx)||+
44 #[stable(feature = "rust1", since = "1.0.0")]
45 impl<$($T:Eq),+> Eq for ($($T,)+) where last_type!($($T,)+): ?Sized {}
47 #[stable(feature = "rust1", since = "1.0.0")]
48 impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+)
49 where last_type!($($T,)+): ?Sized {
51 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
52 lexical_partial_cmp!($(self.$idx, other.$idx),+)
55 fn lt(&self, other: &($($T,)+)) -> bool {
56 lexical_ord!(lt, $(self.$idx, other.$idx),+)
59 fn le(&self, other: &($($T,)+)) -> bool {
60 lexical_ord!(le, $(self.$idx, other.$idx),+)
63 fn ge(&self, other: &($($T,)+)) -> bool {
64 lexical_ord!(ge, $(self.$idx, other.$idx),+)
67 fn gt(&self, other: &($($T,)+)) -> bool {
68 lexical_ord!(gt, $(self.$idx, other.$idx),+)
72 #[stable(feature = "rust1", since = "1.0.0")]
73 impl<$($T:Ord),+> Ord for ($($T,)+) where last_type!($($T,)+): ?Sized {
75 fn cmp(&self, other: &($($T,)+)) -> Ordering {
76 lexical_cmp!($(self.$idx, other.$idx),+)
80 #[stable(feature = "rust1", since = "1.0.0")]
81 impl<$($T:Default),+> Default for ($($T,)+) {
83 fn default() -> ($($T,)+) {
84 ($({ let x: $T = Default::default(); x},)+)
91 // Constructs an expression that performs a lexical ordering using method $rel.
92 // The values are interleaved, so the macro invocation for
93 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
94 // a3, b3)` (and similarly for `lexical_cmp`)
95 macro_rules! lexical_ord {
96 ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
97 if $a != $b { lexical_ord!($rel, $a, $b) }
98 else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
100 ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
103 macro_rules! lexical_partial_cmp {
104 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
105 match ($a).partial_cmp(&$b) {
106 Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
110 ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
113 macro_rules! lexical_cmp {
114 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
115 match ($a).cmp(&$b) {
116 Equal => lexical_cmp!($($rest_a, $rest_b),+),
120 ($a:expr, $b:expr) => { ($a).cmp(&$b) };
123 macro_rules! last_type {
124 ($a:ident,) => { $a };
125 ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };