binops dev

Thu, 01 May 2025 01:25:27 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Thu, 01 May 2025 01:25:27 -0500
branch
dev
changeset 116
6f22e4d277b4
parent 115
f9aec20d0286
child 117
9b782aa6b452

binops

src/direct_product.rs file | annotate | diff | comparison | revisions
--- a/src/direct_product.rs	Thu May 01 01:16:13 2025 -0500
+++ b/src/direct_product.rs	Thu May 01 01:25:27 2025 -0500
@@ -39,39 +39,6 @@
     }
 }
 
-macro_rules! impl_binop {
-    (($a : ty, $b : ty), $trait : ident, $fn : ident, $refl:ident, $refr:ident) => {
-        impl_binop!(@doit: $a, $b, $trait, $fn;
-                           maybe_lifetime!($refl, &'l Pair<$a,$b>),
-                           (maybe_lifetime!($refl, &'l $a),
-                            maybe_lifetime!($refl, &'l $b));
-                           maybe_lifetime!($refr, &'r Pair<Ai,Bi>),
-                           (maybe_lifetime!($refr, &'r Ai),
-                            maybe_lifetime!($refr, &'r Bi));
-                           $refl, $refr);
-    };
-
-    (@doit: $a:ty, $b:ty,
-            $trait:ident, $fn:ident;
-            $self:ty, ($aself:ty, $bself:ty);
-            $in:ty, ($ain:ty, $bin:ty);
-            $refl:ident, $refr:ident) => {
-        impl<'l, 'r, Ai, Bi> $trait<$in>
-        for $self
-        where $aself: $trait<$ain>,
-              $bself: $trait<$bin> {
-            type Output = Pair<<$aself as $trait<$ain>>::Output,
-                               <$bself as $trait<$bin>>::Output>;
-
-            #[inline]
-            fn $fn(self, y : $in) -> Self::Output {
-                Pair(maybe_ref!($refl, self.0).$fn(maybe_ref!($refr, y.0)),
-                     maybe_ref!($refl, self.1).$fn(maybe_ref!($refr, y.1)))
-            }
-        }
-    };
-}
-
 macro_rules! impl_assignop {
     (($a : ty, $b : ty), $trait : ident, $fn : ident, $refr:ident) => {
         impl_assignop!(@doit: $a, $b,
@@ -231,11 +198,66 @@
 
 impl_unary!(Neg, neg);
 
+macro_rules! impl_binary {
+    ($trait:ident, $fn:ident) => {
+        impl<A, B, C, D> $trait<Pair<C, D>> for Pair<A, B>
+        where
+            A: $trait<C>,
+            B: $trait<D>,
+        {
+            type Output = Pair<A::Output, B::Output>;
+            fn $fn(self, Pair(c, d): Pair<C, D>) -> Self::Output {
+                let Pair(a, b) = self;
+                Pair(a.$fn(c), b.$fn(d))
+            }
+        }
+
+        impl<'a, A, B, C, D> $trait<Pair<C, D>> for &'a Pair<A, B>
+        where
+            &'a A: $trait<C>,
+            &'a B: $trait<D>,
+        {
+            type Output = Pair<<&'a A as $trait<C>>::Output, <&'a B as $trait<D>>::Output>;
+            fn $fn(self, Pair(c, d): Pair<C, D>) -> Self::Output {
+                let Pair(ref a, ref b) = self;
+                Pair(a.$fn(c), b.$fn(d))
+            }
+        }
+
+        impl<'a, 'b, A, B, C, D> $trait<&'b Pair<C, D>> for &'a Pair<A, B>
+        where
+            &'a A: $trait<&'b C>,
+            &'a B: $trait<&'b D>,
+        {
+            type Output = Pair<<&'a A as $trait<&'b C>>::Output, <&'a B as $trait<&'b D>>::Output>;
+            fn $fn(self, Pair(ref c, ref d): &'b Pair<C, D>) -> Self::Output {
+                let Pair(ref a, ref b) = self;
+                Pair(a.$fn(c), b.$fn(d))
+            }
+        }
+
+        impl<'b, A, B, C, D> $trait<&'b Pair<C, D>> for Pair<A, B>
+        where
+            A: $trait<&'b C>,
+            B: $trait<&'b D>,
+        {
+            type Output = Pair<<A as $trait<&'b C>>::Output, <B as $trait<&'b D>>::Output>;
+            fn $fn(self, Pair(ref c, ref d): &'b Pair<C, D>) -> Self::Output {
+                let Pair(a, b) = self;
+                Pair(a.$fn(c), b.$fn(d))
+            }
+        }
+    };
+}
+
+impl_binary!(Add, add);
+impl_binary!(Sub, sub);
+
 #[macro_export]
 macro_rules! impl_pair_vectorspace_ops {
     (($a:ty, $b:ty), $field:ty) => {
-        impl_pair_vectorspace_ops!(@binary, ($a, $b), Add, add);
-        impl_pair_vectorspace_ops!(@binary, ($a, $b), Sub, sub);
+        //impl_pair_vectorspace_ops!(@binary, ($a, $b), Add, add);
+        //impl_pair_vectorspace_ops!(@binary, ($a, $b), Sub, sub);
         impl_pair_vectorspace_ops!(@assign, ($a, $b), AddAssign, add_assign);
         impl_pair_vectorspace_ops!(@assign, ($a, $b), SubAssign, sub_assign);
         // impl_pair_vectorspace_ops!(@scalar, ($a, $b), $field, Mul, mul);
@@ -246,7 +268,6 @@
         // )*
         impl_pair_vectorspace_ops!(@scalar_assign, ($a, $b), $field, MulAssign, mul_assign);
         impl_pair_vectorspace_ops!(@scalar_assign, ($a, $b), $field, DivAssign, div_assign);
-        impl_pair_vectorspace_ops!(@unary, ($a, $b), Neg, neg);
     };
     (@binary, ($a : ty, $b : ty), $trait : ident, $fn : ident) => {
         impl_binop!(($a, $b), $trait, $fn, ref, ref);
@@ -269,10 +290,6 @@
     (@scalar_assign, ($a : ty, $b : ty), $field : ty, $trait : ident, $fn : ident) => {
         impl_scalar_assignop!(($a, $b), $field, $trait, $fn);
     };
-    (@unary, ($a : ty, $b : ty), $trait : ident, $fn :  ident) => {
-        //impl_unaryop!(($a, $b), $trait, $fn, ref);
-        //impl_unaryop!(($a, $b), $trait, $fn, noref);
-    };
 }
 
 // TODO: add what we can here.
@@ -287,7 +304,6 @@
         impl_pair_vectorspace_ops_gen!(@scalar, $field, Div, div);
         // impl_pair_vectorspace_ops_gen!(@scalar_assign, $field, MulAssign, mul_assign);
         // impl_pair_vectorspace_ops_gen!(@scalar_assign, $field, DivAssign, div_assign);
-        //impl_unary!(Neg, neg);
     };
     // (@binary, $trait : ident, $fn : ident) => {
     //     impl_binop_gen!(($a, $b), $trait, $fn, ref, ref);
@@ -310,10 +326,6 @@
     // (@scalar_assign, $field : ty, $trait : ident, $fn : ident) => {
     //     impl_scalar_assignop_gen!(($a, $b), $field, $trait, $fn);
     // };
-    // (@unary, $trait : ident, $fn :  ident) => {
-    //     //impl_unaryop_gen!($trait, $fn, ref);
-    //     //impl_unaryop_gen!($trait, $fn, noref);
-    // };
 }
 
 impl_pair_vectorspace_ops_gen!(f32);

mercurial