瀏覽代碼

Add Inv trait.

Clar Charr 7 年之前
父節點
當前提交
5bdff3f0ff
共有 3 個文件被更改,包括 31 次插入0 次删除
  1. 1 0
      src/lib.rs
  2. 29 0
      src/ops/inv.rs
  3. 1 0
      src/ops/mod.rs

+ 1 - 0
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;

+ 29 - 0
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);
+}

+ 1 - 0
src/ops/mod.rs

@@ -1,3 +1,4 @@
 pub mod saturating;
 pub mod checked;
 pub mod wrapping;
+pub mod new;