| 35 impl<A, B> From<Pair<A, B>> for (A, B) { |
35 impl<A, B> From<Pair<A, B>> for (A, B) { |
| 36 #[inline] |
36 #[inline] |
| 37 fn from(Pair(a, b): Pair<A, B>) -> (A, B) { |
37 fn from(Pair(a, b): Pair<A, B>) -> (A, B) { |
| 38 (a, b) |
38 (a, b) |
| 39 } |
39 } |
| 40 } |
|
| 41 |
|
| 42 macro_rules! impl_binop { |
|
| 43 (($a : ty, $b : ty), $trait : ident, $fn : ident, $refl:ident, $refr:ident) => { |
|
| 44 impl_binop!(@doit: $a, $b, $trait, $fn; |
|
| 45 maybe_lifetime!($refl, &'l Pair<$a,$b>), |
|
| 46 (maybe_lifetime!($refl, &'l $a), |
|
| 47 maybe_lifetime!($refl, &'l $b)); |
|
| 48 maybe_lifetime!($refr, &'r Pair<Ai,Bi>), |
|
| 49 (maybe_lifetime!($refr, &'r Ai), |
|
| 50 maybe_lifetime!($refr, &'r Bi)); |
|
| 51 $refl, $refr); |
|
| 52 }; |
|
| 53 |
|
| 54 (@doit: $a:ty, $b:ty, |
|
| 55 $trait:ident, $fn:ident; |
|
| 56 $self:ty, ($aself:ty, $bself:ty); |
|
| 57 $in:ty, ($ain:ty, $bin:ty); |
|
| 58 $refl:ident, $refr:ident) => { |
|
| 59 impl<'l, 'r, Ai, Bi> $trait<$in> |
|
| 60 for $self |
|
| 61 where $aself: $trait<$ain>, |
|
| 62 $bself: $trait<$bin> { |
|
| 63 type Output = Pair<<$aself as $trait<$ain>>::Output, |
|
| 64 <$bself as $trait<$bin>>::Output>; |
|
| 65 |
|
| 66 #[inline] |
|
| 67 fn $fn(self, y : $in) -> Self::Output { |
|
| 68 Pair(maybe_ref!($refl, self.0).$fn(maybe_ref!($refr, y.0)), |
|
| 69 maybe_ref!($refl, self.1).$fn(maybe_ref!($refr, y.1))) |
|
| 70 } |
|
| 71 } |
|
| 72 }; |
|
| 73 } |
40 } |
| 74 |
41 |
| 75 macro_rules! impl_assignop { |
42 macro_rules! impl_assignop { |
| 76 (($a : ty, $b : ty), $trait : ident, $fn : ident, $refr:ident) => { |
43 (($a : ty, $b : ty), $trait : ident, $fn : ident, $refr:ident) => { |
| 77 impl_assignop!(@doit: $a, $b, |
44 impl_assignop!(@doit: $a, $b, |
| 229 }; |
196 }; |
| 230 } |
197 } |
| 231 |
198 |
| 232 impl_unary!(Neg, neg); |
199 impl_unary!(Neg, neg); |
| 233 |
200 |
| |
201 macro_rules! impl_binary { |
| |
202 ($trait:ident, $fn:ident) => { |
| |
203 impl<A, B, C, D> $trait<Pair<C, D>> for Pair<A, B> |
| |
204 where |
| |
205 A: $trait<C>, |
| |
206 B: $trait<D>, |
| |
207 { |
| |
208 type Output = Pair<A::Output, B::Output>; |
| |
209 fn $fn(self, Pair(c, d): Pair<C, D>) -> Self::Output { |
| |
210 let Pair(a, b) = self; |
| |
211 Pair(a.$fn(c), b.$fn(d)) |
| |
212 } |
| |
213 } |
| |
214 |
| |
215 impl<'a, A, B, C, D> $trait<Pair<C, D>> for &'a Pair<A, B> |
| |
216 where |
| |
217 &'a A: $trait<C>, |
| |
218 &'a B: $trait<D>, |
| |
219 { |
| |
220 type Output = Pair<<&'a A as $trait<C>>::Output, <&'a B as $trait<D>>::Output>; |
| |
221 fn $fn(self, Pair(c, d): Pair<C, D>) -> Self::Output { |
| |
222 let Pair(ref a, ref b) = self; |
| |
223 Pair(a.$fn(c), b.$fn(d)) |
| |
224 } |
| |
225 } |
| |
226 |
| |
227 impl<'a, 'b, A, B, C, D> $trait<&'b Pair<C, D>> for &'a Pair<A, B> |
| |
228 where |
| |
229 &'a A: $trait<&'b C>, |
| |
230 &'a B: $trait<&'b D>, |
| |
231 { |
| |
232 type Output = Pair<<&'a A as $trait<&'b C>>::Output, <&'a B as $trait<&'b D>>::Output>; |
| |
233 fn $fn(self, Pair(ref c, ref d): &'b Pair<C, D>) -> Self::Output { |
| |
234 let Pair(ref a, ref b) = self; |
| |
235 Pair(a.$fn(c), b.$fn(d)) |
| |
236 } |
| |
237 } |
| |
238 |
| |
239 impl<'b, A, B, C, D> $trait<&'b Pair<C, D>> for Pair<A, B> |
| |
240 where |
| |
241 A: $trait<&'b C>, |
| |
242 B: $trait<&'b D>, |
| |
243 { |
| |
244 type Output = Pair<<A as $trait<&'b C>>::Output, <B as $trait<&'b D>>::Output>; |
| |
245 fn $fn(self, Pair(ref c, ref d): &'b Pair<C, D>) -> Self::Output { |
| |
246 let Pair(a, b) = self; |
| |
247 Pair(a.$fn(c), b.$fn(d)) |
| |
248 } |
| |
249 } |
| |
250 }; |
| |
251 } |
| |
252 |
| |
253 impl_binary!(Add, add); |
| |
254 impl_binary!(Sub, sub); |
| |
255 |
| 234 #[macro_export] |
256 #[macro_export] |
| 235 macro_rules! impl_pair_vectorspace_ops { |
257 macro_rules! impl_pair_vectorspace_ops { |
| 236 (($a:ty, $b:ty), $field:ty) => { |
258 (($a:ty, $b:ty), $field:ty) => { |
| 237 impl_pair_vectorspace_ops!(@binary, ($a, $b), Add, add); |
259 //impl_pair_vectorspace_ops!(@binary, ($a, $b), Add, add); |
| 238 impl_pair_vectorspace_ops!(@binary, ($a, $b), Sub, sub); |
260 //impl_pair_vectorspace_ops!(@binary, ($a, $b), Sub, sub); |
| 239 impl_pair_vectorspace_ops!(@assign, ($a, $b), AddAssign, add_assign); |
261 impl_pair_vectorspace_ops!(@assign, ($a, $b), AddAssign, add_assign); |
| 240 impl_pair_vectorspace_ops!(@assign, ($a, $b), SubAssign, sub_assign); |
262 impl_pair_vectorspace_ops!(@assign, ($a, $b), SubAssign, sub_assign); |
| 241 // impl_pair_vectorspace_ops!(@scalar, ($a, $b), $field, Mul, mul); |
263 // impl_pair_vectorspace_ops!(@scalar, ($a, $b), $field, Mul, mul); |
| 242 // impl_pair_vectorspace_ops!(@scalar, ($a, $b), $field, Div, div); |
264 // impl_pair_vectorspace_ops!(@scalar, ($a, $b), $field, Div, div); |
| 243 // Compiler overflow |
265 // Compiler overflow |
| 244 // $( |
266 // $( |
| 245 // impl_pair_vectorspace_ops!(@scalar_lhs, ($a, $b), $field, $impl_scalarlhs_op, Mul, mul); |
267 // impl_pair_vectorspace_ops!(@scalar_lhs, ($a, $b), $field, $impl_scalarlhs_op, Mul, mul); |
| 246 // )* |
268 // )* |
| 247 impl_pair_vectorspace_ops!(@scalar_assign, ($a, $b), $field, MulAssign, mul_assign); |
269 impl_pair_vectorspace_ops!(@scalar_assign, ($a, $b), $field, MulAssign, mul_assign); |
| 248 impl_pair_vectorspace_ops!(@scalar_assign, ($a, $b), $field, DivAssign, div_assign); |
270 impl_pair_vectorspace_ops!(@scalar_assign, ($a, $b), $field, DivAssign, div_assign); |
| 249 impl_pair_vectorspace_ops!(@unary, ($a, $b), Neg, neg); |
|
| 250 }; |
271 }; |
| 251 (@binary, ($a : ty, $b : ty), $trait : ident, $fn : ident) => { |
272 (@binary, ($a : ty, $b : ty), $trait : ident, $fn : ident) => { |
| 252 impl_binop!(($a, $b), $trait, $fn, ref, ref); |
273 impl_binop!(($a, $b), $trait, $fn, ref, ref); |
| 253 impl_binop!(($a, $b), $trait, $fn, ref, noref); |
274 impl_binop!(($a, $b), $trait, $fn, ref, noref); |
| 254 impl_binop!(($a, $b), $trait, $fn, noref, ref); |
275 impl_binop!(($a, $b), $trait, $fn, noref, ref); |
| 266 impl_scalarlhs_op!(($a, $b), $field, $trait, $fn, ref); |
287 impl_scalarlhs_op!(($a, $b), $field, $trait, $fn, ref); |
| 267 impl_scalarlhs_op!(($a, $b), $field, $trait, $fn, noref); |
288 impl_scalarlhs_op!(($a, $b), $field, $trait, $fn, noref); |
| 268 }; |
289 }; |
| 269 (@scalar_assign, ($a : ty, $b : ty), $field : ty, $trait : ident, $fn : ident) => { |
290 (@scalar_assign, ($a : ty, $b : ty), $field : ty, $trait : ident, $fn : ident) => { |
| 270 impl_scalar_assignop!(($a, $b), $field, $trait, $fn); |
291 impl_scalar_assignop!(($a, $b), $field, $trait, $fn); |
| 271 }; |
|
| 272 (@unary, ($a : ty, $b : ty), $trait : ident, $fn : ident) => { |
|
| 273 //impl_unaryop!(($a, $b), $trait, $fn, ref); |
|
| 274 //impl_unaryop!(($a, $b), $trait, $fn, noref); |
|
| 275 }; |
292 }; |
| 276 } |
293 } |
| 277 |
294 |
| 278 // TODO: add what we can here. |
295 // TODO: add what we can here. |
| 279 #[macro_export] |
296 #[macro_export] |
| 285 // impl_pair_vectorspace_ops_gen!(@assign, SubAssign, sub_assign); |
302 // impl_pair_vectorspace_ops_gen!(@assign, SubAssign, sub_assign); |
| 286 impl_pair_vectorspace_ops_gen!(@scalar, $field, Mul, mul); |
303 impl_pair_vectorspace_ops_gen!(@scalar, $field, Mul, mul); |
| 287 impl_pair_vectorspace_ops_gen!(@scalar, $field, Div, div); |
304 impl_pair_vectorspace_ops_gen!(@scalar, $field, Div, div); |
| 288 // impl_pair_vectorspace_ops_gen!(@scalar_assign, $field, MulAssign, mul_assign); |
305 // impl_pair_vectorspace_ops_gen!(@scalar_assign, $field, MulAssign, mul_assign); |
| 289 // impl_pair_vectorspace_ops_gen!(@scalar_assign, $field, DivAssign, div_assign); |
306 // impl_pair_vectorspace_ops_gen!(@scalar_assign, $field, DivAssign, div_assign); |
| 290 //impl_unary!(Neg, neg); |
|
| 291 }; |
307 }; |
| 292 // (@binary, $trait : ident, $fn : ident) => { |
308 // (@binary, $trait : ident, $fn : ident) => { |
| 293 // impl_binop_gen!(($a, $b), $trait, $fn, ref, ref); |
309 // impl_binop_gen!(($a, $b), $trait, $fn, ref, ref); |
| 294 // impl_binop_gen!(($a, $b), $trait, $fn, ref, noref); |
310 // impl_binop_gen!(($a, $b), $trait, $fn, ref, noref); |
| 295 // impl_binop_gen!(($a, $b), $trait, $fn, noref, ref); |
311 // impl_binop_gen!(($a, $b), $trait, $fn, noref, ref); |
| 307 // impl_scalarlhs_op_gen!(($a, $b), $field, $trait, $fn, ref); |
323 // impl_scalarlhs_op_gen!(($a, $b), $field, $trait, $fn, ref); |
| 308 // impl_scalarlhs_op_gen!(($a, $b), $field, $trait, $fn, noref); |
324 // impl_scalarlhs_op_gen!(($a, $b), $field, $trait, $fn, noref); |
| 309 // }; |
325 // }; |
| 310 // (@scalar_assign, $field : ty, $trait : ident, $fn : ident) => { |
326 // (@scalar_assign, $field : ty, $trait : ident, $fn : ident) => { |
| 311 // impl_scalar_assignop_gen!(($a, $b), $field, $trait, $fn); |
327 // impl_scalar_assignop_gen!(($a, $b), $field, $trait, $fn); |
| 312 // }; |
|
| 313 // (@unary, $trait : ident, $fn : ident) => { |
|
| 314 // //impl_unaryop_gen!($trait, $fn, ref); |
|
| 315 // //impl_unaryop_gen!($trait, $fn, noref); |
|
| 316 // }; |
328 // }; |
| 317 } |
329 } |
| 318 |
330 |
| 319 impl_pair_vectorspace_ops_gen!(f32); |
331 impl_pair_vectorspace_ops_gen!(f32); |
| 320 impl_pair_vectorspace_ops_gen!(f64); |
332 impl_pair_vectorspace_ops_gen!(f64); |