From 5bdff3f0ff194b2f783fc55f2fffc8b905d457da Mon Sep 17 00:00:00 2001 From: Clar Charr Date: Sun, 18 Feb 2018 14:29:21 -0500 Subject: [PATCH] Add Inv trait. --- src/lib.rs | 1 + src/ops/inv.rs | 29 +++++++++++++++++++++++++++++ src/ops/mod.rs | 1 + 3 files changed, 31 insertions(+) create mode 100644 src/ops/inv.rs diff --git a/src/lib.rs b/src/lib.rs index 83a45f9..2b4844b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,6 +33,7 @@ pub use float::Float; pub use float::FloatConst; // pub use real::{FloatCore, Real}; // NOTE: Don't do this, it breaks `use num_traits::*;`. pub use identities::{Zero, One, zero, one}; +pub use ops::inv::Inv; pub use ops::checked::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv, CheckedShl, CheckedShr}; pub use ops::wrapping::{WrappingAdd, WrappingMul, WrappingSub}; pub use ops::saturating::Saturating; diff --git a/src/ops/inv.rs b/src/ops/inv.rs new file mode 100644 index 0000000..16f3643 --- /dev/null +++ b/src/ops/inv.rs @@ -0,0 +1,29 @@ +/// Unary operator for retrieving the multiplicative inverse, or reciprocal, of a value. +pub trait Inv { + /// The result after applying the operator. + type Output; + + /// Returns the multiplicative inverse of `Self`. + fn inv(self) -> Self::Output; +} + +macro_rules! inv_impl { + ($t:ty, $out:ty, $fn:expr) => { + impl<'a> Inv for $t { + type Output = $out; + + #[inline] + fn inv(self) -> $out { + ($fn)(self) + } + } + } +} + +#[cfg(feature = "std")] +mod float_impls { + inv_impl!(f32, f32, f32::recip); + inv_impl!(f64, f64, f64::recip); + inv_impl!(&'a f32, f32, f32::recip); + inv_impl!(&'a f64, f64, f64::recip); +} diff --git a/src/ops/mod.rs b/src/ops/mod.rs index ec9edeb..b83a2a5 100644 --- a/src/ops/mod.rs +++ b/src/ops/mod.rs @@ -1,3 +1,4 @@ pub mod saturating; pub mod checked; pub mod wrapping; +pub mod new;