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 impl<$($T:PartialEq),+> PartialEq for ($($T,)+)
27 last_type!($($T,)+): ?Sized
30 fn eq(&self, other: &($($T,)+)) -> bool {
31 $( ${ignore(T)} self.${index()} == other.${index()} )&&+
34 fn ne(&self, other: &($($T,)+)) -> bool {
35 $( ${ignore(T)} self.${index()} != other.${index()} )||+
42 #[stable(feature = "rust1", since = "1.0.0")]
43 impl<$($T:Eq),+> Eq for ($($T,)+)
45 last_type!($($T,)+): ?Sized
51 #[stable(feature = "rust1", since = "1.0.0")]
52 impl<$($T:PartialOrd + PartialEq),+> PartialOrd for ($($T,)+)
54 last_type!($($T,)+): ?Sized
57 fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
58 lexical_partial_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
61 fn lt(&self, other: &($($T,)+)) -> bool {
62 lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
65 fn le(&self, other: &($($T,)+)) -> bool {
66 lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
69 fn ge(&self, other: &($($T,)+)) -> bool {
70 lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
73 fn gt(&self, other: &($($T,)+)) -> bool {
74 lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
81 #[stable(feature = "rust1", since = "1.0.0")]
82 impl<$($T:Ord),+> Ord for ($($T,)+)
84 last_type!($($T,)+): ?Sized
87 fn cmp(&self, other: &($($T,)+)) -> Ordering {
88 lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
95 #[stable(feature = "rust1", since = "1.0.0")]
96 #[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
97 impl<$($T: ~const Default),+> const Default for ($($T,)+) {
99 fn default() -> ($($T,)+) {
100 ($({ let x: $T = Default::default(); x},)+)
107 // If this is a unary tuple, it adds a doc comment.
108 // Otherwise, it hides the docs entirely.
109 macro_rules! maybe_tuple_doc {
110 ($a:ident @ #[$meta:meta] $item:item) => {
111 #[doc(fake_variadic)]
112 #[doc = "This trait is implemented for tuples up to twelve items long."]
116 ($a:ident $($rest_a:ident)+ @ #[$meta:meta] $item:item) => {
123 // Constructs an expression that performs a lexical ordering using method $rel.
124 // The values are interleaved, so the macro invocation for
125 // `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
126 // a3, b3)` (and similarly for `lexical_cmp`)
127 macro_rules! lexical_ord {
128 ($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
129 if $a != $b { lexical_ord!($rel, $a, $b) }
130 else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
132 ($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
135 macro_rules! lexical_partial_cmp {
136 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
137 match ($a).partial_cmp(&$b) {
138 Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
142 ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
145 macro_rules! lexical_cmp {
146 ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
147 match ($a).cmp(&$b) {
148 Equal => lexical_cmp!($($rest_a, $rest_b),+),
152 ($a:expr, $b:expr) => { ($a).cmp(&$b) };
155 macro_rules! last_type {
156 ($a:ident,) => { $a };
157 ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
160 tuple_impls!(E D C B A Z Y X W V U T);