nearly/trait_impl/
tuple.rs

1use crate::nearly_eq::{NearlyEq, NearlyEqEps, NearlyEqTol, NearlyEqUlps};
2use crate::nearly_ord::{NearlyOrd, NearlyOrdEps, NearlyOrdTol, NearlyOrdUlps};
3use crate::tolerance::{
4    EpsTolerance, EpsToleranceType, Tolerance, UlpsTolerance, UlpsToleranceType,
5};
6
7macro_rules! impl_tuple {
8    ($lhs:ident, $rhs:ident, $idx: tt) => {
9        impl_tuple!(@impl $lhs, $rhs, $idx);
10    };
11    ($lhs:ident $( $lhs_tail:ident )+, $rhs:ident $( $rhs_tail:ident )+, $idx:tt $( $idx_tail:tt )+) => {
12        impl_tuple!($( $lhs_tail )+, $( $rhs_tail )+, $( $idx_tail )+);
13        impl_tuple!(@impl $lhs $( $lhs_tail )+, $rhs $( $rhs_tail )+, $idx $( $idx_tail )+);
14    };
15    (@impl $( $lhs: ident )+, $( $rhs: ident )+, $( $idx: tt )+) => {
16        ///////////////
17        // nearly_eq //
18        ///////////////
19
20        impl<Lhs, Rhs> NearlyEqEps<($($rhs,)+), Lhs, Rhs> for ($($lhs,)+)
21        where
22            Lhs: NearlyEqEps<Rhs> + EpsTolerance<Rhs>,
23        {
24            fn nearly_eq_eps(&self, other: &($($rhs,)+), eps: &EpsToleranceType<Lhs, Rhs>) -> bool {
25                $( self.$idx.nearly_eq_eps(&other.$idx, eps) )&&+
26            }
27        }
28
29        impl<Lhs, Rhs> NearlyEqUlps<($($rhs,)+), Lhs, Rhs> for ($($lhs,)+)
30        where
31            Lhs: NearlyEqUlps<Rhs> + UlpsTolerance<Rhs>,
32        {
33            fn nearly_eq_ulps(&self, other: &($($rhs,)+), ulps: &UlpsToleranceType<Lhs, Rhs>) -> bool {
34                $( self.$idx.nearly_eq_ulps(&other.$idx, ulps) )&&+
35            }
36        }
37
38        impl<Lhs, Rhs> NearlyEqTol<($($rhs,)+), Lhs, Rhs> for ($($lhs,)+)
39        where
40            Lhs: NearlyEqTol<Rhs> + EpsTolerance<Rhs> + UlpsTolerance<Rhs>,
41        {
42            fn nearly_eq_tol(&self, other: &($($rhs,)+), tol: &Tolerance<Lhs, Rhs>) -> bool {
43                $( self.$idx.nearly_eq_tol(&other.$idx, tol) )&&+
44            }
45        }
46
47        impl<Lhs, Rhs> NearlyEq<($($rhs,)+), Lhs, Rhs> for ($($lhs,)+)
48        where
49            Lhs: NearlyEq<Rhs> + EpsTolerance<Rhs> + UlpsTolerance<Rhs>,
50        {
51        }
52
53        ////////////////
54        // nearly_ord //
55        ////////////////
56
57        impl<Lhs, Rhs> NearlyOrdEps<($($rhs,)+), Lhs, Rhs> for ($($lhs,)+)
58        where
59            Lhs: NearlyOrdEps<Rhs> + EpsTolerance<Rhs>,
60        {
61            fn nearly_lt_eps(&self, other: &($($rhs,)+), eps: &EpsToleranceType<Lhs, Rhs>) -> bool {
62                $( self.$idx.nearly_lt_eps(&other.$idx, eps) )&&+
63            }
64
65            fn nearly_le_eps(&self, other: &($($rhs,)+), eps: &EpsToleranceType<Lhs, Rhs>) -> bool {
66                $( self.$idx.nearly_le_eps(&other.$idx, eps) )&&+
67            }
68
69            fn nearly_gt_eps(&self, other: &($($rhs,)+), eps: &EpsToleranceType<Lhs, Rhs>) -> bool {
70                $( self.$idx.nearly_gt_eps(&other.$idx, eps) )&&+
71            }
72
73            fn nearly_ge_eps(&self, other: &($($rhs,)+), eps: &EpsToleranceType<Lhs, Rhs>) -> bool {
74                $( self.$idx.nearly_ge_eps(&other.$idx, eps) )&&+
75            }
76        }
77
78        impl<Lhs, Rhs> NearlyOrdUlps<($($rhs,)+), Lhs, Rhs> for ($($lhs,)+)
79        where
80            Lhs: NearlyOrdUlps<Rhs> + UlpsTolerance<Rhs>,
81        {
82            fn nearly_lt_ulps(&self, other: &($($rhs,)+), ulps: &UlpsToleranceType<Lhs, Rhs>) -> bool {
83                $( self.$idx.nearly_lt_ulps(&other.$idx, ulps) )&&+
84            }
85
86            fn nearly_le_ulps(&self, other: &($($rhs,)+), ulps: &UlpsToleranceType<Lhs, Rhs>) -> bool {
87                $( self.$idx.nearly_le_ulps(&other.$idx, ulps) )&&+
88            }
89
90            fn nearly_gt_ulps(&self, other: &($($rhs,)+), ulps: &UlpsToleranceType<Lhs, Rhs>) -> bool {
91                $( self.$idx.nearly_gt_ulps(&other.$idx, ulps) )&&+
92            }
93
94            fn nearly_ge_ulps(&self, other: &($($rhs,)+), ulps: &UlpsToleranceType<Lhs, Rhs>) -> bool {
95                $( self.$idx.nearly_ge_ulps(&other.$idx, ulps) )&&+
96            }
97        }
98
99        impl<Lhs, Rhs> NearlyOrdTol<($($rhs,)+), Lhs, Rhs> for ($($lhs,)+)
100        where
101            Lhs: NearlyOrdTol<Rhs> + EpsTolerance<Rhs> + UlpsTolerance<Rhs>,
102        {
103            fn nearly_lt_tol(&self, other: &($($rhs,)+), tol: &Tolerance<Lhs, Rhs>) -> bool {
104                $( self.$idx.nearly_lt_tol(&other.$idx, tol) )&&+
105            }
106
107            fn nearly_le_tol(&self, other: &($($rhs,)+), tol: &Tolerance<Lhs, Rhs>) -> bool {
108                $( self.$idx.nearly_le_tol(&other.$idx, tol) )&&+
109            }
110
111            fn nearly_gt_tol(&self, other: &($($rhs,)+), tol: &Tolerance<Lhs, Rhs>) -> bool {
112                $( self.$idx.nearly_gt_tol(&other.$idx, tol) )&&+
113            }
114
115            fn nearly_ge_tol(&self, other: &($($rhs,)+), tol: &Tolerance<Lhs, Rhs>) -> bool {
116                $( self.$idx.nearly_ge_tol(&other.$idx, tol) )&&+
117            }
118        }
119
120        impl<Lhs, Rhs> NearlyOrd<($($rhs,)+), Lhs, Rhs> for ($($lhs,)+)
121        where
122            Lhs: NearlyOrd<Rhs> + EpsTolerance<Rhs> + UlpsTolerance<Rhs>,
123        {
124        }
125    }
126}
127
128impl_tuple!(
129    Lhs Lhs Lhs Lhs Lhs Lhs Lhs Lhs Lhs Lhs Lhs Lhs,
130    Rhs Rhs Rhs Rhs Rhs Rhs Rhs Rhs Rhs Rhs Rhs Rhs,
131    11 10 9 8 7 6 5 4 3 2 1 0);