Skip to content

Commit 0bc5699

Browse files
Relax tuple comparison impls
1 parent 536635c commit 0bc5699

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

library/core/src/tuple.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,37 @@ use crate::marker::{StructuralEq, StructuralPartialEq};
1111
// will implement everything for (A, B, C), (A, B) and (A,).
1212
macro_rules! tuple_impls {
1313
// Stopping criteria (1-ary tuple)
14-
($T:ident) => {
15-
tuple_impls!(@impl $T);
14+
($T:ident $U:ident) => {
15+
tuple_impls!(@impl $T $U);
1616
};
1717
// Running criteria (n-ary tuple, with n >= 2)
18-
($T:ident $( $U:ident )+) => {
19-
tuple_impls!($( $U )+);
20-
tuple_impls!(@impl $T $( $U )+);
18+
($T:ident $U:ident $( $rest:ident )+) => {
19+
tuple_impls!($( $rest )+);
20+
tuple_impls!(@impl $T $U $( $rest )+);
2121
};
2222
// "Private" internal implementation
23-
(@impl $( $T:ident )+) => {
23+
(@impl $( $T:ident $U:ident )+) => {
2424
maybe_tuple_doc! {
25-
$($T)+ @
25+
$($T $U)+ @
2626
#[stable(feature = "rust1", since = "1.0.0")]
27-
impl<$($T: PartialEq),+> PartialEq for ($($T,)+)
27+
impl<$($T: PartialEq<$U>,)+ $($U,)+> PartialEq<($($U,)+)> for ($($T,)+)
2828
where
29-
last_type!($($T,)+): ?Sized
29+
last_type!($($T,)+): ?Sized,
30+
last_type!($($U,)+): ?Sized
3031
{
3132
#[inline]
32-
fn eq(&self, other: &($($T,)+)) -> bool {
33+
fn eq(&self, other: &($($U,)+)) -> bool {
3334
$( ${ignore(T)} self.${index()} == other.${index()} )&&+
3435
}
3536
#[inline]
36-
fn ne(&self, other: &($($T,)+)) -> bool {
37+
fn ne(&self, other: &($($U,)+)) -> bool {
3738
$( ${ignore(T)} self.${index()} != other.${index()} )||+
3839
}
3940
}
4041
}
4142

4243
maybe_tuple_doc! {
43-
$($T)+ @
44+
$($T $U)+ @
4445
#[stable(feature = "rust1", since = "1.0.0")]
4546
impl<$($T: Eq),+> Eq for ($($T,)+)
4647
where
@@ -49,73 +50,74 @@ macro_rules! tuple_impls {
4950
}
5051

5152
maybe_tuple_doc! {
52-
$($T)+ @
53+
$($T $U)+ @
5354
#[unstable(feature = "structural_match", issue = "31434")]
5455
#[cfg(not(bootstrap))]
5556
impl<$($T: ConstParamTy),+> ConstParamTy for ($($T,)+)
5657
{}
5758
}
5859

5960
maybe_tuple_doc! {
60-
$($T)+ @
61+
$($T $U)+ @
6162
#[unstable(feature = "structural_match", issue = "31434")]
6263
impl<$($T),+> StructuralPartialEq for ($($T,)+)
6364
{}
6465
}
6566

6667
maybe_tuple_doc! {
67-
$($T)+ @
68+
$($T $U)+ @
6869
#[unstable(feature = "structural_match", issue = "31434")]
6970
impl<$($T),+> StructuralEq for ($($T,)+)
7071
{}
7172
}
7273

7374
maybe_tuple_doc! {
74-
$($T)+ @
75+
$($T $U)+ @
7576
#[stable(feature = "rust1", since = "1.0.0")]
76-
impl<$($T: PartialOrd),+> PartialOrd for ($($T,)+)
77+
impl<$($T: PartialOrd<$U>,)+ $($U,)+> PartialOrd<($($U,)+)> for ($($T,)+)
7778
where
78-
last_type!($($T,)+): ?Sized
79+
last_type!($($T,)+): ?Sized,
80+
last_type!($($U,)+): ?Sized
7981
{
8082
#[inline]
81-
fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
83+
fn partial_cmp(&self, other: &($($U,)+)) -> Option<Ordering> {
8284
lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
8385
}
8486
#[inline]
85-
fn lt(&self, other: &($($T,)+)) -> bool {
87+
fn lt(&self, other: &($($U,)+)) -> bool {
8688
lexical_ord!(lt, Less, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
8789
}
8890
#[inline]
89-
fn le(&self, other: &($($T,)+)) -> bool {
91+
fn le(&self, other: &($($U,)+)) -> bool {
9092
lexical_ord!(le, Less, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
9193
}
9294
#[inline]
93-
fn ge(&self, other: &($($T,)+)) -> bool {
95+
fn ge(&self, other: &($($U,)+)) -> bool {
9496
lexical_ord!(ge, Greater, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
9597
}
9698
#[inline]
97-
fn gt(&self, other: &($($T,)+)) -> bool {
99+
fn gt(&self, other: &($($U,)+)) -> bool {
98100
lexical_ord!(gt, Greater, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
99101
}
100102
}
101103
}
102104

103105
maybe_tuple_doc! {
104-
$($T)+ @
106+
$($T $U)+ @
105107
#[stable(feature = "rust1", since = "1.0.0")]
106108
impl<$($T: Ord),+> Ord for ($($T,)+)
107109
where
108110
last_type!($($T,)+): ?Sized
109111
{
110112
#[inline]
111-
fn cmp(&self, other: &($($T,)+)) -> Ordering {
113+
fn cmp(&self, other: &Self) -> Ordering {
112114
lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
113115
}
114116
}
115117
}
116118

117119
maybe_tuple_doc! {
118-
$($T)+ @
120+
$($T $U)+ @
119121
#[stable(feature = "rust1", since = "1.0.0")]
120122
impl<$($T: Default),+> Default for ($($T,)+) {
121123
#[inline]
@@ -219,4 +221,4 @@ macro_rules! last_type {
219221
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
220222
}
221223

222-
tuple_impls!(E D C B A Z Y X W V U T);
224+
tuple_impls!(E1 E2 D1 D2 C1 C2 B1 B2 A1 A2 Z1 Z2 Y1 Y2 X1 X2 W1 W2 V1 V2 U1 U2 T1 T2);

0 commit comments

Comments
 (0)