Skip to main content

core/
tuple.rs

1// See core/src/primitive_docs.rs for documentation.
2
3use crate::cell::CloneFromCell;
4use crate::cmp::Ordering::{self, *};
5use crate::marker::{ConstParamTy_, StructuralPartialEq};
6use crate::ops::ControlFlow::{self, Break, Continue};
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            #[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
28            impl<$($T: [const] PartialEq),+> const PartialEq for ($($T,)+) {
29                #[inline]
30                #[ferrocene::prevalidated]
31                fn eq(&self, other: &($($T,)+)) -> bool {
32                    $( ${ignore($T)} self.${index()} == other.${index()} )&&+
33                }
34                #[inline]
35                #[ferrocene::prevalidated]
36                fn ne(&self, other: &($($T,)+)) -> bool {
37                    $( ${ignore($T)} self.${index()} != other.${index()} )||+
38                }
39            }
40        }
41
42        maybe_tuple_doc! {
43            $($T)+ @
44            #[stable(feature = "rust1", since = "1.0.0")]
45            #[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
46            impl<$($T: [const] Eq),+> const Eq for ($($T,)+)
47            {}
48        }
49
50        maybe_tuple_doc! {
51            $($T)+ @
52            #[unstable(feature = "min_adt_const_params", issue = "154042")]
53            impl<$($T: ConstParamTy_),+> ConstParamTy_ for ($($T,)+)
54            {}
55        }
56
57        maybe_tuple_doc! {
58            $($T)+ @
59            #[unstable(feature = "structural_match", issue = "31434")]
60            impl<$($T),+> StructuralPartialEq for ($($T,)+)
61            {}
62        }
63
64        maybe_tuple_doc! {
65            $($T)+ @
66            #[stable(feature = "rust1", since = "1.0.0")]
67            #[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
68            impl<$($T: [const] PartialOrd),+> const PartialOrd for ($($T,)+)
69            {
70                #[inline]
71                fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
72                    lexical_partial_cmp!($( ${ignore($T)} self.${index()}, other.${index()} ),+)
73                }
74                #[inline]
75                fn lt(&self, other: &($($T,)+)) -> bool {
76                    lexical_ord!(lt, __chaining_lt, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
77                }
78                #[inline]
79                fn le(&self, other: &($($T,)+)) -> bool {
80                    lexical_ord!(le, __chaining_le, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
81                }
82                #[inline]
83                fn ge(&self, other: &($($T,)+)) -> bool {
84                    lexical_ord!(ge, __chaining_ge, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
85                }
86                #[inline]
87                fn gt(&self, other: &($($T,)+)) -> bool {
88                    lexical_ord!(gt, __chaining_gt, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
89                }
90                #[inline]
91                fn __chaining_lt(&self, other: &($($T,)+)) -> ControlFlow<bool> {
92                    lexical_chain!(__chaining_lt, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
93                }
94                #[inline]
95                fn __chaining_le(&self, other: &($($T,)+)) -> ControlFlow<bool> {
96                    lexical_chain!(__chaining_le, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
97                }
98                #[inline]
99                fn __chaining_gt(&self, other: &($($T,)+)) -> ControlFlow<bool> {
100                    lexical_chain!(__chaining_gt, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
101                }
102                #[inline]
103                fn __chaining_ge(&self, other: &($($T,)+)) -> ControlFlow<bool> {
104                    lexical_chain!(__chaining_ge, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
105                }
106            }
107        }
108
109        maybe_tuple_doc! {
110            $($T)+ @
111            #[stable(feature = "rust1", since = "1.0.0")]
112            #[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
113            impl<$($T: [const] Ord),+> const Ord for ($($T,)+)
114            {
115                #[inline]
116                fn cmp(&self, other: &($($T,)+)) -> Ordering {
117                    lexical_cmp!($( ${ignore($T)} self.${index()}, other.${index()} ),+)
118                }
119            }
120        }
121
122        maybe_tuple_doc! {
123            $($T)+ @
124            #[stable(feature = "rust1", since = "1.0.0")]
125            impl<$($T: Default),+> Default for ($($T,)+) {
126                #[inline]
127                fn default() -> ($($T,)+) {
128                    ($({ let x: $T = Default::default(); x},)+)
129                }
130            }
131        }
132
133        maybe_tuple_doc! {
134            $($T)+ @
135            #[stable(feature = "array_tuple_conv", since = "1.71.0")]
136            // can't do const From due to https://github.com/rust-lang/rust/issues/144280
137            impl<T> From<[T; ${count($T)}]> for ($(${ignore($T)} T,)+) {
138                #[inline]
139                #[allow(non_snake_case)]
140                fn from(array: [T; ${count($T)}]) -> Self {
141                    let [$($T,)+] = array;
142                    ($($T,)+)
143                }
144            }
145        }
146
147        maybe_tuple_doc! {
148            $($T)+ @
149            #[stable(feature = "array_tuple_conv", since = "1.71.0")]
150            // can't do const From due to https://github.com/rust-lang/rust/issues/144280
151            impl<T> From<($(${ignore($T)} T,)+)> for [T; ${count($T)}] {
152                #[inline]
153                #[allow(non_snake_case)]
154                fn from(tuple: ($(${ignore($T)} T,)+)) -> Self {
155                    let ($($T,)+) = tuple;
156                    [$($T,)+]
157                }
158            }
159        }
160
161        maybe_tuple_doc! {
162            $($T)+ @
163            // SAFETY: tuples introduce no additional indirection, so they can be copied whenever T
164            // can.
165            #[unstable(feature = "cell_get_cloned", issue = "145329")]
166            unsafe impl<$($T: CloneFromCell),+> CloneFromCell for ($($T,)+)
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);