1 // See src/libstd/primitive_docs.rs for documentation.
3 use crate::cmp::Ordering::*;
6 // Recursive macro for implementing n-ary tuple functions and operations
8 // Also provides implementations for tuples with lesser arity. For example, tuple_impls!(A B C)
9 // will implement everything for (A, B, C), (A, B) and (A,).
10 macro_rules! tuple_impls {
11 // Stopping criteria (1-ary tuple)
13 tuple_impls!(@impl $T);
15 // Running criteria (n-ary tuple, with n >= 2)
16 ($T:ident $( $U:ident )+) => {
17 tuple_impls!($( $U )+);
18 tuple_impls!(@impl $T $( $U )+);
20 // "Private" internal implementation
21 (@impl $( $T:ident )+) => {
24 #[stable(feature = "rust1", since = "1.0.0")]
25 impl<$($T:PartialEq),+> PartialEq for ($($T,)+)
27 last_type!($($T,)+): ?Sized
30 fn eq(&self, other: &($($T,)+)) -> bool {
31 $( ${ignore(T)} self.${index()} == other.${index()} )&&+
34 fn ne(&self, other: &($($T,)+)) -> bool {
35 $( ${ignore(T)} self.${index()} != other.${index()} )||+
42 #[stable(feature = "rust1", since = "1.0.0")]
43 impl<$($T:Eq),+> Eq for ($($T,)+)
45 last_type!($($T,)+): ?Sized
51 #[stable(feature = "rust1", since = "1.0.0")]
52 impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+)
54 last_type!($($T,)+): ?Sized
57 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
58 lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
61 fn lt(&self, other: &($($T,)+)) -> bool {
62 lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
65 fn le(&self, other: &($($T,)+)) -> bool {
66 lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
69 fn ge(&self, other: &($($T,)+)) -> bool {
70 lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
73 fn gt(&self, other: &($($T,)+)) -> bool {
74 lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
81 #[stable(feature = "rust1", since = "1.0.0")]
82 impl<$($T:Ord),+> Ord for ($($T,)+)
84 last_type!($($T,)+): ?Sized
87 fn cmp(&self, other: &($($T,)+)) -> Ordering {
88 lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
95 #[stable(feature = "rust1", since = "1.0.0")]
96 impl<$($T:Default),+> Default for ($($T,)+) {
98 fn default() -> ($($T,)+) {
99 ($({ let x: $T = Default::default(); x},)+)
106 // If this is a unary tuple, it adds a doc comment.
107 // Otherwise, it hides the docs entirely.
108 macro_rules! maybe_tuple_doc {
109 ($a:ident @ #[$meta:meta] $item:item) => {
110 #[doc(fake_variadic)]
111 #[doc = "This trait is implemented for tuples up to twelve items long."]
115 ($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
122 // Constructs an expression that performs a lexical ordering using method $rel.
123 // The values are interleaved, so the macro invocation for
124 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
125 // a3, b3)` (and similarly for `lexical_cmp`)
126 macro_rules! lexical_ord {
127 ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
128 if $a != $b { lexical_ord!($rel, $a, $b) }
129 else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
131 ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
134 macro_rules! lexical_partial_cmp {
135 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
136 match ($a).partial_cmp(&$b) {
137 Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
141 ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
144 macro_rules! lexical_cmp {
145 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
146 match ($a).cmp(&$b) {
147 Equal => lexical_cmp!($($rest_a, $rest_b),+),
151 ($a:expr, $b:expr) => { ($a).cmp(&$b) };
154 macro_rules! last_type {
155 ($a:ident,) => { $a };
156 ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
159 tuple_impls!(E D C B A Z Y X W V U T);