From: Ian Jackson Date: Sun, 21 Feb 2021 16:25:40 +0000 (+0000) Subject: arithmetic checking: Break out CheckedArithMul X-Git-Tag: otter-0.4.0~403 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=3e731ad389f848cac52319087383ec979e0d5855;p=otter.git arithmetic checking: Break out CheckedArithMul Nothing uses this yet so NFC. Signed-off-by: Ian Jackson --- diff --git a/src/utils.rs b/src/utils.rs index dc94e3c3..47e537ed 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -189,9 +189,11 @@ pub struct CoordinateOverflow; pub trait CheckedArith: Copy + Clone + Debug + 'static { fn checked_add(self, rhs: Self) -> Result; fn checked_sub(self, rhs: Self) -> Result; - fn checked_mul(self, rhs: Self) -> Result; fn checked_neg(self) -> Result; - fn checked_mulf(self, rhs: f64) -> Result; +} +pub trait CheckedArithMul: + Copy + Clone + Debug + 'static { + fn checked_mul(self, rhs: RHS) -> Result; } macro_rules! checked_inherent { {$n:ident($($formal:tt)*) $($actual:tt)*} => { @@ -203,9 +205,13 @@ macro_rules! checked_inherent { {$n:ident($($formal:tt)*) $($actual:tt)*} => { impl CheckedArith for i32 { checked_inherent!{checked_add(, rhs: Self) rhs} checked_inherent!{checked_sub(, rhs: Self) rhs} - checked_inherent!{checked_mul(, rhs: Self) rhs} checked_inherent!{checked_neg( ) } - fn checked_mulf(self, rhs: f64) -> Result { +} +impl CheckedArithMul for i32 { + checked_inherent!{checked_mul(, rhs: Self) rhs} +} +impl CheckedArithMul for i32 { + fn checked_mul(self, rhs: f64) -> Result { let lhs: f64 = self.into(); let out: f64 = lhs.checked_mul(rhs)?; let out: Self = num::NumCast::from(out).ok_or(CoordinateOverflow)?; @@ -221,12 +227,13 @@ macro_rules! checked_float { {$n:ident($($formal:tt)*) $($modify:tt)*} => { } } impl CheckedArith for f64 { - checked_float!{checked_add (, rhs: Self) + rhs } - checked_float!{checked_sub (, rhs: Self) - rhs } - checked_float!{checked_mul (, rhs: Self) * rhs } - checked_float!{checked_mulf(, rhs: Self) * rhs } + checked_float!{checked_add(, rhs: Self) + rhs } + checked_float!{checked_sub(, rhs: Self) - rhs } checked_float!{checked_neg() .neg()} } +impl CheckedArithMul for f64 { + checked_float!{checked_mul(, rhs: Self) * rhs } +} pub fn toml_merge<'u, S: 'u + AsRef,