1 // See src/libstd/primitive_docs.rs for documentation.
3 use crate::cmp::Ordering::*;
6 // Recursive macro for implementing n-ary tuple functions and operations
8 // Also provides implementations for tuples with lesser arity. For example, tuple_impls!(A B C)
9 // will implement everything for (A, B, C), (A, B) and (A,).
10 macro_rules! tuple_impls {
11 // Stopping criteria (1-ary tuple)
13 tuple_impls!(@impl $T);
15 // Running criteria (n-ary tuple, with n >= 2)
16 ($T:ident $( $U:ident )+) => {
17 tuple_impls!($( $U )+);
18 tuple_impls!(@impl $T $( $U )+);
20 // "Private" internal implementation
21 (@impl $( $T:ident )+) => {
24 #[stable(feature = "rust1", since = "1.0.0")]
25 #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
26 impl<$($T: ~const PartialEq),+> const PartialEq for ($($T,)+)
28 last_type!($($T,)+): ?Sized
31 fn eq(&self, other: &($($T,)+)) -> bool {
32 $( ${ignore(T)} self.${index()} == other.${index()} )&&+
35 fn ne(&self, other: &($($T,)+)) -> bool {
36 $( ${ignore(T)} self.${index()} != other.${index()} )||+
43 #[stable(feature = "rust1", since = "1.0.0")]
44 impl<$($T: Eq),+> Eq for ($($T,)+)
46 last_type!($($T,)+): ?Sized
52 #[stable(feature = "rust1", since = "1.0.0")]
53 #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
54 impl<$($T: ~const PartialOrd + ~const PartialEq),+> const PartialOrd for ($($T,)+)
56 last_type!($($T,)+): ?Sized
59 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
60 lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
63 fn lt(&self, other: &($($T,)+)) -> bool {
64 lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
67 fn le(&self, other: &($($T,)+)) -> bool {
68 lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
71 fn ge(&self, other: &($($T,)+)) -> bool {
72 lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
75 fn gt(&self, other: &($($T,)+)) -> bool {
76 lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
83 #[stable(feature = "rust1", since = "1.0.0")]
84 #[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
85 impl<$($T: ~const Ord),+> const Ord for ($($T,)+)
87 last_type!($($T,)+): ?Sized
90 fn cmp(&self, other: &($($T,)+)) -> Ordering {
91 lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
98 #[stable(feature = "rust1", since = "1.0.0")]
99 #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
100 impl<$($T: ~const Default),+> const Default for ($($T,)+) {
102 fn default() -> ($($T,)+) {
103 ($({ let x: $T = Default::default(); x},)+)
110 // If this is a unary tuple, it adds a doc comment.
111 // Otherwise, it hides the docs entirely.
112 macro_rules! maybe_tuple_doc {
113 ($a:ident @ #[$meta:meta] $item:item) => {
114 #[doc(fake_variadic)]
115 #[doc = "This trait is implemented for tuples up to twelve items long."]
119 ($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
126 // Constructs an expression that performs a lexical ordering using method $rel.
127 // The values are interleaved, so the macro invocation for
128 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
129 // a3, b3)` (and similarly for `lexical_cmp`)
130 macro_rules! lexical_ord {
131 ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
132 if $a != $b { lexical_ord!($rel, $a, $b) }
133 else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
135 ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
138 macro_rules! lexical_partial_cmp {
139 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
140 match ($a).partial_cmp(&$b) {
141 Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
145 ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
148 macro_rules! lexical_cmp {
149 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
150 match ($a).cmp(&$b) {
151 Equal => lexical_cmp!($($rest_a, $rest_b),+),
155 ($a:expr, $b:expr) => { ($a).cmp(&$b) };
158 macro_rules! last_type {
159 ($a:ident,) => { $a };
160 ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
163 tuple_impls!(E D C B A Z Y X W V U T);