]> git.lizzy.rs Git - rust.git/blob - library/core/src/tuple.rs
Rollup merge of #104402 - joboet:sync_remutex, r=m-ou-se
[rust.git] / library / core / src / tuple.rs
1 // See src/libstd/primitive_docs.rs for documentation.
2
3 use crate::cmp::Ordering::*;
4 use crate::cmp::*;
5
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,).
10 macro_rules! tuple_impls {
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 )+) => {
22         maybe_tuple_doc! {
23             $($T)+ @
24             #[stable(feature = "rust1", since = "1.0.0")]
25             #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
26             impl<$($T: ~const PartialEq),+> const PartialEq for ($($T,)+)
27             where
28                 last_type!($($T,)+): ?Sized
29             {
30                 #[inline]
31                 fn eq(&self, other: &($($T,)+)) -> bool {
32                     $( ${ignore(T)} self.${index()} == other.${index()} )&&+
33                 }
34                 #[inline]
35                 fn ne(&self, other: &($($T,)+)) -> bool {
36                     $( ${ignore(T)} self.${index()} != other.${index()} )||+
37                 }
38             }
39         }
40
41         maybe_tuple_doc! {
42             $($T)+ @
43             #[stable(feature = "rust1", since = "1.0.0")]
44             impl<$($T: Eq),+> Eq for ($($T,)+)
45             where
46                 last_type!($($T,)+): ?Sized
47             {}
48         }
49
50         maybe_tuple_doc! {
51             $($T)+ @
52             #[stable(feature = "rust1", since = "1.0.0")]
53             #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
54             impl<$($T: ~const PartialOrd + ~const PartialEq),+> const PartialOrd for ($($T,)+)
55             where
56                 last_type!($($T,)+): ?Sized
57             {
58                 #[inline]
59                 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
60                     lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
61                 }
62                 #[inline]
63                 fn lt(&self, other: &($($T,)+)) -> bool {
64                     lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
65                 }
66                 #[inline]
67                 fn le(&self, other: &($($T,)+)) -> bool {
68                     lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
69                 }
70                 #[inline]
71                 fn ge(&self, other: &($($T,)+)) -> bool {
72                     lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
73                 }
74                 #[inline]
75                 fn gt(&self, other: &($($T,)+)) -> bool {
76                     lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
77                 }
78             }
79         }
80
81         maybe_tuple_doc! {
82             $($T)+ @
83             #[stable(feature = "rust1", since = "1.0.0")]
84             #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
85             impl<$($T: ~const Ord),+> const Ord for ($($T,)+)
86             where
87                 last_type!($($T,)+): ?Sized
88             {
89                 #[inline]
90                 fn cmp(&self, other: &($($T,)+)) -> Ordering {
91                     lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
92                 }
93             }
94         }
95
96         maybe_tuple_doc! {
97             $($T)+ @
98             #[stable(feature = "rust1", since = "1.0.0")]
99             #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
100             impl<$($T: ~const Default),+> const Default for ($($T,)+) {
101                 #[inline]
102                 fn default() -> ($($T,)+) {
103                     ($({ let x: $T = Default::default(); x},)+)
104                 }
105             }
106         }
107     }
108 }
109
110 // If this is a unary tuple, it adds a doc comment.
111 // Otherwise, it hides the docs entirely.
112 macro_rules! maybe_tuple_doc {
113     ($a:ident @ #[$meta:meta] $item:item) => {
114         #[doc(fake_variadic)]
115         #[doc = "This trait is implemented for tuples up to twelve items long."]
116         #[$meta]
117         $item
118     };
119     ($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
120         #[doc(hidden)]
121         #[$meta]
122         $item
123     };
124 }
125
126 // Constructs an expression that performs a lexical ordering using method $rel.
127 // The values are interleaved, so the macro invocation for
128 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
129 // a3, b3)` (and similarly for `lexical_cmp`)
130 macro_rules! lexical_ord {
131     ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
132         if $a != $b { lexical_ord!($rel, $a, $b) }
133         else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
134     };
135     ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
136 }
137
138 macro_rules! lexical_partial_cmp {
139     ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
140         match ($a).partial_cmp(&$b) {
141             Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
142             ordering   => ordering
143         }
144     };
145     ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
146 }
147
148 macro_rules! lexical_cmp {
149     ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
150         match ($a).cmp(&$b) {
151             Equal => lexical_cmp!($($rest_a, $rest_b),+),
152             ordering   => ordering
153         }
154     };
155     ($a:expr, $b:expr) => { ($a).cmp(&$b) };
156 }
157
158 macro_rules! last_type {
159     ($a:ident,) => { $a };
160     ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
161 }
162
163 tuple_impls!(E D C B A Z Y X W V U T);