|
3 | 3 | use crate::cmp::Ordering::*; |
4 | 4 | use crate::cmp::*; |
5 | 5 |
|
6 | | -// macro for implementing n-ary tuple functions and operations |
| 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,). |
7 | 10 | macro_rules! tuple_impls { |
8 | | - ( $( ( $( $T:ident )+ ) )+ ) => { |
9 | | - $( |
10 | | - #[stable(feature = "rust1", since = "1.0.0")] |
11 | | - impl<$($T:PartialEq),+> PartialEq for ($($T,)+) where last_type!($($T,)+): ?Sized { |
12 | | - #[inline] |
13 | | - fn eq(&self, other: &($($T,)+)) -> bool { |
14 | | - $( ${ignore(T)} self.${index()} == other.${index()} )&&+ |
15 | | - } |
16 | | - #[inline] |
17 | | - fn ne(&self, other: &($($T,)+)) -> bool { |
18 | | - $( ${ignore(T)} self.${index()} != other.${index()} )||+ |
19 | | - } |
| 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 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 23 | + impl<$($T:PartialEq),+> PartialEq for ($($T,)+) |
| 24 | + where |
| 25 | + last_type!($($T,)+): ?Sized |
| 26 | + { |
| 27 | + #[inline] |
| 28 | + fn eq(&self, other: &($($T,)+)) -> bool { |
| 29 | + $( ${ignore(T)} self.${index()} == other.${index()} )&&+ |
| 30 | + } |
| 31 | + #[inline] |
| 32 | + fn ne(&self, other: &($($T,)+)) -> bool { |
| 33 | + $( ${ignore(T)} self.${index()} != other.${index()} )||+ |
20 | 34 | } |
| 35 | + } |
21 | 36 |
|
22 | | - #[stable(feature = "rust1", since = "1.0.0")] |
23 | | - impl<$($T:Eq),+> Eq for ($($T,)+) where last_type!($($T,)+): ?Sized {} |
| 37 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 38 | + impl<$($T:Eq),+> Eq for ($($T,)+) |
| 39 | + where |
| 40 | + last_type!($($T,)+): ?Sized |
| 41 | + {} |
24 | 42 |
|
25 | | - #[stable(feature = "rust1", since = "1.0.0")] |
26 | | - impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) |
27 | | - where |
28 | | - last_type!($($T,)+): ?Sized |
29 | | - { |
30 | | - #[inline] |
31 | | - fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> { |
32 | | - lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+) |
33 | | - } |
34 | | - #[inline] |
35 | | - fn lt(&self, other: &($($T,)+)) -> bool { |
36 | | - lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+) |
37 | | - } |
38 | | - #[inline] |
39 | | - fn le(&self, other: &($($T,)+)) -> bool { |
40 | | - lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+) |
41 | | - } |
42 | | - #[inline] |
43 | | - fn ge(&self, other: &($($T,)+)) -> bool { |
44 | | - lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+) |
45 | | - } |
46 | | - #[inline] |
47 | | - fn gt(&self, other: &($($T,)+)) -> bool { |
48 | | - lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+) |
49 | | - } |
| 43 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 44 | + impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) |
| 45 | + where |
| 46 | + last_type!($($T,)+): ?Sized |
| 47 | + { |
| 48 | + #[inline] |
| 49 | + fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> { |
| 50 | + lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+) |
| 51 | + } |
| 52 | + #[inline] |
| 53 | + fn lt(&self, other: &($($T,)+)) -> bool { |
| 54 | + lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+) |
| 55 | + } |
| 56 | + #[inline] |
| 57 | + fn le(&self, other: &($($T,)+)) -> bool { |
| 58 | + lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+) |
50 | 59 | } |
| 60 | + #[inline] |
| 61 | + fn ge(&self, other: &($($T,)+)) -> bool { |
| 62 | + lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+) |
| 63 | + } |
| 64 | + #[inline] |
| 65 | + fn gt(&self, other: &($($T,)+)) -> bool { |
| 66 | + lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+) |
| 67 | + } |
| 68 | + } |
51 | 69 |
|
52 | | - #[stable(feature = "rust1", since = "1.0.0")] |
53 | | - impl<$($T:Ord),+> Ord for ($($T,)+) where last_type!($($T,)+): ?Sized { |
54 | | - #[inline] |
55 | | - fn cmp(&self, other: &($($T,)+)) -> Ordering { |
56 | | - lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+) |
57 | | - } |
| 70 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 71 | + impl<$($T:Ord),+> Ord for ($($T,)+) |
| 72 | + where |
| 73 | + last_type!($($T,)+): ?Sized |
| 74 | + { |
| 75 | + #[inline] |
| 76 | + fn cmp(&self, other: &($($T,)+)) -> Ordering { |
| 77 | + lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+) |
58 | 78 | } |
| 79 | + } |
59 | 80 |
|
60 | | - #[stable(feature = "rust1", since = "1.0.0")] |
61 | | - impl<$($T:Default),+> Default for ($($T,)+) { |
62 | | - #[inline] |
63 | | - fn default() -> ($($T,)+) { |
64 | | - ($({ let x: $T = Default::default(); x},)+) |
65 | | - } |
| 81 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 82 | + impl<$($T:Default),+> Default for ($($T,)+) { |
| 83 | + #[inline] |
| 84 | + fn default() -> ($($T,)+) { |
| 85 | + ($({ let x: $T = Default::default(); x},)+) |
66 | 86 | } |
67 | | - )+ |
| 87 | + } |
68 | 88 | } |
69 | 89 | } |
70 | 90 |
|
@@ -105,17 +125,4 @@ macro_rules! last_type { |
105 | 125 | ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) }; |
106 | 126 | } |
107 | 127 |
|
108 | | -tuple_impls! { |
109 | | - (A) |
110 | | - (A B) |
111 | | - (A B C) |
112 | | - (A B C D) |
113 | | - (A B C D E) |
114 | | - (A B C D E F) |
115 | | - (A B C D E F G) |
116 | | - (A B C D E F G H) |
117 | | - (A B C D E F G H I) |
118 | | - (A B C D E F G H I J) |
119 | | - (A B C D E F G H I J K) |
120 | | - (A B C D E F G H I J K L) |
121 | | -} |
| 128 | +tuple_impls!(A B C D E F G H I J K L); |
0 commit comments