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