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")]
25 impl<$($T:PartialEq),+> PartialEq for ($($T,)+) where last_type!($($T,)+): ?Sized {
27 fn eq(&self, other: &($($T,)+)) -> bool {
28 $(self.$idx == other.$idx)&&+
31 fn ne(&self, other: &($($T,)+)) -> bool {
32 $(self.$idx != other.$idx)||+
36 #[stable(feature = "rust1", since = "1.0.0")]
37 impl<$($T:Eq),+> Eq for ($($T,)+) where last_type!($($T,)+): ?Sized {}
39 #[stable(feature = "rust1", since = "1.0.0")]
40 impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+)
41 where last_type!($($T,)+): ?Sized {
43 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
44 lexical_partial_cmp!($(self.$idx, other.$idx),+)
47 fn lt(&self, other: &($($T,)+)) -> bool {
48 lexical_ord!(lt, $(self.$idx, other.$idx),+)
51 fn le(&self, other: &($($T,)+)) -> bool {
52 lexical_ord!(le, $(self.$idx, other.$idx),+)
55 fn ge(&self, other: &($($T,)+)) -> bool {
56 lexical_ord!(ge, $(self.$idx, other.$idx),+)
59 fn gt(&self, other: &($($T,)+)) -> bool {
60 lexical_ord!(gt, $(self.$idx, other.$idx),+)
64 #[stable(feature = "rust1", since = "1.0.0")]
65 impl<$($T:Ord),+> Ord for ($($T,)+) where last_type!($($T,)+): ?Sized {
67 fn cmp(&self, other: &($($T,)+)) -> Ordering {
68 lexical_cmp!($(self.$idx, other.$idx),+)
72 #[stable(feature = "rust1", since = "1.0.0")]
73 impl<$($T:Default),+> Default for ($($T,)+) {
75 fn default() -> ($($T,)+) {
76 ($({ let x: $T = Default::default(); x},)+)
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),+) }
92 ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
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),+),
102 ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
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),+),
112 ($a:expr, $b:expr) => { ($a).cmp(&$b) };
115 macro_rules! last_type {
116 ($a:ident,) => { $a };
117 ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };