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