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            const impl<$($T: [const] PartialEq),+> 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            const impl<$($T: [const] Eq),+> 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            const impl<$($T: [const] PartialOrd),+> 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_less, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
77                }
78                #[inline]
79                fn le(&self, other: &($($T,)+)) -> bool {
80                    lexical_ord!(le, __chaining_less, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
81                }
82                #[inline]
83                fn ge(&self, other: &($($T,)+)) -> bool {
84                    lexical_ord!(ge, __chaining_greater, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
85                }
86                #[inline]
87                fn gt(&self, other: &($($T,)+)) -> bool {
88                    lexical_ord!(gt, __chaining_greater, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
89                }
90                #[inline]
91                fn __chaining_less(&self, other: &($($T,)+)) -> ControlFlow<bool> {
92                    lexical_chain!(__chaining_less, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
93                }
94                #[inline]
95                fn __chaining_greater(&self, other: &($($T,)+)) -> ControlFlow<bool> {
96                    lexical_chain!(__chaining_greater, $( ${ignore($T)} self.${index()}, other.${index()} ),+)
97                }
98            }
99        }
100
101        maybe_tuple_doc! {
102            $($T)+ @
103            #[stable(feature = "rust1", since = "1.0.0")]
104            #[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
105            const impl<$($T: [const] Ord),+> Ord for ($($T,)+)
106            {
107                #[inline]
108                fn cmp(&self, other: &($($T,)+)) -> Ordering {
109                    lexical_cmp!($( ${ignore($T)} self.${index()}, other.${index()} ),+)
110                }
111            }
112        }
113
114        maybe_tuple_doc! {
115            $($T)+ @
116            #[stable(feature = "rust1", since = "1.0.0")]
117            #[rustc_const_unstable(feature = "const_default", issue = "143894")]
118            const impl<$($T: [const] Default),+> Default for ($($T,)+) {
119                #[inline]
120                fn default() -> ($($T,)+) {
121                    ($({ let x: $T = Default::default(); x},)+)
122                }
123            }
124        }
125
126        maybe_tuple_doc! {
127            $($T)+ @
128            #[stable(feature = "array_tuple_conv", since = "1.71.0")]
129            // can't do const From due to https://github.com/rust-lang/rust/issues/144280
130            impl<T> From<[T; ${count($T)}]> for ($(${ignore($T)} T,)+) {
131                #[inline]
132                #[allow(non_snake_case)]
133                fn from(array: [T; ${count($T)}]) -> Self {
134                    let [$($T,)+] = array;
135                    ($($T,)+)
136                }
137            }
138        }
139
140        maybe_tuple_doc! {
141            $($T)+ @
142            #[stable(feature = "array_tuple_conv", since = "1.71.0")]
143            // can't do const From due to https://github.com/rust-lang/rust/issues/144280
144            impl<T> From<($(${ignore($T)} T,)+)> for [T; ${count($T)}] {
145                #[inline]
146                #[allow(non_snake_case)]
147                fn from(tuple: ($(${ignore($T)} T,)+)) -> Self {
148                    let ($($T,)+) = tuple;
149                    [$($T,)+]
150                }
151            }
152        }
153
154        maybe_tuple_doc! {
155            $($T)+ @
156            // SAFETY: tuples introduce no additional indirection, so they can be copied whenever T
157            // can.
158            #[unstable(feature = "cell_get_cloned", issue = "145329")]
159            unsafe impl<$($T: CloneFromCell),+> CloneFromCell for ($($T,)+)
160            {}
161        }
162    }
163}
164
165// If this is a unary tuple, it adds a doc comment.
166// Otherwise, it hides the docs entirely.
167macro_rules! maybe_tuple_doc {
168    ($a:ident @ #[$meta:meta] $item:item) => {
169        #[doc(fake_variadic)]
170        #[doc = "This trait is implemented for tuples up to twelve items long."]
171        #[$meta]
172        $item
173    };
174    ($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
175        #[doc(hidden)]
176        #[$meta]
177        $item
178    };
179}
180
181// Constructs an expression that performs a lexical ordering using method `$rel`.
182// The values are interleaved, so the macro invocation for
183// `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, opt_is_lt, a1, b1,
184// a2, b2, a3, b3)` (and similarly for `lexical_cmp`)
185//
186// `$chain_rel` is the chaining method from `PartialOrd` to use for all but the
187// final value, to produce better results for simple primitives.
188macro_rules! lexical_ord {
189    ($rel: ident, $chain_rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {{
190        match PartialOrd::$chain_rel(&$a, &$b) {
191            Break(val) => val,
192            Continue(()) => lexical_ord!($rel, $chain_rel, $($rest_a, $rest_b),+),
193        }
194    }};
195    ($rel: ident, $chain_rel: ident, $a:expr, $b:expr) => {
196        // Use the specific method for the last element
197        PartialOrd::$rel(&$a, &$b)
198    };
199}
200
201// Same parameter interleaving as `lexical_ord` above
202macro_rules! lexical_chain {
203    ($chain_rel:ident $(, $a:expr, $b:expr)* $(,)?) => {{
204        // Using `?` here makes this harder on mir-opt due
205        // to the `Try` residuals bloating the function
206        $(if let r @ Break(_) = PartialOrd::$chain_rel(&$a, &$b) {
207            return r;
208        })*
209        Continue(())
210    }};
211}
212
213macro_rules! lexical_partial_cmp {
214    ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
215        match ($a).partial_cmp(&$b) {
216            Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
217            ordering => ordering
218        }
219    };
220    ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
221}
222
223macro_rules! lexical_cmp {
224    ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
225        match ($a).cmp(&$b) {
226            Equal => lexical_cmp!($($rest_a, $rest_b),+),
227            ordering => ordering
228        }
229    };
230    ($a:expr, $b:expr) => { ($a).cmp(&$b) };
231}
232
233tuple_impls!(E D C B A Z Y X W V U T);