palette/macros/
equality.rs

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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
macro_rules! impl_eq {
    (  $self_ty: ident , [$element: tt]) => {
        impl_eq!($self_ty<>, [$element]);
    };
    (  $self_ty: ident < $($ty_param: ident),* > , [$element: tt]) => {
        impl<$($ty_param,)* T> PartialEq for $self_ty<$($ty_param,)* T>
        where
            T: PartialEq,
        {
            fn eq(&self, other: &Self) -> bool {
                self.$element == other.$element
            }
        }

        impl<$($ty_param,)* T> Eq for $self_ty<$($ty_param,)* T> where T: Eq {}

        #[cfg(feature = "approx")]
        impl<$($ty_param,)* T> approx::AbsDiffEq for $self_ty<$($ty_param,)* T>
        where
            T: approx::AbsDiffEq,
        {
            type Epsilon = T::Epsilon;

            fn default_epsilon() -> Self::Epsilon {
                T::default_epsilon()
            }

            fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool {
                self.$element.abs_diff_eq(&other.$element, epsilon)
            }
            fn abs_diff_ne(&self, other: &Self, epsilon: T::Epsilon) -> bool {
                self.$element.abs_diff_ne(&other.$element, epsilon)
            }
        }

        #[cfg(feature = "approx")]
        impl<$($ty_param,)* T> approx::RelativeEq for $self_ty<$($ty_param,)* T>
        where
            T: approx::RelativeEq,
        {
            fn default_max_relative() -> T::Epsilon {
                T::default_max_relative()
            }

            fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
                self.$element.relative_eq(&other.$element, epsilon, max_relative)
            }
            fn relative_ne(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
                self.$element.relative_ne(&other.$element, epsilon, max_relative)
            }
        }

        #[cfg(feature = "approx")]
        impl<$($ty_param,)* T> approx::UlpsEq for $self_ty<$($ty_param,)* T>
        where
            T: approx::UlpsEq,
        {
            fn default_max_ulps() -> u32 {
                T::default_max_ulps()
            }

            fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
                self.$element.ulps_eq(&other.$element, epsilon, max_ulps)
            }
            fn ulps_ne(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
                self.$element.ulps_ne(&other.$element, epsilon, max_ulps)
            }
        }
    };
    (  $self_ty: ident , [$($element: ident),+]) => {
        impl_eq!($self_ty<>, [$($element),+]);
    };
    (  $self_ty: ident < $($ty_param: ident),* > , [$($element: ident),+]) => {
        impl<$($ty_param,)* T> PartialEq for $self_ty<$($ty_param,)* T>
        where
            T: PartialEq,
        {
            fn eq(&self, other: &Self) -> bool {
                $( self.$element == other.$element )&&+
            }
        }

        impl<$($ty_param,)* T> Eq for $self_ty<$($ty_param,)* T> where T: Eq {}

        #[cfg(feature = "approx")]
        impl<$($ty_param,)* T> approx::AbsDiffEq for $self_ty<$($ty_param,)* T>
        where
            T: approx::AbsDiffEq,
            T::Epsilon: Clone,
        {
            type Epsilon = T::Epsilon;

            fn default_epsilon() -> Self::Epsilon {
                T::default_epsilon()
            }

            fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool {
                $( self.$element.abs_diff_eq(&other.$element, epsilon.clone()) )&&+
            }
            fn abs_diff_ne(&self, other: &Self, epsilon: T::Epsilon) -> bool {
                $( self.$element.abs_diff_ne(&other.$element, epsilon.clone()) )||+
            }
        }

        #[cfg(feature = "approx")]
        impl<$($ty_param,)* T> approx::RelativeEq for $self_ty<$($ty_param,)* T>
        where
            T: approx::RelativeEq,
            T::Epsilon: Clone,
        {
            fn default_max_relative() -> T::Epsilon {
                T::default_max_relative()
            }

            fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
                $( self.$element.relative_eq(&other.$element, epsilon.clone(), max_relative.clone()) )&&+
            }
            fn relative_ne(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
                $( self.$element.relative_ne(&other.$element, epsilon.clone(), max_relative.clone()) )||+
            }
        }

        #[cfg(feature = "approx")]
        impl<$($ty_param,)* T> approx::UlpsEq for $self_ty<$($ty_param,)* T>
        where
            T: approx::UlpsEq,
            T::Epsilon: Clone,
        {
            fn default_max_ulps() -> u32 {
                T::default_max_ulps()
            }

            fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
                $( self.$element.ulps_eq(&other.$element, epsilon.clone(), max_ulps) )&&+
            }
            fn ulps_ne(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
                $( self.$element.ulps_ne(&other.$element, epsilon.clone(), max_ulps) )||+
            }
        }
    }
}

macro_rules! impl_eq_hue {
    (  $self_ty: ident, $hue_ty: ident, [$($element: ident),+]) => {
        impl_eq_hue!($self_ty<>, $hue_ty, [$($element),+]);
    };
    (  $self_ty: ident < $($ty_param: ident),* >, $hue_ty: ident, [$($element: ident),+]) => {

        impl<$($ty_param,)* T> PartialEq for $self_ty<$($ty_param,)* T>
        where
            T: PartialEq,
            $hue_ty<T>: PartialEq,
        {
            fn eq(&self, other: &Self) -> bool {
                $( self.$element == other.$element )&&+
            }
        }

        impl<$($ty_param,)* T> Eq for $self_ty<$($ty_param,)* T>
        where
            T: Eq,
            $hue_ty<T>: Eq,
        {}

        #[cfg(feature = "approx")]
        impl<$($ty_param,)* T> approx::AbsDiffEq for $self_ty<$($ty_param,)* T>
        where
            T: approx::AbsDiffEq,
            T::Epsilon: Clone,
            $hue_ty<T>: approx::AbsDiffEq<Epsilon = T::Epsilon>,
        {
            type Epsilon = T::Epsilon;

            fn default_epsilon() -> Self::Epsilon {
                T::default_epsilon()
            }

            fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool {
                $( self.$element.abs_diff_eq(&other.$element, epsilon.clone()) )&&+
            }
            fn abs_diff_ne(&self, other: &Self, epsilon: T::Epsilon) -> bool {
                $( self.$element.abs_diff_ne(&other.$element, epsilon.clone()) )||+
            }
        }

        #[cfg(feature = "approx")]
        impl<$($ty_param,)* T> approx::RelativeEq for $self_ty<$($ty_param,)* T>
        where
            T: approx::RelativeEq,
            T::Epsilon: Clone,
            $hue_ty<T>: approx::RelativeEq + approx::AbsDiffEq<Epsilon = T::Epsilon>,
        {
            fn default_max_relative() -> T::Epsilon {
                T::default_max_relative()
            }

            fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
                $( self.$element.relative_eq(&other.$element, epsilon.clone(), max_relative.clone()) )&&+
            }
            fn relative_ne(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
                $( self.$element.relative_ne(&other.$element, epsilon.clone(), max_relative.clone()) )||+
            }
        }

        #[cfg(feature = "approx")]
        impl<$($ty_param,)* T> approx::UlpsEq for $self_ty<$($ty_param,)* T>
        where
            T: approx::UlpsEq,
            T::Epsilon: Clone,
            $hue_ty<T>: approx::UlpsEq + approx::AbsDiffEq<Epsilon = T::Epsilon>,
        {
            fn default_max_ulps() -> u32 {
                T::default_max_ulps()
            }

            fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
                $( self.$element.ulps_eq(&other.$element, epsilon.clone(), max_ulps) )&&+
            }
            fn ulps_ne(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
                $( self.$element.ulps_ne(&other.$element, epsilon.clone(), max_ulps) )||+
            }
        }
    }
}