add scalar operations
authoralfadur
Tue, 16 Oct 2018 05:09:42 +0300
changeset 13927 cf28d7a2b7fe
parent 13926 2717a5289d88
child 13928 5fdc41cd0841
add scalar operations
rust/fpnum/src/lib.rs
--- a/rust/fpnum/src/lib.rs	Tue Oct 16 04:44:11 2018 +0300
+++ b/rust/fpnum/src/lib.rs	Tue Oct 16 05:09:42 2018 +0300
@@ -8,6 +8,7 @@
 }
 
 impl FPNum {
+    #[inline]
     fn new(numerator: i32, denominator: u32) -> Self {
         FPNum::from(numerator) / denominator
     }
@@ -85,6 +86,7 @@
         }
     }
 
+    #[inline]
     fn with_sign(&self, is_negative: bool) -> FPNum {
         FPNum{ is_negative, ..*self}
     }
@@ -93,6 +95,11 @@
     fn with_sign_as(self, other: FPNum) -> FPNum {
         self.with_sign(other.is_negative)
     }
+
+    #[inline]
+    fn point(self) -> FPPoint {
+        FPPoint::new(self, self)
+    }
 }
 
 impl From<i32> for FPNum {
@@ -378,7 +385,7 @@
 }
 
 macro_rules! bin_op_impl {
-    ($op: path, $name: tt) => {
+    ($op: ty, $name: tt) => {
         impl $op for FPPoint {
             type Output = Self;
 
@@ -391,9 +398,27 @@
     }
 }
 
+macro_rules! scalar_bin_op_impl {
+    ($op: ty, $name: tt) => {
+        impl $op for FPPoint {
+            type Output = Self;
+
+            #[inline]
+            fn $name(self, rhs: FPNum) -> Self {
+                Self::new(self.x().$name(rhs),
+                          self.y().$name(rhs))
+            }
+        }
+    }
+}
+
 bin_op_impl!(ops::Add, add);
 bin_op_impl!(ops::Sub, sub);
 bin_op_impl!(ops::Mul, mul);
+scalar_bin_op_impl!(ops::Add<FPNum>, add);
+scalar_bin_op_impl!(ops::Mul<FPNum>, mul);
+scalar_bin_op_impl!(ops::Sub<FPNum>, sub);
+scalar_bin_op_impl!(ops::Div<FPNum>, div);
 
 #[inline]
 fn distance<T>(x: T, y: T) -> FPNum
@@ -490,4 +515,5 @@
     assert_eq!(p * z, z);
     assert_eq!(p.dot(&z), fp!(0));
     assert_eq!(distance(4, 3), fp!(5));
+    assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6)));
 }
\ No newline at end of file