Move Pow to pow module.

This commit is contained in:
Clar Charr 2018-02-19 15:17:41 -05:00
parent c1f4118b4e
commit ce3badca57
4 changed files with 76 additions and 74 deletions

View File

@ -34,14 +34,13 @@ pub use float::FloatConst;
// pub use real::{FloatCore, Real}; // NOTE: Don't do this, it breaks `use num_traits::*;`. // pub use real::{FloatCore, Real}; // NOTE: Don't do this, it breaks `use num_traits::*;`.
pub use identities::{Zero, One, zero, one}; pub use identities::{Zero, One, zero, one};
pub use ops::inv::Inv; pub use ops::inv::Inv;
pub use ops::pow::Pow;
pub use ops::checked::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv, CheckedShl, CheckedShr}; pub use ops::checked::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv, CheckedShl, CheckedShr};
pub use ops::wrapping::{WrappingAdd, WrappingMul, WrappingSub}; pub use ops::wrapping::{WrappingAdd, WrappingMul, WrappingSub};
pub use ops::saturating::Saturating; pub use ops::saturating::Saturating;
pub use sign::{Signed, Unsigned, abs, abs_sub, signum}; pub use sign::{Signed, Unsigned, abs, abs_sub, signum};
pub use cast::{AsPrimitive, FromPrimitive, ToPrimitive, NumCast, cast}; pub use cast::{AsPrimitive, FromPrimitive, ToPrimitive, NumCast, cast};
pub use int::PrimInt; pub use int::PrimInt;
pub use pow::{pow, checked_pow}; pub use pow::{Pow, pow, checked_pow};
pub mod identities; pub mod identities;
pub mod sign; pub mod sign;

View File

@ -2,4 +2,3 @@ pub mod saturating;
pub mod checked; pub mod checked;
pub mod wrapping; pub mod wrapping;
pub mod inv; pub mod inv;
pub mod pow;

View File

@ -1,71 +0,0 @@
/// Binary operator for raising a value to a power.
pub trait Pow<RHS> {
/// The result after applying the operator.
type Output;
/// Returns `self` to the power `rhs`.
///
/// # Examples
///
/// ```
/// use num_traits::Pow;
/// assert_eq!(10.pow(2), 100);
/// ```
fn pow(self, rhs: RHS) -> Self::Output;
}
macro_rules! pow_impl {
($t:ty, $rhs:ty, $method:ident) => {
impl Pow<$rhs> for $t {
type Output = $t;
#[inline]
fn pow(self, rhs: $rhs) -> $t {
<$t>::$method(self, rhs)
}
}
impl<'a> Pow<&'a $rhs> for $t {
type Output = $t;
#[inline]
fn pow(self, rhs: &'a $rhs) -> $t {
<$t>::$method(self, *rhs)
}
}
impl<'a> Pow<$rhs> for &'a $t {
type Output = $t;
#[inline]
fn pow(self, rhs: $rhs) -> $t {
<$t>::$method(*self, rhs)
}
}
impl<'a, 'b> Pow<&'a $rhs> for &'b $t {
type Output = $t;
#[inline]
fn pow(self, rhs: &'a $rhs) -> $t {
<$t>::$method(*self, *rhs)
}
}
}
}
pow_impl!(u8, u32, pow);
pow_impl!(i8, u32, pow);
pow_impl!(u16, u32, pow);
pow_impl!(i16, u32, pow);
pow_impl!(u32, u32, pow);
pow_impl!(i32, u32, pow);
pow_impl!(u64, u32, pow);
pow_impl!(i64, u32, pow);
pow_impl!(usize, u32, pow);
pow_impl!(isize, u32, pow);
#[cfg(feature = "std")]
mod float_impls {
pow_impl!(f32, i32, powi);
pow_impl!(f64, i32, powi);
pow_impl!(f32, f32, powf);
pow_impl!(f64, f64, powf);
}

View File

@ -1,6 +1,81 @@
use core::ops::Mul; use core::ops::Mul;
use {One, CheckedMul}; use {One, CheckedMul};
/// Binary operator for raising a value to a power.
pub trait Pow<RHS> {
/// The result after applying the operator.
type Output;
/// Returns `self` to the power `rhs`.
///
/// # Examples
///
/// ```
/// use num_traits::Pow;
/// assert_eq!(10.pow(2), 100);
/// ```
fn pow(self, rhs: RHS) -> Self::Output;
}
macro_rules! pow_impl {
($t:ty, $rhs:ty, $method:ident) => {
impl Pow<$rhs> for $t {
type Output = $t;
#[inline]
fn pow(self, rhs: $rhs) -> $t {
<$t>::$method(self, rhs)
}
}
impl<'a> Pow<&'a $rhs> for $t {
type Output = $t;
#[inline]
fn pow(self, rhs: &'a $rhs) -> $t {
<$t>::$method(self, *rhs)
}
}
impl<'a> Pow<$rhs> for &'a $t {
type Output = $t;
#[inline]
fn pow(self, rhs: $rhs) -> $t {
<$t>::$method(*self, rhs)
}
}
impl<'a, 'b> Pow<&'a $rhs> for &'b $t {
type Output = $t;
#[inline]
fn pow(self, rhs: &'a $rhs) -> $t {
<$t>::$method(*self, *rhs)
}
}
}
}
pow_impl!(u8, u32, pow);
pow_impl!(i8, u32, pow);
pow_impl!(u16, u32, pow);
pow_impl!(i16, u32, pow);
pow_impl!(u32, u32, pow);
pow_impl!(i32, u32, pow);
pow_impl!(u64, u32, pow);
pow_impl!(i64, u32, pow);
pow_impl!(usize, u32, pow);
pow_impl!(isize, u32, pow);
#[cfg(feature = "std")]
mod float_impls {
use super::Pow;
pow_impl!(f32, i32, powi);
pow_impl!(f64, i32, powi);
pow_impl!(f32, f32, powf);
pow_impl!(f64, f64, powf);
}
/// Raises a value to the power of exp, using exponentiation by squaring. /// Raises a value to the power of exp, using exponentiation by squaring.
/// ///
/// # Example /// # Example