2016-02-09 12:54:53 -05:00
|
|
|
// See src/libstd/primitive_docs.rs for documentation.
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2019-04-15 11:23:21 +09:00
|
|
|
use crate::cmp::Ordering::*;
|
|
|
|
use crate::cmp::*;
|
2013-05-18 19:43:14 +10:00
|
|
|
|
2022-04-12 16:23:36 -03:00
|
|
|
// 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,).
|
2013-05-19 18:51:14 +10:00
|
|
|
macro_rules! tuple_impls {
|
2022-04-12 16:23:36 -03:00
|
|
|
// 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 )+) => {
|
2022-06-07 11:23:06 -07:00
|
|
|
maybe_tuple_doc! {
|
|
|
|
$($T)+ @
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2022-11-07 21:47:46 +01:00
|
|
|
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
|
|
|
|
impl<$($T: ~const PartialEq),+> const PartialEq for ($($T,)+)
|
2022-06-07 11:23:06 -07:00
|
|
|
where
|
|
|
|
last_type!($($T,)+): ?Sized
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn eq(&self, other: &($($T,)+)) -> bool {
|
|
|
|
$( ${ignore(T)} self.${index()} == other.${index()} )&&+
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn ne(&self, other: &($($T,)+)) -> bool {
|
|
|
|
$( ${ignore(T)} self.${index()} != other.${index()} )||+
|
|
|
|
}
|
2013-11-29 15:52:38 +01:00
|
|
|
}
|
2022-04-12 16:23:36 -03:00
|
|
|
}
|
2012-08-27 16:26:35 -07:00
|
|
|
|
2022-06-07 11:23:06 -07:00
|
|
|
maybe_tuple_doc! {
|
|
|
|
$($T)+ @
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2022-11-07 21:47:46 +01:00
|
|
|
impl<$($T: Eq),+> Eq for ($($T,)+)
|
2022-06-07 11:23:06 -07:00
|
|
|
where
|
|
|
|
last_type!($($T,)+): ?Sized
|
|
|
|
{}
|
|
|
|
}
|
2013-05-19 18:51:14 +10:00
|
|
|
|
2022-06-07 11:23:06 -07:00
|
|
|
maybe_tuple_doc! {
|
|
|
|
$($T)+ @
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2022-11-07 21:47:46 +01:00
|
|
|
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
|
|
|
|
impl<$($T: ~const PartialOrd + ~const PartialEq),+> const PartialOrd for ($($T,)+)
|
2022-06-07 11:23:06 -07:00
|
|
|
where
|
|
|
|
last_type!($($T,)+): ?Sized
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
|
|
|
|
lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn lt(&self, other: &($($T,)+)) -> bool {
|
|
|
|
lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn le(&self, other: &($($T,)+)) -> bool {
|
|
|
|
lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn ge(&self, other: &($($T,)+)) -> bool {
|
|
|
|
lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
|
|
|
|
}
|
|
|
|
#[inline]
|
|
|
|
fn gt(&self, other: &($($T,)+)) -> bool {
|
|
|
|
lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
|
|
|
|
}
|
2022-04-12 16:23:36 -03:00
|
|
|
}
|
|
|
|
}
|
2013-05-19 18:51:14 +10:00
|
|
|
|
2022-06-07 11:23:06 -07:00
|
|
|
maybe_tuple_doc! {
|
|
|
|
$($T)+ @
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2022-11-07 21:47:46 +01:00
|
|
|
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
|
|
|
|
impl<$($T: ~const Ord),+> const Ord for ($($T,)+)
|
2022-06-07 11:23:06 -07:00
|
|
|
where
|
|
|
|
last_type!($($T,)+): ?Sized
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn cmp(&self, other: &($($T,)+)) -> Ordering {
|
|
|
|
lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
|
|
|
|
}
|
2013-11-29 15:52:38 +01:00
|
|
|
}
|
2022-04-12 16:23:36 -03:00
|
|
|
}
|
2013-06-14 18:27:52 -07:00
|
|
|
|
2022-06-07 11:23:06 -07:00
|
|
|
maybe_tuple_doc! {
|
|
|
|
$($T)+ @
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2022-09-23 17:53:59 +02:00
|
|
|
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
|
|
|
|
impl<$($T: ~const Default),+> const Default for ($($T,)+) {
|
2022-06-07 11:23:06 -07:00
|
|
|
#[inline]
|
|
|
|
fn default() -> ($($T,)+) {
|
|
|
|
($({ let x: $T = Default::default(); x},)+)
|
|
|
|
}
|
2013-11-29 15:52:38 +01:00
|
|
|
}
|
2022-04-12 16:23:36 -03:00
|
|
|
}
|
2013-05-19 18:51:14 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 21:47:03 -07:00
|
|
|
// If this is a unary tuple, it adds a doc comment.
|
|
|
|
// Otherwise, it hides the docs entirely.
|
2022-06-07 11:23:06 -07:00
|
|
|
macro_rules! maybe_tuple_doc {
|
|
|
|
($a:ident @ #[$meta:meta] $item:item) => {
|
2022-08-09 09:56:13 -04:00
|
|
|
#[doc(fake_variadic)]
|
2022-06-07 11:23:06 -07:00
|
|
|
#[doc = "This trait is implemented for tuples up to twelve items long."]
|
|
|
|
#[$meta]
|
|
|
|
$item
|
|
|
|
};
|
|
|
|
($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[$meta]
|
|
|
|
$item
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-08-08 22:07:22 +02: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 18:51:14 +10:00
|
|
|
// a3, b3)` (and similarly for `lexical_cmp`)
|
2013-08-08 22:07:22 +02:00
|
|
|
macro_rules! lexical_ord {
|
|
|
|
($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
|
2014-12-09 11:44:56 -05:00
|
|
|
if $a != $b { lexical_ord!($rel, $a, $b) }
|
2013-08-08 22:07:22 +02:00
|
|
|
else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
|
2013-05-19 18:51:14 +10:00
|
|
|
};
|
2014-12-09 11:44:56 -05:00
|
|
|
($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
|
2013-05-19 18:51:14 +10:00
|
|
|
}
|
|
|
|
|
2014-06-17 23:25:51 -07:00
|
|
|
macro_rules! lexical_partial_cmp {
|
|
|
|
($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
|
2014-12-09 11:44:56 -05:00
|
|
|
match ($a).partial_cmp(&$b) {
|
2014-06-17 23:25:51 -07:00
|
|
|
Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
|
|
|
|
ordering => ordering
|
|
|
|
}
|
|
|
|
};
|
2014-12-09 11:44:56 -05:00
|
|
|
($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
|
2014-06-17 23:25:51 -07:00
|
|
|
}
|
|
|
|
|
2013-05-19 18:51:14 +10:00
|
|
|
macro_rules! lexical_cmp {
|
|
|
|
($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
|
2014-12-09 11:44:56 -05:00
|
|
|
match ($a).cmp(&$b) {
|
2013-05-19 18:51:14 +10:00
|
|
|
Equal => lexical_cmp!($($rest_a, $rest_b),+),
|
|
|
|
ordering => ordering
|
|
|
|
}
|
|
|
|
};
|
2014-12-09 11:44:56 -05:00
|
|
|
($a:expr, $b:expr) => { ($a).cmp(&$b) };
|
2013-05-19 18:51:14 +10:00
|
|
|
}
|
|
|
|
|
2017-07-02 08:41:39 +09:00
|
|
|
macro_rules! last_type {
|
|
|
|
($a:ident,) => { $a };
|
|
|
|
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
|
|
|
|
}
|
|
|
|
|
2022-06-11 10:32:39 -07:00
|
|
|
tuple_impls!(E D C B A Z Y X W V U T);
|