Skip to content

Commit 6aa875a

Browse files
authored
Rollup merge of #95914 - c410-f3r:meta-vars, r=petrochenkov
Implement tuples using recursion Because it is c00l3r™, requires less repetition and can be used as a reference for external people. This change is non-essential and I am not sure about potential performance impacts so feel free to close this PR if desired. r? `@petrochenkov`
2 parents 2743c13 + 23bf977 commit 6aa875a

File tree

1 file changed

+74
-67
lines changed

1 file changed

+74
-67
lines changed

library/core/src/tuple.rs

+74-67
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,88 @@
33
use crate::cmp::Ordering::*;
44
use crate::cmp::*;
55

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,).
710
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()} )||+
2034
}
35+
}
2136

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+
{}
2442

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()} ),+)
5059
}
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+
}
5169

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()} ),+)
5878
}
79+
}
5980

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},)+)
6686
}
67-
)+
87+
}
6888
}
6989
}
7090

@@ -105,17 +125,4 @@ macro_rules! last_type {
105125
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
106126
}
107127

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

Comments
 (0)