]>
Commit | Line | Data |
---|---|---|
7453a54e | 1 | // See src/libstd/primitive_docs.rs for documentation. |
1a4d82fc | 2 | |
48663c56 | 3 | use crate::cmp::Ordering::*; |
60c5eb7d | 4 | use crate::cmp::*; |
1a4d82fc | 5 | |
04454e1e FG |
6 | // Recursive macro for implementing n-ary tuple functions and operations |
7 | // | |
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,). | |
1a4d82fc | 10 | macro_rules! tuple_impls { |
04454e1e FG |
11 | // Stopping criteria (1-ary tuple) |
12 | ($T:ident) => { | |
13 | tuple_impls!(@impl $T); | |
14 | }; | |
15 | // Running criteria (n-ary tuple, with n >= 2) | |
16 | ($T:ident $( $U:ident )+) => { | |
17 | tuple_impls!($( $U )+); | |
18 | tuple_impls!(@impl $T $( $U )+); | |
19 | }; | |
20 | // "Private" internal implementation | |
21 | (@impl $( $T:ident )+) => { | |
923072b8 FG |
22 | maybe_tuple_doc! { |
23 | $($T)+ @ | |
24 | #[stable(feature = "rust1", since = "1.0.0")] | |
25 | impl<$($T:PartialEq),+> PartialEq for ($($T,)+) | |
26 | where | |
27 | last_type!($($T,)+): ?Sized | |
28 | { | |
29 | #[inline] | |
30 | fn eq(&self, other: &($($T,)+)) -> bool { | |
31 | $( ${ignore(T)} self.${index()} == other.${index()} )&&+ | |
32 | } | |
33 | #[inline] | |
34 | fn ne(&self, other: &($($T,)+)) -> bool { | |
35 | $( ${ignore(T)} self.${index()} != other.${index()} )||+ | |
36 | } | |
1a4d82fc | 37 | } |
04454e1e | 38 | } |
1a4d82fc | 39 | |
923072b8 FG |
40 | maybe_tuple_doc! { |
41 | $($T)+ @ | |
42 | #[stable(feature = "rust1", since = "1.0.0")] | |
43 | impl<$($T:Eq),+> Eq for ($($T,)+) | |
44 | where | |
45 | last_type!($($T,)+): ?Sized | |
46 | {} | |
47 | } | |
1a4d82fc | 48 | |
923072b8 FG |
49 | maybe_tuple_doc! { |
50 | $($T)+ @ | |
51 | #[stable(feature = "rust1", since = "1.0.0")] | |
52 | impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) | |
53 | where | |
54 | last_type!($($T,)+): ?Sized | |
55 | { | |
56 | #[inline] | |
57 | fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> { | |
58 | lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+) | |
59 | } | |
60 | #[inline] | |
61 | fn lt(&self, other: &($($T,)+)) -> bool { | |
62 | lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+) | |
63 | } | |
64 | #[inline] | |
65 | fn le(&self, other: &($($T,)+)) -> bool { | |
66 | lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+) | |
67 | } | |
68 | #[inline] | |
69 | fn ge(&self, other: &($($T,)+)) -> bool { | |
70 | lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+) | |
71 | } | |
72 | #[inline] | |
73 | fn gt(&self, other: &($($T,)+)) -> bool { | |
74 | lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+) | |
75 | } | |
04454e1e FG |
76 | } |
77 | } | |
1a4d82fc | 78 | |
923072b8 FG |
79 | maybe_tuple_doc! { |
80 | $($T)+ @ | |
81 | #[stable(feature = "rust1", since = "1.0.0")] | |
82 | impl<$($T:Ord),+> Ord for ($($T,)+) | |
83 | where | |
84 | last_type!($($T,)+): ?Sized | |
85 | { | |
86 | #[inline] | |
87 | fn cmp(&self, other: &($($T,)+)) -> Ordering { | |
88 | lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+) | |
89 | } | |
1a4d82fc | 90 | } |
04454e1e | 91 | } |
1a4d82fc | 92 | |
923072b8 FG |
93 | maybe_tuple_doc! { |
94 | $($T)+ @ | |
95 | #[stable(feature = "rust1", since = "1.0.0")] | |
96 | impl<$($T:Default),+> Default for ($($T,)+) { | |
97 | #[inline] | |
98 | fn default() -> ($($T,)+) { | |
99 | ($({ let x: $T = Default::default(); x},)+) | |
100 | } | |
1a4d82fc | 101 | } |
04454e1e | 102 | } |
1a4d82fc JJ |
103 | } |
104 | } | |
105 | ||
923072b8 FG |
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 | #[cfg_attr(not(bootstrap), doc(tuple_variadic))] | |
111 | #[doc = "This trait is implemented for tuples up to twelve items long."] | |
112 | #[$meta] | |
113 | $item | |
114 | }; | |
115 | ($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => { | |
116 | #[doc(hidden)] | |
117 | #[$meta] | |
118 | $item | |
119 | }; | |
120 | } | |
121 | ||
1a4d82fc JJ |
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),+) } | |
130 | }; | |
131 | ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) }; | |
132 | } | |
133 | ||
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),+), | |
138 | ordering => ordering | |
139 | } | |
140 | }; | |
141 | ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) }; | |
142 | } | |
143 | ||
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),+), | |
148 | ordering => ordering | |
149 | } | |
150 | }; | |
151 | ($a:expr, $b:expr) => { ($a).cmp(&$b) }; | |
152 | } | |
153 | ||
041b39d2 XL |
154 | macro_rules! last_type { |
155 | ($a:ident,) => { $a }; | |
156 | ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) }; | |
157 | } | |
158 | ||
923072b8 | 159 | tuple_impls!(E D C B A Z Y X W V U T); |