Implement Eq/Hash/Debug etc. for unsized tuples.

This commit is contained in:
Masaki Hara 2017-07-02 08:41:39 +09:00
parent 7a2c09b6f5
commit 01b6c9459c
No known key found for this signature in database
GPG Key ID: 7CA7A85E049A82E8
3 changed files with 23 additions and 7 deletions

View File

@ -1627,13 +1627,13 @@ macro_rules! tuple {
() => (); () => ();
( $($name:ident,)+ ) => ( ( $($name:ident,)+ ) => (
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<$($name:Debug),*> Debug for ($($name,)*) { impl<$($name:Debug),*> Debug for ($($name,)*) where last_type!($($name,)+): ?Sized {
#[allow(non_snake_case, unused_assignments, deprecated)] #[allow(non_snake_case, unused_assignments, deprecated)]
fn fmt(&self, f: &mut Formatter) -> Result { fn fmt(&self, f: &mut Formatter) -> Result {
let mut builder = f.debug_tuple(""); let mut builder = f.debug_tuple("");
let ($(ref $name,)*) = *self; let ($(ref $name,)*) = *self;
$( $(
builder.field($name); builder.field(&$name);
)* )*
builder.finish() builder.finish()
@ -1643,6 +1643,11 @@ fn fmt(&self, f: &mut Formatter) -> Result {
) )
} }
macro_rules! last_type {
($a:ident,) => { $a };
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
}
tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, } tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]

View File

@ -559,7 +559,7 @@ fn hash<H: Hasher>(&self, _state: &mut H) {}
( $($name:ident)+) => ( ( $($name:ident)+) => (
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<$($name: Hash),*> Hash for ($($name,)*) { impl<$($name: Hash),*> Hash for ($($name,)*) where last_type!($($name,)+): ?Sized {
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn hash<S: Hasher>(&self, state: &mut S) { fn hash<S: Hasher>(&self, state: &mut S) {
let ($(ref $name,)*) = *self; let ($(ref $name,)*) = *self;
@ -569,6 +569,11 @@ fn hash<S: Hasher>(&self, state: &mut S) {
); );
} }
macro_rules! last_type {
($a:ident,) => { $a };
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
}
impl_hash_tuple! {} impl_hash_tuple! {}
impl_hash_tuple! { A } impl_hash_tuple! { A }
impl_hash_tuple! { A B } impl_hash_tuple! { A B }

View File

@ -29,7 +29,7 @@ fn clone(&self) -> ($($T,)+) {
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<$($T:PartialEq),+> PartialEq for ($($T,)+) { impl<$($T:PartialEq),+> PartialEq for ($($T,)+) where last_type!($($T,)+): ?Sized {
#[inline] #[inline]
fn eq(&self, other: &($($T,)+)) -> bool { fn eq(&self, other: &($($T,)+)) -> bool {
$(self.$idx == other.$idx)&&+ $(self.$idx == other.$idx)&&+
@ -41,10 +41,11 @@ fn ne(&self, other: &($($T,)+)) -> bool {
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<$($T:Eq),+> Eq for ($($T,)+) {} impl<$($T:Eq),+> Eq for ($($T,)+) where last_type!($($T,)+): ?Sized {}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+) { impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+)
where last_type!($($T,)+): ?Sized {
#[inline] #[inline]
fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> { fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
lexical_partial_cmp!($(self.$idx, other.$idx),+) lexical_partial_cmp!($(self.$idx, other.$idx),+)
@ -68,7 +69,7 @@ fn gt(&self, other: &($($T,)+)) -> bool {
} }
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<$($T:Ord),+> Ord for ($($T,)+) { impl<$($T:Ord),+> Ord for ($($T,)+) where last_type!($($T,)+): ?Sized {
#[inline] #[inline]
fn cmp(&self, other: &($($T,)+)) -> Ordering { fn cmp(&self, other: &($($T,)+)) -> Ordering {
lexical_cmp!($(self.$idx, other.$idx),+) lexical_cmp!($(self.$idx, other.$idx),+)
@ -118,6 +119,11 @@ macro_rules! lexical_cmp {
($a:expr, $b:expr) => { ($a).cmp(&$b) }; ($a:expr, $b:expr) => { ($a).cmp(&$b) };
} }
macro_rules! last_type {
($a:ident,) => { $a };
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
}
tuple_impls! { tuple_impls! {
Tuple1 { Tuple1 {
(0) -> A (0) -> A