1 // See src/libstd/primitive_docs.rs for documentation.
3 use crate::cmp::Ordering::*;
6 // macro for implementing n-ary tuple functions and operations
7 macro_rules! tuple_impls {
8 ( $( ( $( $T:ident )+ ) )+ ) => {
10 #[stable(feature = "rust1", since = "1.0.0")]
11 impl<$($T:PartialEq),+> PartialEq for ($($T,)+) where last_type!($($T,)+): ?Sized {
13 fn eq(&self, other: &($($T,)+)) -> bool {
14 $( ${ignore(T)} self.${index()} == other.${index()} )&&+
17 fn ne(&self, other: &($($T,)+)) -> bool {
18 $( ${ignore(T)} self.${index()} != other.${index()} )||+
22 #[stable(feature = "rust1", since = "1.0.0")]
23 impl<$($T:Eq),+> Eq for ($($T,)+) where last_type!($($T,)+): ?Sized {}
25 #[stable(feature = "rust1", since = "1.0.0")]
26 impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+)
28 last_type!($($T,)+): ?Sized
31 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
32 lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
35 fn lt(&self, other: &($($T,)+)) -> bool {
36 lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
39 fn le(&self, other: &($($T,)+)) -> bool {
40 lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
43 fn ge(&self, other: &($($T,)+)) -> bool {
44 lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
47 fn gt(&self, other: &($($T,)+)) -> bool {
48 lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
52 #[stable(feature = "rust1", since = "1.0.0")]
53 impl<$($T:Ord),+> Ord for ($($T,)+) where last_type!($($T,)+): ?Sized {
55 fn cmp(&self, other: &($($T,)+)) -> Ordering {
56 lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
60 #[stable(feature = "rust1", since = "1.0.0")]
61 impl<$($T:Default),+> Default for ($($T,)+) {
63 fn default() -> ($($T,)+) {
64 ($({ let x: $T = Default::default(); x},)+)
71 // Constructs an expression that performs a lexical ordering using method $rel.
72 // The values are interleaved, so the macro invocation for
73 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
74 // a3, b3)` (and similarly for `lexical_cmp`)
75 macro_rules! lexical_ord {
76 ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
77 if $a != $b { lexical_ord!($rel, $a, $b) }
78 else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
80 ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
83 macro_rules! lexical_partial_cmp {
84 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
85 match ($a).partial_cmp(&$b) {
86 Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
90 ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
93 macro_rules! lexical_cmp {
94 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
96 Equal => lexical_cmp!($($rest_a, $rest_b),+),
100 ($a:expr, $b:expr) => { ($a).cmp(&$b) };
103 macro_rules! last_type {
104 ($a:ident,) => { $a };
105 ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
118 (A B C D E F G H I J)
119 (A B C D E F G H I J K)
120 (A B C D E F G H I J K L)