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.
18 use option::Option::Some;
20 // FIXME(#19630) Remove this work-around
25 // macro for implementing n-ary tuple functions and operations
26 macro_rules! tuple_impls {
29 $(($idx:tt) -> $T:ident)+
33 #[stable(feature = "rust1", since = "1.0.0")]
34 impl<$($T:Clone),+> Clone for ($($T,)+) {
35 fn clone(&self) -> ($($T,)+) {
36 ($(e!(self.$idx.clone()),)+)
40 #[stable(feature = "rust1", since = "1.0.0")]
41 impl<$($T:PartialEq),+> PartialEq for ($($T,)+) {
43 fn eq(&self, other: &($($T,)+)) -> bool {
44 e!($(self.$idx == other.$idx)&&+)
47 fn ne(&self, other: &($($T,)+)) -> bool {
48 e!($(self.$idx != other.$idx)||+)
52 #[stable(feature = "rust1", since = "1.0.0")]
53 impl<$($T:Eq),+> Eq for ($($T,)+) {}
55 #[stable(feature = "rust1", since = "1.0.0")]
56 impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) {
58 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
59 lexical_partial_cmp!($(self.$idx, other.$idx),+)
62 fn lt(&self, other: &($($T,)+)) -> bool {
63 lexical_ord!(lt, $(self.$idx, other.$idx),+)
66 fn le(&self, other: &($($T,)+)) -> bool {
67 lexical_ord!(le, $(self.$idx, other.$idx),+)
70 fn ge(&self, other: &($($T,)+)) -> bool {
71 lexical_ord!(ge, $(self.$idx, other.$idx),+)
74 fn gt(&self, other: &($($T,)+)) -> bool {
75 lexical_ord!(gt, $(self.$idx, other.$idx),+)
79 #[stable(feature = "rust1", since = "1.0.0")]
80 impl<$($T:Ord),+> Ord for ($($T,)+) {
82 fn cmp(&self, other: &($($T,)+)) -> Ordering {
83 lexical_cmp!($(self.$idx, other.$idx),+)
87 #[stable(feature = "rust1", since = "1.0.0")]
88 impl<$($T:Default),+> Default for ($($T,)+) {
90 fn default() -> ($($T,)+) {
91 ($({ let x: $T = Default::default(); x},)+)
98 // Constructs an expression that performs a lexical ordering using method $rel.
99 // The values are interleaved, so the macro invocation for
100 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
101 // a3, b3)` (and similarly for `lexical_cmp`)
102 macro_rules! lexical_ord {
103 ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
104 if $a != $b { lexical_ord!($rel, $a, $b) }
105 else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
107 ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
110 macro_rules! lexical_partial_cmp {
111 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
112 match ($a).partial_cmp(&$b) {
113 Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
117 ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
120 macro_rules! lexical_cmp {
121 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
122 match ($a).cmp(&$b) {
123 Equal => lexical_cmp!($($rest_a, $rest_b),+),
127 ($a:expr, $b:expr) => { ($a).cmp(&$b) };