1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// 有关文档,请参见 src/libstd/primitive_docs.rs。

use crate::cmp::Ordering::{self, *};

// 用于实现 n 元组函数和操作的递归宏
//
// 还提供了具有较少元组的实现。
// 例如,tuple_impls!(ABC) 将实现 (A, B, C)、(A, B) 和 (A,) 的所有内容。
macro_rules! tuple_impls {
    // 停止条件 (1 元组)
    ($T:ident) => {
        tuple_impls!(@impl $T);
    };
    // 运行条件 (n 元元组,n >= 2)
    ($T:ident $( $U:ident )+) => {
        tuple_impls!($( $U )+);
        tuple_impls!(@impl $T $( $U )+);
    };
    // "Private" 内部实现
    (@impl $( $T:ident )+) => {
        maybe_tuple_doc! {
            $($T)+ @
            #[stable(feature = "rust1", since = "1.0.0")]
            impl<$($T: PartialEq),+> PartialEq for ($($T,)+)
            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()} )||+
                }
            }
        }

        maybe_tuple_doc! {
            $($T)+ @
            #[stable(feature = "rust1", since = "1.0.0")]
            impl<$($T: Eq),+> Eq for ($($T,)+)
            where
                last_type!($($T,)+): ?Sized
            {}
        }

        maybe_tuple_doc! {
            $($T)+ @
            #[stable(feature = "rust1", since = "1.0.0")]
            impl<$($T: PartialOrd),+> PartialOrd for ($($T,)+)
            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, Less, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
                }
                #[inline]
                fn le(&self, other: &($($T,)+)) -> bool {
                    lexical_ord!(le, Less, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
                }
                #[inline]
                fn ge(&self, other: &($($T,)+)) -> bool {
                    lexical_ord!(ge, Greater, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
                }
                #[inline]
                fn gt(&self, other: &($($T,)+)) -> bool {
                    lexical_ord!(gt, Greater, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
                }
            }
        }

        maybe_tuple_doc! {
            $($T)+ @
            #[stable(feature = "rust1", since = "1.0.0")]
            impl<$($T: Ord),+> Ord for ($($T,)+)
            where
                last_type!($($T,)+): ?Sized
            {
                #[inline]
                fn cmp(&self, other: &($($T,)+)) -> Ordering {
                    lexical_cmp!($( ${ignore(T)} self.${index()}, other.${index()} ),+)
                }
            }
        }

        maybe_tuple_doc! {
            $($T)+ @
            #[stable(feature = "rust1", since = "1.0.0")]
            impl<$($T: Default),+> Default for ($($T,)+) {
                #[inline]
                fn default() -> ($($T,)+) {
                    ($({ let x: $T = Default::default(); x},)+)
                }
            }
        }

        #[stable(feature = "array_tuple_conv", since = "1.71.0")]
        impl<T> From<[T; ${count(T)}]> for ($(${ignore(T)} T,)+) {
            #[inline]
            #[allow(non_snake_case)]
            fn from(array: [T; ${count(T)}]) -> Self {
                let [$($T,)+] = array;
                ($($T,)+)
            }
        }

        #[stable(feature = "array_tuple_conv", since = "1.71.0")]
        impl<T> From<($(${ignore(T)} T,)+)> for [T; ${count(T)}] {
            #[inline]
            #[allow(non_snake_case)]
            fn from(tuple: ($(${ignore(T)} T,)+)) -> Self {
                let ($($T,)+) = tuple;
                [$($T,)+]
            }
        }
    }
}

// 如果这是一个一元元组,它会添加一个文档注释。
// 否则,它会完全隐藏文档。
macro_rules! maybe_tuple_doc {
    ($a:ident @ #[$meta:meta] $item:item) => {
        #[doc(fake_variadic)]
        #[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
    };
}

#[inline]
const fn ordering_is_some(c: Option<Ordering>, x: Ordering) -> bool {
    // FIXME: 一旦 `==` 在 `Option` 上是常量稳定的,只需使用它。
    // 这是将 `None` 映射到 2,然后再进行比较,因为它优化得更好 (`None::<Ordering>` 表示为 2)。
    //
    x as i8
        == match c {
            Some(c) => c as i8,
            None => 2,
        }
}

// 使用方法 `$rel` 创建一个执行词法排序的表达式。
// 这些值是交错的,因此 `(a1, a2, a3) < (b1, b2, b3)` 的宏调用将是 `lexical_ord!(lt, opt_is_lt, a1, b1, a2, b2, a3, b3)` (对于 `lexical_cmp` 也类似)
//
//
// `$ne_rel` 只是用来判断不相等的结果,所以 `lt` 和 `le` 都可以只用 `Less`。
//
//
macro_rules! lexical_ord {
    ($rel: ident, $ne_rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {{
        let c = PartialOrd::partial_cmp(&$a, &$b);
        if !ordering_is_some(c, Equal) { ordering_is_some(c, $ne_rel) }
        else { lexical_ord!($rel, $ne_rel, $($rest_a, $rest_b),+) }
    }};
    ($rel: ident, $ne_rel: ident, $a:expr, $b:expr) => {
        // 对最后一个元素使用特定方法
        PartialOrd::$rel(&$a, &$b)
    };
}

macro_rules! lexical_partial_cmp {
    ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
        match ($a).partial_cmp(&$b) {
            Some(Equal) => lexical_partial_cmp!($($rest_a, $rest_b),+),
            ordering   => ordering
        }
    };
    ($a:expr, $b:expr) => { ($a).partial_cmp(&$b) };
}

macro_rules! lexical_cmp {
    ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
        match ($a).cmp(&$b) {
            Equal => lexical_cmp!($($rest_a, $rest_b),+),
            ordering   => ordering
        }
    };
    ($a:expr, $b:expr) => { ($a).cmp(&$b) };
}

macro_rules! last_type {
    ($a:ident,) => { $a };
    ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
}

tuple_impls!(E D C B A Z Y X W V U T);