changeset 23:2d5ae243921c

lib: move signal math into a common macro Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Wed, 10 Jun 2020 12:29:27 -0400
parents e548cd172539
children daa09aecdc40
files src/lib/signal/filesource.rs src/lib/signal/gain.rs src/lib/signal/math.rs src/lib/signal/mixer.rs src/lib/signal/sinusoid.rs
diffstat 5 files changed, 118 insertions(+), 100 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/signal/filesource.rs	Wed Jun 10 11:54:52 2020 -0400
+++ b/src/lib/signal/filesource.rs	Wed Jun 10 12:29:27 2020 -0400
@@ -76,28 +76,5 @@
     }
 }
 
-/* mix two signals */
-impl<T: BufRead, U: SignalFormatter, V: Signal> Mul<V> for FileSourceSignal<T, U> {
-    type Output = Mixer<FileSourceSignal<T, U>, V>;
-
-    fn mul(self, rhs: V) -> Self::Output {
-        Mixer::new(self, rhs)
-    }
-}
-
-/* add gain to a signal */
-impl<T: BufRead, U: SignalFormatter> Mul<Scalar> for FileSourceSignal<T, U> {
-    type Output = Gain<FileSourceSignal<T, U>>;
-
-    fn mul(self, rhs: Scalar) -> Self::Output {
-        Gain::new(self, rhs)
-    }
-}
-
-impl<T: BufRead, U: SignalFormatter> Mul<FileSourceSignal<T, U>> for Scalar {
-    type Output = Gain<FileSourceSignal<T, U>>;
-
-    fn mul(self, rhs: FileSourceSignal<T, U>) -> Self::Output {
-        Gain::new(rhs, self)
-    }
-}
+include!("math.rs");
+signal_math!(FileSourceSignal<BufRead, SignalFormatter>);
--- a/src/lib/signal/gain.rs	Wed Jun 10 11:54:52 2020 -0400
+++ b/src/lib/signal/gain.rs	Wed Jun 10 12:29:27 2020 -0400
@@ -66,28 +66,5 @@
     }
 }
 
-/* mix two signals */
-impl<T: Signal, U: Signal> Mul<U> for Gain<T> {
-    type Output = Mixer<Gain<T>, U>;
-
-    fn mul(self, rhs: U) -> Self::Output {
-        Mixer::new(self, rhs)
-    }
-}
-
-/* add gain to a signal */
-impl<T: Signal> Mul<Scalar> for Gain<T> {
-    type Output = Gain<Gain<T>>;
-
-    fn mul(self, rhs: Scalar) -> Self::Output {
-        Gain::new(self, rhs)
-    }
-}
-
-impl<T: Signal> Mul<Gain<T>> for Scalar {
-    type Output = Gain<Gain<T>>;
-
-    fn mul(self, rhs: Gain<T>) -> Self::Output {
-        Gain::new(rhs, self)
-    }
-}
+include!("math.rs");
+signal_math!(Gain<Signal>);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/signal/math.rs	Wed Jun 10 12:29:27 2020 -0400
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2020 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+macro_rules! signal_math {
+    /* Foo signal */
+    ($type:ident) => {
+        /* mix two signals */
+        impl<T: Signal> Mul<T> for $type {
+            type Output = Mixer<$type, T>;
+
+            fn mul(self, rhs: T) -> Self::Output {
+                Mixer::new(self, rhs)
+            }
+        }
+
+        /* add gain to a signal */
+        impl Mul<Scalar> for $type {
+            type Output = Gain<$type>;
+
+            fn mul(self, rhs: Scalar) -> Self::Output {
+                Gain::new(self, rhs)
+            }
+        }
+
+        impl Mul<$type> for Scalar {
+            type Output = Gain<$type>;
+
+            fn mul(self, rhs: $type) -> Self::Output {
+                Gain::new(rhs, self)
+            }
+        }
+    };
+
+    /* Foo<T> signal */
+    ($type:ident < $lim:ident >) => {
+        /* mix two signals */
+        impl<T: $lim, U: Signal> Mul<U> for $type<T> {
+            type Output = Mixer<$type<T>, U>;
+
+            fn mul(self, rhs: U) -> Self::Output {
+                Mixer::new(self, rhs)
+            }
+        }
+
+        /* add gain to a signal */
+        impl<T: $lim> Mul<Scalar> for $type<T> {
+            type Output = Gain<$type<T>>;
+
+            fn mul(self, rhs: Scalar) -> Self::Output {
+                Gain::new(self, rhs)
+            }
+        }
+
+        impl<T: $lim> Mul<$type<T>> for Scalar {
+            type Output = Gain<$type<T>>;
+
+            fn mul(self, rhs: $type<T>) -> Self::Output {
+                Gain::new(rhs, self)
+            }
+        }
+    };
+
+    /* Foo<T, U> signal */
+    ($type:ident < $lim1:ident, $lim2:ident >) => {
+        /* mix two signals */
+        impl<T: $lim1, U: $lim2, V: Signal> Mul<V> for $type<T, U> {
+            type Output = Mixer<$type<T, U>, V>;
+
+            fn mul(self, rhs: V) -> Self::Output {
+                Mixer::new(self, rhs)
+            }
+        }
+
+        /* add gain to a signal */
+        impl<T: $lim1, U: $lim2> Mul<Scalar> for $type<T, U> {
+            type Output = Gain<$type<T, U>>;
+
+            fn mul(self, rhs: Scalar) -> Self::Output {
+                Gain::new(self, rhs)
+            }
+        }
+
+        impl<T: $lim1, U: $lim2> Mul<$type<T, U>> for Scalar {
+            type Output = Gain<$type<T, U>>;
+
+            fn mul(self, rhs: $type<T, U>) -> Self::Output {
+                Gain::new(rhs, self)
+            }
+        }
+    };
+}
--- a/src/lib/signal/mixer.rs	Wed Jun 10 11:54:52 2020 -0400
+++ b/src/lib/signal/mixer.rs	Wed Jun 10 12:29:27 2020 -0400
@@ -73,28 +73,5 @@
     }
 }
 
-/* mix two signals */
-impl<T: Signal, U: Signal, V: Signal> Mul<V> for Mixer<T, U> {
-    type Output = Mixer<Mixer<T, U>, V>;
-
-    fn mul(self, rhs: V) -> Self::Output {
-        Mixer::new(self, rhs)
-    }
-}
-
-/* add gain to a signal */
-impl<T: Signal, U: Signal> Mul<Scalar> for Mixer<T, U> {
-    type Output = Gain<Mixer<T, U>>;
-
-    fn mul(self, rhs: Scalar) -> Self::Output {
-        Gain::new(self, rhs)
-    }
-}
-
-impl<T: Signal, U: Signal> Mul<Mixer<T, U>> for Scalar {
-    type Output = Gain<Mixer<T, U>>;
-
-    fn mul(self, rhs: Mixer<T, U>) -> Self::Output {
-        Gain::new(rhs, self)
-    }
-}
+include!("math.rs");
+signal_math!(Mixer<Signal, Signal>);
--- a/src/lib/signal/sinusoid.rs	Wed Jun 10 11:54:52 2020 -0400
+++ b/src/lib/signal/sinusoid.rs	Wed Jun 10 12:29:27 2020 -0400
@@ -81,28 +81,5 @@
     }
 }
 
-/* mix two signals */
-impl<T: Signal> Mul<T> for Sinusoid {
-    type Output = Mixer<Sinusoid, T>;
-
-    fn mul(self, rhs: T) -> Self::Output {
-        Mixer::new(self, rhs)
-    }
-}
-
-/* add gain to a signal */
-impl Mul<Scalar> for Sinusoid {
-    type Output = Gain<Sinusoid>;
-
-    fn mul(self, rhs: Scalar) -> Self::Output {
-        Gain::new(self, rhs)
-    }
-}
-
-impl Mul<Sinusoid> for Scalar {
-    type Output = Gain<Sinusoid>;
-
-    fn mul(self, rhs: Sinusoid) -> Self::Output {
-        Gain::new(rhs, self)
-    }
-}
+include!("math.rs");
+signal_math!(Sinusoid);