core/
tuple.rs

1// See core/src/primitive_docs.rs for documentation.
2
3use crate::cmp::Ordering::{self, *};
4use crate::marker::{ConstParamTy_, StructuralPartialEq, UnsizedConstParamTy};
5use crate::ops::ControlFlow::{self, Break, Continue};
6use crate::random::{Random, RandomSource};
7
8// Recursive macro for implementing n-ary tuple functions and operations
9//
10// Also provides implementations for tuples with lesser arity. For example, tuple_impls!(A B C)
11// will implement everything for (A, B, C), (A, B) and (A,).
12macro_rules! tuple_impls {
13    // Stopping criteria (1-ary tuple)
14    ($T:ident) => {
15        tuple_impls!(@impl $T);
16    };
17    // Running criteria (n-ary tuple, with n >= 2)
18    ($T:ident $( $U:ident )+) => {
19        tuple_impls!($( $U )+);
20        tuple_impls!(@impl $T $( $U )+);
21    };
22    // "Private" internal implementation
23    (@impl $( $T:ident )+) => {
24        maybe_tuple_doc! {
25            $($T)+ @
26            #[stable(feature = "rust1", since = "1.0.0")]
27            impl<$($T: PartialEq),+> PartialEq for ($($T,)+) {
28                #[inline]
29                fn eq(&self, other: &($($T,)+)) -> bool {
30                    $( ${ignore($T)} self.${index()} == other.${index()} )&&+
31                }
32                #[inline]
33                fn ne(&self, other: &($($T,)+)) -> bool {
34                    $( ${ignore($T)} self.${index()} != other.${index()} )||+
35                }
36            }
37        }
38
39        maybe_tuple_doc! {
40            $($T)+ @
41            #[stable(feature = "rust1", since = "1.0.0")]
42            impl<$($T: Eq),+> Eq for ($($T,)+)
43            {}
44        }
45
46        maybe_tuple_doc! {
47            $($T)+ @
48            #[unstable(feature = "adt_const_params", issue = "95174")]
49            impl<$($T: ConstParamTy_),+> ConstParamTy_ for ($($T,)+)
50            {}
51        }
52
53        maybe_tuple_doc! {
54            $($T)+ @
55            #[unstable(feature = "unsized_const_params", issue = "95174")]
56            impl<$($T: UnsizedConstParamTy),+> UnsizedConstParamTy for ($($T,)+)
57            {}
58        }
59
60        maybe_tuple_doc! {
61            $($T)+ @
62            #[unstable(feature = "structural_match", issue = "31434")]
63            impl<$($T),+> StructuralPartialEq for ($($T,)+)
64            {}
65        }
66
67        maybe_tuple_doc! {
68            $($T)+ @
69            #[stable(feature = "rust1", since = "1.0.0")]
70            impl<$($T: PartialOrd),+> PartialOrd for ($($T,)+)
71            {
72                #[inline]
73                fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
74                    lexical_partial_cmp!($( ${ignore($T)} self.${index()}, other.${index()} ),+)
75                }
76                #[inline]
77                fn lt(&self, other: &($($T,)+)) -> bool {
78                    lexical_ord!(lt, __chaining_lt, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
79                }
80                #[inline]
81                fn le(&self, other: &($($T,)+)) -> bool {
82                    lexical_ord!(le, __chaining_le, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
83                }
84                #[inline]
85                fn ge(&self, other: &($($T,)+)) -> bool {
86                    lexical_ord!(ge, __chaining_ge, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
87                }
88                #[inline]
89                fn gt(&self, other: &($($T,)+)) -> bool {
90                    lexical_ord!(gt, __chaining_gt, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
91                }
92                #[inline]
93                fn __chaining_lt(&self, other: &($($T,)+)) -> ControlFlow<bool> {
94                    lexical_chain!(__chaining_lt, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
95                }
96                #[inline]
97                fn __chaining_le(&self, other: &($($T,)+)) -> ControlFlow<bool> {
98                    lexical_chain!(__chaining_le, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
99                }
100                #[inline]
101                fn __chaining_gt(&self, other: &($($T,)+)) -> ControlFlow<bool> {
102                    lexical_chain!(__chaining_gt, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
103                }
104                #[inline]
105                fn __chaining_ge(&self, other: &($($T,)+)) -> ControlFlow<bool> {
106                    lexical_chain!(__chaining_ge, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
107                }
108            }
109        }
110
111        maybe_tuple_doc! {
112            $($T)+ @
113            #[stable(feature = "rust1", since = "1.0.0")]
114            impl<$($T: Ord),+> Ord for ($($T,)+)
115            {
116                #[inline]
117                fn cmp(&self, other: &($($T,)+)) -> Ordering {
118                    lexical_cmp!($( ${ignore($T)} self.${index()}, other.${index()} ),+)
119                }
120            }
121        }
122
123        maybe_tuple_doc! {
124            $($T)+ @
125            #[stable(feature = "rust1", since = "1.0.0")]
126            impl<$($T: Default),+> Default for ($($T,)+) {
127                #[inline]
128                fn default() -> ($($T,)+) {
129                    ($({ let x: $T = Default::default(); x},)+)
130                }
131            }
132        }
133
134        maybe_tuple_doc! {
135            $($T)+ @
136            #[unstable(feature = "random", issue = "130703")]
137            impl<$($T: Random),+> Random for ($($T,)+) {
138                fn random(source: &mut (impl RandomSource + ?Sized)) -> Self {
139                    ($({ let x: $T = Random::random(source); x},)+)
140                }
141            }
142        }
143
144        maybe_tuple_doc! {
145            $($T)+ @
146            #[stable(feature = "array_tuple_conv", since = "1.71.0")]
147            impl<T> From<[T; ${count($T)}]> for ($(${ignore($T)} T,)+) {
148                #[inline]
149                #[allow(non_snake_case)]
150                fn from(array: [T; ${count($T)}]) -> Self {
151                    let [$($T,)+] = array;
152                    ($($T,)+)
153                }
154            }
155        }
156
157        maybe_tuple_doc! {
158            $($T)+ @
159            #[stable(feature = "array_tuple_conv", since = "1.71.0")]
160            impl<T> From<($(${ignore($T)} T,)+)> for [T; ${count($T)}] {
161                #[inline]
162                #[allow(non_snake_case)]
163                fn from(tuple: ($(${ignore($T)} T,)+)) -> Self {
164                    let ($($T,)+) = tuple;
165                    [$($T,)+]
166                }
167            }
168        }
169    }
170}
171
172// If this is a unary tuple, it adds a doc comment.
173// Otherwise, it hides the docs entirely.
174macro_rules! maybe_tuple_doc {
175    ($a:ident @ #[$meta:meta] $item:item) => {
176        #[doc(fake_variadic)]
177        #[doc = "This trait is implemented for tuples up to twelve items long."]
178        #[$meta]
179        $item
180    };
181    ($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
182        #[doc(hidden)]
183        #[$meta]
184        $item
185    };
186}
187
188// Constructs an expression that performs a lexical ordering using method `$rel`.
189// The values are interleaved, so the macro invocation for
190// `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, opt_is_lt, a1, b1,
191// a2, b2, a3, b3)` (and similarly for `lexical_cmp`)
192//
193// `$chain_rel` is the chaining method from `PartialOrd` to use for all but the
194// final value, to produce better results for simple primitives.
195macro_rules! lexical_ord {
196    ($rel: ident, $chain_rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {{
197        match PartialOrd::$chain_rel(&$a, &$b) {
198            Break(val) => val,
199            Continue(()) => lexical_ord!($rel, $chain_rel, $($rest_a, $rest_b),+),
200        }
201    }};
202    ($rel: ident, $chain_rel: ident, $a:expr, $b:expr) => {
203        // Use the specific method for the last element
204        PartialOrd::$rel(&$a, &$b)
205    };
206}
207
208// Same parameter interleaving as `lexical_ord` above
209macro_rules! lexical_chain {
210    ($chain_rel: ident, $a:expr, $b:expr $(,$rest_a:expr, $rest_b:expr)*) => {{
211        PartialOrd::$chain_rel(&$a, &$b)?;
212        lexical_chain!($chain_rel $(,$rest_a, $rest_b)*)
213    }};
214    ($chain_rel: ident) => {
215        Continue(())
216    };
217}
218
219macro_rules! lexical_partial_cmp {
220    ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
221        match ($a).partial_cmp(&$b) {
222            Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
223            ordering => ordering
224        }
225    };
226    ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
227}
228
229macro_rules! lexical_cmp {
230    ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
231        match ($a).cmp(&$b) {
232            Equal => lexical_cmp!($($rest_a, $rest_b),+),
233            ordering => ordering
234        }
235    };
236    ($a:expr, $b:expr) => { ($a).cmp(&$b) };
237}
238
239tuple_impls!(E D C B A Z Y X W V U T);