palette/macros/
equality.rs

1macro_rules! impl_eq {
2    (  $self_ty: ident , [$element: tt]) => {
3        impl_eq!($self_ty<>, [$element]);
4    };
5    (  $self_ty: ident < $($ty_param: ident),* > , [$element: tt]) => {
6        impl<$($ty_param,)* T> PartialEq for $self_ty<$($ty_param,)* T>
7        where
8            T: PartialEq,
9        {
10            fn eq(&self, other: &Self) -> bool {
11                self.$element == other.$element
12            }
13        }
14
15        impl<$($ty_param,)* T> Eq for $self_ty<$($ty_param,)* T> where T: Eq {}
16
17        #[cfg(feature = "approx")]
18        impl<$($ty_param,)* T> approx::AbsDiffEq for $self_ty<$($ty_param,)* T>
19        where
20            T: approx::AbsDiffEq,
21        {
22            type Epsilon = T::Epsilon;
23
24            fn default_epsilon() -> Self::Epsilon {
25                T::default_epsilon()
26            }
27
28            fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool {
29                self.$element.abs_diff_eq(&other.$element, epsilon)
30            }
31            fn abs_diff_ne(&self, other: &Self, epsilon: T::Epsilon) -> bool {
32                self.$element.abs_diff_ne(&other.$element, epsilon)
33            }
34        }
35
36        #[cfg(feature = "approx")]
37        impl<$($ty_param,)* T> approx::RelativeEq for $self_ty<$($ty_param,)* T>
38        where
39            T: approx::RelativeEq,
40        {
41            fn default_max_relative() -> T::Epsilon {
42                T::default_max_relative()
43            }
44
45            fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
46                self.$element.relative_eq(&other.$element, epsilon, max_relative)
47            }
48            fn relative_ne(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
49                self.$element.relative_ne(&other.$element, epsilon, max_relative)
50            }
51        }
52
53        #[cfg(feature = "approx")]
54        impl<$($ty_param,)* T> approx::UlpsEq for $self_ty<$($ty_param,)* T>
55        where
56            T: approx::UlpsEq,
57        {
58            fn default_max_ulps() -> u32 {
59                T::default_max_ulps()
60            }
61
62            fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
63                self.$element.ulps_eq(&other.$element, epsilon, max_ulps)
64            }
65            fn ulps_ne(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
66                self.$element.ulps_ne(&other.$element, epsilon, max_ulps)
67            }
68        }
69    };
70    (  $self_ty: ident , [$($element: ident),+]) => {
71        impl_eq!($self_ty<>, [$($element),+]);
72    };
73    (  $self_ty: ident < $($ty_param: ident),* > , [$($element: ident),+]) => {
74        impl<$($ty_param,)* T> PartialEq for $self_ty<$($ty_param,)* T>
75        where
76            T: PartialEq,
77        {
78            fn eq(&self, other: &Self) -> bool {
79                $( self.$element == other.$element )&&+
80            }
81        }
82
83        impl<$($ty_param,)* T> Eq for $self_ty<$($ty_param,)* T> where T: Eq {}
84
85        #[cfg(feature = "approx")]
86        impl<$($ty_param,)* T> approx::AbsDiffEq for $self_ty<$($ty_param,)* T>
87        where
88            T: approx::AbsDiffEq,
89            T::Epsilon: Clone,
90        {
91            type Epsilon = T::Epsilon;
92
93            fn default_epsilon() -> Self::Epsilon {
94                T::default_epsilon()
95            }
96
97            fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool {
98                $( self.$element.abs_diff_eq(&other.$element, epsilon.clone()) )&&+
99            }
100            fn abs_diff_ne(&self, other: &Self, epsilon: T::Epsilon) -> bool {
101                $( self.$element.abs_diff_ne(&other.$element, epsilon.clone()) )||+
102            }
103        }
104
105        #[cfg(feature = "approx")]
106        impl<$($ty_param,)* T> approx::RelativeEq for $self_ty<$($ty_param,)* T>
107        where
108            T: approx::RelativeEq,
109            T::Epsilon: Clone,
110        {
111            fn default_max_relative() -> T::Epsilon {
112                T::default_max_relative()
113            }
114
115            fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
116                $( self.$element.relative_eq(&other.$element, epsilon.clone(), max_relative.clone()) )&&+
117            }
118            fn relative_ne(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
119                $( self.$element.relative_ne(&other.$element, epsilon.clone(), max_relative.clone()) )||+
120            }
121        }
122
123        #[cfg(feature = "approx")]
124        impl<$($ty_param,)* T> approx::UlpsEq for $self_ty<$($ty_param,)* T>
125        where
126            T: approx::UlpsEq,
127            T::Epsilon: Clone,
128        {
129            fn default_max_ulps() -> u32 {
130                T::default_max_ulps()
131            }
132
133            fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
134                $( self.$element.ulps_eq(&other.$element, epsilon.clone(), max_ulps) )&&+
135            }
136            fn ulps_ne(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
137                $( self.$element.ulps_ne(&other.$element, epsilon.clone(), max_ulps) )||+
138            }
139        }
140    }
141}
142
143macro_rules! impl_eq_hue {
144    (  $self_ty: ident, $hue_ty: ident, [$($element: ident),+]) => {
145        impl_eq_hue!($self_ty<>, $hue_ty, [$($element),+]);
146    };
147    (  $self_ty: ident < $($ty_param: ident),* >, $hue_ty: ident, [$($element: ident),+]) => {
148
149        impl<$($ty_param,)* T> PartialEq for $self_ty<$($ty_param,)* T>
150        where
151            T: PartialEq,
152            $hue_ty<T>: PartialEq,
153        {
154            fn eq(&self, other: &Self) -> bool {
155                $( self.$element == other.$element )&&+
156            }
157        }
158
159        impl<$($ty_param,)* T> Eq for $self_ty<$($ty_param,)* T>
160        where
161            T: Eq,
162            $hue_ty<T>: Eq,
163        {}
164
165        #[cfg(feature = "approx")]
166        impl<$($ty_param,)* T> approx::AbsDiffEq for $self_ty<$($ty_param,)* T>
167        where
168            T: approx::AbsDiffEq,
169            T::Epsilon: Clone,
170            $hue_ty<T>: approx::AbsDiffEq<Epsilon = T::Epsilon>,
171        {
172            type Epsilon = T::Epsilon;
173
174            fn default_epsilon() -> Self::Epsilon {
175                T::default_epsilon()
176            }
177
178            fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool {
179                $( self.$element.abs_diff_eq(&other.$element, epsilon.clone()) )&&+
180            }
181            fn abs_diff_ne(&self, other: &Self, epsilon: T::Epsilon) -> bool {
182                $( self.$element.abs_diff_ne(&other.$element, epsilon.clone()) )||+
183            }
184        }
185
186        #[cfg(feature = "approx")]
187        impl<$($ty_param,)* T> approx::RelativeEq for $self_ty<$($ty_param,)* T>
188        where
189            T: approx::RelativeEq,
190            T::Epsilon: Clone,
191            $hue_ty<T>: approx::RelativeEq + approx::AbsDiffEq<Epsilon = T::Epsilon>,
192        {
193            fn default_max_relative() -> T::Epsilon {
194                T::default_max_relative()
195            }
196
197            fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
198                $( self.$element.relative_eq(&other.$element, epsilon.clone(), max_relative.clone()) )&&+
199            }
200            fn relative_ne(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
201                $( self.$element.relative_ne(&other.$element, epsilon.clone(), max_relative.clone()) )||+
202            }
203        }
204
205        #[cfg(feature = "approx")]
206        impl<$($ty_param,)* T> approx::UlpsEq for $self_ty<$($ty_param,)* T>
207        where
208            T: approx::UlpsEq,
209            T::Epsilon: Clone,
210            $hue_ty<T>: approx::UlpsEq + approx::AbsDiffEq<Epsilon = T::Epsilon>,
211        {
212            fn default_max_ulps() -> u32 {
213                T::default_max_ulps()
214            }
215
216            fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
217                $( self.$element.ulps_eq(&other.$element, epsilon.clone(), max_ulps) )&&+
218            }
219            fn ulps_ne(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
220                $( self.$element.ulps_ne(&other.$element, epsilon.clone(), max_ulps) )||+
221            }
222        }
223    }
224}