float_eq/trait_impls/
tuples.rs

1use crate::{AssertFloatEq, DebugUlpsDiff, FloatEq, FloatEqDebugUlpsDiff, FloatEqUlpsTol, UlpsTol};
2use core::fmt;
3
4impl FloatEqUlpsTol for () {
5    type UlpsTol = ();
6}
7
8impl FloatEqDebugUlpsDiff for () {
9    type DebugUlpsDiff = ();
10}
11
12impl FloatEq for () {
13    type Tol = ();
14
15    #[inline]
16    fn eq_abs(&self, _other: &(), _tol: &Self::Tol) -> bool {
17        true
18    }
19
20    #[inline]
21    fn eq_rmax(&self, _other: &(), _tol: &Self::Tol) -> bool {
22        true
23    }
24
25    #[inline]
26    fn eq_rmin(&self, _other: &(), _tol: &Self::Tol) -> bool {
27        true
28    }
29
30    #[inline]
31    fn eq_r1st(&self, _other: &(), _tol: &Self::Tol) -> bool {
32        true
33    }
34
35    #[inline]
36    fn eq_r2nd(&self, _other: &(), _tol: &Self::Tol) -> bool {
37        true
38    }
39
40    #[inline]
41    fn eq_ulps(&self, _other: &(), _tol: &UlpsTol<Self::Tol>) -> bool {
42        true
43    }
44}
45
46impl AssertFloatEq for () {
47    type DebugAbsDiff = ();
48    type DebugTol = ();
49
50    #[inline]
51    fn debug_abs_diff(&self, _other: &()) -> Self::DebugAbsDiff {}
52
53    #[inline]
54    fn debug_ulps_diff(&self, _other: &()) -> DebugUlpsDiff<Self::DebugAbsDiff> {}
55
56    #[inline]
57    fn debug_abs_tol(&self, _other: &(), _tol: &Self::Tol) -> Self::DebugTol {}
58
59    #[inline]
60    fn debug_rmax_tol(&self, _other: &(), _tol: &Self::Tol) -> Self::DebugTol {}
61
62    #[inline]
63    fn debug_rmin_tol(&self, _other: &(), _tol: &Self::Tol) -> Self::DebugTol {}
64
65    #[inline]
66    fn debug_r1st_tol(&self, _other: &(), _tol: &Self::Tol) -> Self::DebugTol {}
67
68    #[inline]
69    fn debug_r2nd_tol(&self, _other: &(), _tol: &Self::Tol) -> Self::DebugTol {}
70
71    #[inline]
72    fn debug_ulps_tol(&self, _other: &(), _tol: &UlpsTol<Self::Tol>) -> UlpsTol<Self::DebugTol> {}
73}
74
75// Non-unit type tuple impls, as for std PartialEq implementation
76macro_rules! tuple_impls {
77    ($(
78        $Tuple:ident {
79            $(($idx:tt) -> $T:ident)+
80        }
81    )+) => {
82        $(
83            impl<$($T:FloatEqUlpsTol),+> FloatEqUlpsTol for ($($T,)+)
84            where
85                last_type!($($T,)+): ?Sized,
86                $(UlpsTol<$T>: Sized,)+
87            {
88                type UlpsTol = ($(UlpsTol<$T>,)+);
89            }
90
91            impl<$($T:FloatEqDebugUlpsDiff),+> FloatEqDebugUlpsDiff for ($($T,)+)
92            {
93                type DebugUlpsDiff = ($(DebugUlpsDiff<$T>,)+);
94            }
95
96            impl<$($T:FloatEq),+> FloatEq for ($($T,)+)
97            where
98                last_type!($($T,)+): ?Sized,
99                $($T::Tol: Sized,)+
100                $(UlpsTol<$T::Tol>: Sized,)+
101            {
102                type Tol = ($($T::Tol,)+);
103
104                #[inline]
105                fn eq_abs(&self, other: &Self, tol: &Self::Tol) -> bool {
106                    $(self.$idx.eq_abs(&other.$idx, &tol.$idx))&&+
107                }
108
109                #[inline]
110                fn eq_rmax(&self, other: &Self, tol: &Self::Tol) -> bool {
111                    $(self.$idx.eq_rmax(&other.$idx, &tol.$idx))&&+
112                }
113
114                #[inline]
115                fn eq_rmin(&self, other: &Self, tol: &Self::Tol) -> bool {
116                    $(self.$idx.eq_rmin(&other.$idx, &tol.$idx))&&+
117                }
118
119                #[inline]
120                fn eq_r1st(&self, other: &Self, tol: &Self::Tol) -> bool {
121                    $(self.$idx.eq_r1st(&other.$idx, &tol.$idx))&&+
122                }
123
124                #[inline]
125                fn eq_r2nd(&self, other: &Self, tol: &Self::Tol) -> bool {
126                    $(self.$idx.eq_r2nd(&other.$idx, &tol.$idx))&&+
127                }
128
129                #[inline]
130                fn eq_ulps(&self, other: &Self, tol: &UlpsTol<Self::Tol>) -> bool {
131                    $(self.$idx.eq_ulps(&other.$idx, &tol.$idx))&&+
132                }
133            }
134
135            impl<$($T:AssertFloatEq + fmt::Debug),+> AssertFloatEq for ($($T,)+)
136            where
137                last_type!($($T,)+): ?Sized,
138                $($T::Tol: Sized,)+
139                $($T::DebugTol: Sized,)+
140                $(UlpsTol<$T::Tol>: Sized,)+
141                $(UlpsTol<$T::DebugTol>: Sized,)+
142            {
143                type DebugAbsDiff = ($($T::DebugAbsDiff,)+);
144                type DebugTol = ($($T::DebugTol,)+);
145
146                #[inline]
147                fn debug_abs_diff(&self, other: &Self) -> Self::DebugAbsDiff {
148                    ($(self.$idx.debug_abs_diff(&other.$idx),)+)
149                }
150
151                #[inline]
152                fn debug_ulps_diff(&self, other: &Self) -> DebugUlpsDiff<Self::DebugAbsDiff> {
153                    ($(self.$idx.debug_ulps_diff(&other.$idx),)+)
154                }
155
156                #[inline]
157                fn debug_abs_tol(&self, other: &Self, tol: &Self::Tol) -> Self::DebugTol {
158                    ($(self.$idx.debug_abs_tol(&other.$idx, &tol.$idx),)+)
159                }
160
161                #[inline]
162                fn debug_rmax_tol(&self, other: &Self, tol: &Self::Tol) -> Self::DebugTol {
163                    ($(self.$idx.debug_rmax_tol(&other.$idx, &tol.$idx),)+)
164                }
165
166                #[inline]
167                fn debug_rmin_tol(&self, other: &Self, tol: &Self::Tol) -> Self::DebugTol {
168                    ($(self.$idx.debug_rmin_tol(&other.$idx, &tol.$idx),)+)
169                }
170
171                #[inline]
172                fn debug_r1st_tol(&self, other: &Self, tol: &Self::Tol) -> Self::DebugTol {
173                    ($(self.$idx.debug_r1st_tol(&other.$idx, &tol.$idx),)+)
174                }
175
176                #[inline]
177                fn debug_r2nd_tol(&self, other: &Self, tol: &Self::Tol) -> Self::DebugTol {
178                    ($(self.$idx.debug_r2nd_tol(&other.$idx, &tol.$idx),)+)
179                }
180
181                #[inline]
182                fn debug_ulps_tol(&self, other: &Self, tol: &UlpsTol<Self::Tol>) -> UlpsTol<Self::DebugTol> {
183                    ($(self.$idx.debug_ulps_tol(&other.$idx, &tol.$idx),)+)
184                }
185            }
186        )+
187    };
188}
189
190macro_rules! last_type {
191    ($a:ident,) => { $a };
192    ($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
193}
194
195tuple_impls! {
196    Tuple1 {
197        (0) -> A
198    }
199    Tuple2 {
200        (0) -> A
201        (1) -> B
202    }
203    Tuple3 {
204        (0) -> A
205        (1) -> B
206        (2) -> C
207    }
208    Tuple4 {
209        (0) -> A
210        (1) -> B
211        (2) -> C
212        (3) -> D
213    }
214    Tuple5 {
215        (0) -> A
216        (1) -> B
217        (2) -> C
218        (3) -> D
219        (4) -> E
220    }
221    Tuple6 {
222        (0) -> A
223        (1) -> B
224        (2) -> C
225        (3) -> D
226        (4) -> E
227        (5) -> F
228    }
229    Tuple7 {
230        (0) -> A
231        (1) -> B
232        (2) -> C
233        (3) -> D
234        (4) -> E
235        (5) -> F
236        (6) -> G
237    }
238    Tuple8 {
239        (0) -> A
240        (1) -> B
241        (2) -> C
242        (3) -> D
243        (4) -> E
244        (5) -> F
245        (6) -> G
246        (7) -> H
247    }
248    Tuple9 {
249        (0) -> A
250        (1) -> B
251        (2) -> C
252        (3) -> D
253        (4) -> E
254        (5) -> F
255        (6) -> G
256        (7) -> H
257        (8) -> I
258    }
259    Tuple10 {
260        (0) -> A
261        (1) -> B
262        (2) -> C
263        (3) -> D
264        (4) -> E
265        (5) -> F
266        (6) -> G
267        (7) -> H
268        (8) -> I
269        (9) -> J
270    }
271    Tuple11 {
272        (0) -> A
273        (1) -> B
274        (2) -> C
275        (3) -> D
276        (4) -> E
277        (5) -> F
278        (6) -> G
279        (7) -> H
280        (8) -> I
281        (9) -> J
282        (10) -> K
283    }
284    Tuple12 {
285        (0) -> A
286        (1) -> B
287        (2) -> C
288        (3) -> D
289        (4) -> E
290        (5) -> F
291        (6) -> G
292        (7) -> H
293        (8) -> I
294        (9) -> J
295        (10) -> K
296        (11) -> L
297    }
298}