]> git.lizzy.rs Git - rust.git/blob - library/core/src/tuple.rs
Auto merge of #95621 - saethlin:remove-mpsc-transmute, r=RalfJung
[rust.git] / library / core / src / tuple.rs
1 // See src/libstd/primitive_docs.rs for documentation.
2
3 use crate::cmp::Ordering::*;
4 use crate::cmp::*;
5
6 // macro for implementing n-ary tuple functions and operations
7 macro_rules! tuple_impls {
8     ( $( ( $( $T:ident )+ ) )+ ) => {
9         $(
10             #[stable(feature = "rust1", since = "1.0.0")]
11             impl<$($T:PartialEq),+> PartialEq for ($($T,)+) where last_type!($($T,)+): ?Sized {
12                 #[inline]
13                 fn eq(&self, other: &($($T,)+)) -> bool {
14                     $( ${ignore(T)} self.${index()} == other.${index()} )&&+
15                 }
16                 #[inline]
17                 fn ne(&self, other: &($($T,)+)) -> bool {
18                     $( ${ignore(T)} self.${index()} != other.${index()} )||+
19                 }
20             }
21
22             #[stable(feature = "rust1", since = "1.0.0")]
23             impl<$($T:Eq),+> Eq for ($($T,)+) where last_type!($($T,)+): ?Sized {}
24
25             #[stable(feature = "rust1", since = "1.0.0")]
26             impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+)
27             where
28                 last_type!($($T,)+): ?Sized
29             {
30                 #[inline]
31                 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
32                     lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
33                 }
34                 #[inline]
35                 fn lt(&self, other: &($($T,)+)) -> bool {
36                     lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
37                 }
38                 #[inline]
39                 fn le(&self, other: &($($T,)+)) -> bool {
40                     lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
41                 }
42                 #[inline]
43                 fn ge(&self, other: &($($T,)+)) -> bool {
44                     lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
45                 }
46                 #[inline]
47                 fn gt(&self, other: &($($T,)+)) -> bool {
48                     lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
49                 }
50             }
51
52             #[stable(feature = "rust1", since = "1.0.0")]
53             impl<$($T:Ord),+> Ord for ($($T,)+) where last_type!($($T,)+): ?Sized {
54                 #[inline]
55                 fn cmp(&self, other: &($($T,)+)) -> Ordering {
56                     lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
57                 }
58             }
59
60             #[stable(feature = "rust1", since = "1.0.0")]
61             impl<$($T:Default),+> Default for ($($T,)+) {
62                 #[inline]
63                 fn default() -> ($($T,)+) {
64                     ($({ let x: $T = Default::default(); x},)+)
65                 }
66             }
67         )+
68     }
69 }
70
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),+) }
79     };
80     ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
81 }
82
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),+),
87             ordering   => ordering
88         }
89     };
90     ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
91 }
92
93 macro_rules! lexical_cmp {
94     ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
95         match ($a).cmp(&$b) {
96             Equal => lexical_cmp!($($rest_a, $rest_b),+),
97             ordering   => ordering
98         }
99     };
100     ($a:expr, $b:expr) => { ($a).cmp(&$b) };
101 }
102
103 macro_rules! last_type {
104     ($a:ident,) => { $a };
105     ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
106 }
107
108 tuple_impls! {
109     (A)
110     (A B)
111     (A B C)
112     (A B C D)
113     (A B C D E)
114     (A B C D E F)
115     (A B C D E F G)
116     (A B C D E F G H)
117     (A B C D E F G H I)
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)
121 }