Add Inv trait.

This commit is contained in:
Clar Charr 2018-02-18 14:29:21 -05:00
parent 3431da80a2
commit 5bdff3f0ff
3 changed files with 31 additions and 0 deletions

View File

@ -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
src/ops/inv.rs Normal file
View File

@ -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);
}

View File

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