From aa5071576401aef89d1dd372e8781a7eab104a5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20B=C3=A4renz?= Date: Wed, 16 Mar 2022 15:16:16 +0100 Subject: [PATCH] Implement Inv impl for Decimal (#494) --- src/arithmetic_impls.rs | 11 ++++++++++- tests/decimal_tests.rs | 7 ++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/arithmetic_impls.rs b/src/arithmetic_impls.rs index c51b6ebe..8600d4fe 100644 --- a/src/arithmetic_impls.rs +++ b/src/arithmetic_impls.rs @@ -3,7 +3,7 @@ use crate::{decimal::CalculationResult, ops, Decimal}; use core::ops::{Add, Div, Mul, Rem, Sub}; -use num_traits::{CheckedAdd, CheckedDiv, CheckedMul, CheckedRem, CheckedSub}; +use num_traits::{CheckedAdd, CheckedDiv, CheckedMul, CheckedRem, CheckedSub, Inv}; // Macros and `Decimal` implementations @@ -198,6 +198,15 @@ impl CheckedRem for Decimal { } } +impl Inv for Decimal { + type Output = Self; + + #[inline] + fn inv(self) -> Self { + Decimal::ONE / self + } +} + forward_all_binop!(impl Div for Decimal, div); impl<'a, 'b> Div<&'b Decimal> for &'a Decimal { type Output = Decimal; diff --git a/tests/decimal_tests.rs b/tests/decimal_tests.rs index 8cf0e211..667b80f2 100644 --- a/tests/decimal_tests.rs +++ b/tests/decimal_tests.rs @@ -5,7 +5,7 @@ use core::{ convert::{TryFrom, TryInto}, str::FromStr, }; -use num_traits::{Signed, ToPrimitive}; +use num_traits::{Inv, Signed, ToPrimitive}; use rust_decimal::{Decimal, Error, RoundingStrategy}; #[test] @@ -3366,6 +3366,11 @@ fn test_constants() { assert_eq!("2", Decimal::TWO.to_string()); } +#[test] +fn test_inv() { + assert_eq!("0.01", Decimal::ONE_HUNDRED.inv().to_string()); +} + // Mathematical features #[cfg(feature = "maths")] mod maths {