# HG changeset patch # User alfadur # Date 1539655782 -10800 # Node ID cf28d7a2b7fe579d57218cccefdcd7bffc769aa0 # Parent 2717a5289d887281557aa8c8179ed25f8836d006 add scalar operations diff -r 2717a5289d88 -r cf28d7a2b7fe 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 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, add); +scalar_bin_op_impl!(ops::Mul, mul); +scalar_bin_op_impl!(ops::Sub, sub); +scalar_bin_op_impl!(ops::Div, div); #[inline] fn distance(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