Skip to content

Commit 01b6c94

Browse files
committed
Implement Eq/Hash/Debug etc. for unsized tuples.
1 parent 7a2c09b commit 01b6c94

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/libcore/fmt/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,13 +1627,13 @@ macro_rules! tuple {
16271627
() => ();
16281628
( $($name:ident,)+ ) => (
16291629
#[stable(feature = "rust1", since = "1.0.0")]
1630-
impl<$($name:Debug),*> Debug for ($($name,)*) {
1630+
impl<$($name:Debug),*> Debug for ($($name,)*) where last_type!($($name,)+): ?Sized {
16311631
#[allow(non_snake_case, unused_assignments, deprecated)]
16321632
fn fmt(&self, f: &mut Formatter) -> Result {
16331633
let mut builder = f.debug_tuple("");
16341634
let ($(ref $name,)*) = *self;
16351635
$(
1636-
builder.field($name);
1636+
builder.field(&$name);
16371637
)*
16381638

16391639
builder.finish()
@@ -1643,6 +1643,11 @@ macro_rules! tuple {
16431643
)
16441644
}
16451645

1646+
macro_rules! last_type {
1647+
($a:ident,) => { $a };
1648+
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
1649+
}
1650+
16461651
tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
16471652

16481653
#[stable(feature = "rust1", since = "1.0.0")]

src/libcore/hash/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ mod impls {
559559

560560
( $($name:ident)+) => (
561561
#[stable(feature = "rust1", since = "1.0.0")]
562-
impl<$($name: Hash),*> Hash for ($($name,)*) {
562+
impl<$($name: Hash),*> Hash for ($($name,)*) where last_type!($($name,)+): ?Sized {
563563
#[allow(non_snake_case)]
564564
fn hash<S: Hasher>(&self, state: &mut S) {
565565
let ($(ref $name,)*) = *self;
@@ -569,6 +569,11 @@ mod impls {
569569
);
570570
}
571571

572+
macro_rules! last_type {
573+
($a:ident,) => { $a };
574+
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
575+
}
576+
572577
impl_hash_tuple! {}
573578
impl_hash_tuple! { A }
574579
impl_hash_tuple! { A B }

src/libcore/tuple.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ macro_rules! tuple_impls {
2929
}
3030

3131
#[stable(feature = "rust1", since = "1.0.0")]
32-
impl<$($T:PartialEq),+> PartialEq for ($($T,)+) {
32+
impl<$($T:PartialEq),+> PartialEq for ($($T,)+) where last_type!($($T,)+): ?Sized {
3333
#[inline]
3434
fn eq(&self, other: &($($T,)+)) -> bool {
3535
$(self.$idx == other.$idx)&&+
@@ -41,10 +41,11 @@ macro_rules! tuple_impls {
4141
}
4242

4343
#[stable(feature = "rust1", since = "1.0.0")]
44-
impl<$($T:Eq),+> Eq for ($($T,)+) {}
44+
impl<$($T:Eq),+> Eq for ($($T,)+) where last_type!($($T,)+): ?Sized {}
4545

4646
#[stable(feature = "rust1", since = "1.0.0")]
47-
impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) {
47+
impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+)
48+
where last_type!($($T,)+): ?Sized {
4849
#[inline]
4950
fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
5051
lexical_partial_cmp!($(self.$idx, other.$idx),+)
@@ -68,7 +69,7 @@ macro_rules! tuple_impls {
6869
}
6970

7071
#[stable(feature = "rust1", since = "1.0.0")]
71-
impl<$($T:Ord),+> Ord for ($($T,)+) {
72+
impl<$($T:Ord),+> Ord for ($($T,)+) where last_type!($($T,)+): ?Sized {
7273
#[inline]
7374
fn cmp(&self, other: &($($T,)+)) -> Ordering {
7475
lexical_cmp!($(self.$idx, other.$idx),+)
@@ -118,6 +119,11 @@ macro_rules! lexical_cmp {
118119
($a:expr, $b:expr) => { ($a).cmp(&$b) };
119120
}
120121

122+
macro_rules! last_type {
123+
($a:ident,) => { $a };
124+
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
125+
}
126+
121127
tuple_impls! {
122128
Tuple1 {
123129
(0) -> A

0 commit comments

Comments
 (0)