Implement tuples using recursion
This commit is contained in:
parent
1f7fb6413d
commit
23bf977758
1 changed files with 77 additions and 70 deletions
|
@ -3,12 +3,27 @@
|
||||||
use crate::cmp::Ordering::*;
|
use crate::cmp::Ordering::*;
|
||||||
use crate::cmp::*;
|
use crate::cmp::*;
|
||||||
|
|
||||||
// macro for implementing n-ary tuple functions and operations
|
// Recursive macro for implementing n-ary tuple functions and operations
|
||||||
|
//
|
||||||
|
// Also provides implementations for tuples with lesser arity. For example, tuple_impls!(A B C)
|
||||||
|
// will implement everything for (A, B, C), (A, B) and (A,).
|
||||||
macro_rules! tuple_impls {
|
macro_rules! tuple_impls {
|
||||||
( $( ( $( $T:ident )+ ) )+ ) => {
|
// Stopping criteria (1-ary tuple)
|
||||||
$(
|
($T:ident) => {
|
||||||
|
tuple_impls!(@impl $T);
|
||||||
|
};
|
||||||
|
// Running criteria (n-ary tuple, with n >= 2)
|
||||||
|
($T:ident $( $U:ident )+) => {
|
||||||
|
tuple_impls!($( $U )+);
|
||||||
|
tuple_impls!(@impl $T $( $U )+);
|
||||||
|
};
|
||||||
|
// "Private" internal implementation
|
||||||
|
(@impl $( $T:ident )+) => {
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<$($T:PartialEq),+> PartialEq for ($($T,)+) where last_type!($($T,)+): ?Sized {
|
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 {
|
||||||
$( ${ignore(T)} self.${index()} == other.${index()} )&&+
|
$( ${ignore(T)} self.${index()} == other.${index()} )&&+
|
||||||
|
@ -20,7 +35,10 @@ macro_rules! tuple_impls {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<$($T:Eq),+> Eq for ($($T,)+) where last_type!($($T,)+): ?Sized {}
|
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,)+)
|
||||||
|
@ -50,7 +68,10 @@ macro_rules! tuple_impls {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<$($T:Ord),+> Ord for ($($T,)+) where last_type!($($T,)+): ?Sized {
|
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!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
|
lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
|
||||||
|
@ -64,7 +85,6 @@ macro_rules! tuple_impls {
|
||||||
($({ let x: $T = Default::default(); x},)+)
|
($({ let x: $T = Default::default(); x},)+)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)+
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,17 +125,4 @@ macro_rules! last_type {
|
||||||
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
|
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
|
||||||
}
|
}
|
||||||
|
|
||||||
tuple_impls! {
|
tuple_impls!(A B C D E F G H I J K L);
|
||||||
(A)
|
|
||||||
(A B)
|
|
||||||
(A B C)
|
|
||||||
(A B C D)
|
|
||||||
(A B C D E)
|
|
||||||
(A B C D E F)
|
|
||||||
(A B C D E F G)
|
|
||||||
(A B C D E F G H)
|
|
||||||
(A B C D E F G H I)
|
|
||||||
(A B C D E F G H I J)
|
|
||||||
(A B C D E F G H I J K)
|
|
||||||
(A B C D E F G H I J K L)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue