2016-02-09 17:54:53 +00:00
|
|
|
// See src/libstd/primitive_docs.rs for documentation.
|
2013-05-28 21:35:52 +00:00
|
|
|
|
2019-04-15 02:23:21 +00:00
|
|
|
use crate::cmp::Ordering::*;
|
|
|
|
use crate::cmp::*;
|
2013-05-18 09:43:14 +00:00
|
|
|
|
2013-05-18 11:38:06 +00:00
|
|
|
// macro for implementing n-ary tuple functions and operations
|
2013-05-19 08:51:14 +00:00
|
|
|
macro_rules! tuple_impls {
|
2022-04-08 13:30:24 +00:00
|
|
|
( $( ( $( $T:ident )+ ) )+ ) => {
|
2013-11-29 14:52:38 +00:00
|
|
|
$(
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2017-07-01 23:41:39 +00:00
|
|
|
impl<$($T:PartialEq),+> PartialEq for ($($T,)+) where last_type!($($T,)+): ?Sized {
|
2013-11-29 14:52:38 +00:00
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &($($T,)+)) -> bool {
|
2022-04-07 11:13:41 +00:00
|
|
|
$( ${ignore(T)} self.${index()} == other.${index()} )&&+
|
2013-11-29 14:52:38 +00:00
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn ne(&self, other: &($($T,)+)) -> bool {
|
2022-04-07 11:13:41 +00:00
|
|
|
$( ${ignore(T)} self.${index()} != other.${index()} )||+
|
2012-11-15 02:59:30 +00:00
|
|
|
}
|
2013-11-29 14:52:38 +00:00
|
|
|
}
|
2012-08-27 23:26:35 +00:00
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2017-07-01 23:41:39 +00:00
|
|
|
impl<$($T:Eq),+> Eq for ($($T,)+) where last_type!($($T,)+): ?Sized {}
|
2013-05-19 08:51:14 +00:00
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2017-07-01 23:41:39 +00:00
|
|
|
impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+)
|
2022-04-07 11:13:41 +00:00
|
|
|
where
|
|
|
|
last_type!($($T,)+): ?Sized
|
|
|
|
{
|
2014-06-18 06:25:51 +00:00
|
|
|
#[inline]
|
|
|
|
fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
|
2022-04-07 11:13:41 +00:00
|
|
|
lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
|
2014-06-18 06:25:51 +00:00
|
|
|
}
|
2013-11-29 14:52:38 +00:00
|
|
|
#[inline]
|
|
|
|
fn lt(&self, other: &($($T,)+)) -> bool {
|
2022-04-07 11:13:41 +00:00
|
|
|
lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
|
2013-11-29 14:52:38 +00:00
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn le(&self, other: &($($T,)+)) -> bool {
|
2022-04-07 11:13:41 +00:00
|
|
|
lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
|
2013-11-29 14:52:38 +00:00
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn ge(&self, other: &($($T,)+)) -> bool {
|
2022-04-07 11:13:41 +00:00
|
|
|
lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
|
2013-05-18 11:38:06 +00:00
|
|
|
}
|
2013-11-29 14:52:38 +00:00
|
|
|
#[inline]
|
|
|
|
fn gt(&self, other: &($($T,)+)) -> bool {
|
2022-04-07 11:13:41 +00:00
|
|
|
lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
|
2013-11-29 14:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-19 08:51:14 +00:00
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2017-07-01 23:41:39 +00:00
|
|
|
impl<$($T:Ord),+> Ord for ($($T,)+) where last_type!($($T,)+): ?Sized {
|
2013-11-29 14:52:38 +00:00
|
|
|
#[inline]
|
|
|
|
fn cmp(&self, other: &($($T,)+)) -> Ordering {
|
2022-04-07 11:13:41 +00:00
|
|
|
lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
|
2013-05-19 08:51:14 +00:00
|
|
|
}
|
2013-11-29 14:52:38 +00:00
|
|
|
}
|
2013-06-15 01:27:52 +00:00
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-11-29 14:52:38 +00:00
|
|
|
impl<$($T:Default),+> Default for ($($T,)+) {
|
|
|
|
#[inline]
|
|
|
|
fn default() -> ($($T,)+) {
|
|
|
|
($({ let x: $T = Default::default(); x},)+)
|
2013-09-12 04:49:25 +00:00
|
|
|
}
|
2013-11-29 14:52:38 +00:00
|
|
|
}
|
|
|
|
)+
|
2013-05-19 08:51:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 20:07:22 +00:00
|
|
|
// Constructs an expression that performs a lexical ordering using method $rel.
|
|
|
|
// The values are interleaved, so the macro invocation for
|
|
|
|
// `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
|
2013-05-19 08:51:14 +00:00
|
|
|
// a3, b3)` (and similarly for `lexical_cmp`)
|
2013-08-08 20:07:22 +00:00
|
|
|
macro_rules! lexical_ord {
|
|
|
|
($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
|
2014-12-09 16:44:56 +00:00
|
|
|
if $a != $b { lexical_ord!($rel, $a, $b) }
|
2013-08-08 20:07:22 +00:00
|
|
|
else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
|
2013-05-19 08:51:14 +00:00
|
|
|
};
|
2014-12-09 16:44:56 +00:00
|
|
|
($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
|
2013-05-19 08:51:14 +00:00
|
|
|
}
|
|
|
|
|
2014-06-18 06:25:51 +00:00
|
|
|
macro_rules! lexical_partial_cmp {
|
|
|
|
($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
|
2014-12-09 16:44:56 +00:00
|
|
|
match ($a).partial_cmp(&$b) {
|
2014-06-18 06:25:51 +00:00
|
|
|
Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
|
|
|
|
ordering => ordering
|
|
|
|
}
|
|
|
|
};
|
2014-12-09 16:44:56 +00:00
|
|
|
($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
|
2014-06-18 06:25:51 +00:00
|
|
|
}
|
|
|
|
|
2013-05-19 08:51:14 +00:00
|
|
|
macro_rules! lexical_cmp {
|
|
|
|
($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
|
2014-12-09 16:44:56 +00:00
|
|
|
match ($a).cmp(&$b) {
|
2013-05-19 08:51:14 +00:00
|
|
|
Equal => lexical_cmp!($($rest_a, $rest_b),+),
|
|
|
|
ordering => ordering
|
|
|
|
}
|
|
|
|
};
|
2014-12-09 16:44:56 +00:00
|
|
|
($a:expr, $b:expr) => { ($a).cmp(&$b) };
|
2013-05-19 08:51:14 +00:00
|
|
|
}
|
|
|
|
|
2017-07-01 23:41:39 +00:00
|
|
|
macro_rules! last_type {
|
|
|
|
($a:ident,) => { $a };
|
|
|
|
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
|
|
|
|
}
|
|
|
|
|
2013-05-19 08:51:14 +00:00
|
|
|
tuple_impls! {
|
2022-04-08 13:30:24 +00:00
|
|
|
(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)
|
2013-05-19 08:51:14 +00:00
|
|
|
}
|