From d2bf4e04e43add49c4ab6b7df794501b09d0a081 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 12 Jul 2018 17:09:22 -0700 Subject: [PATCH] Run cargo fmt --- src/bounds.rs | 49 ++++++------ src/cast.rs | 177 ++++++++++++++++++++++++++++---------------- src/float.rs | 44 +++++------ src/identities.rs | 85 ++++++++++++--------- src/int.rs | 59 ++++++++------- src/lib.rs | 123 ++++++++++++++++-------------- src/ops/checked.rs | 20 ++--- src/ops/inv.rs | 16 +++- src/ops/mod.rs | 4 +- src/ops/wrapping.rs | 38 +++++++--- src/pow.rs | 30 +++++--- src/real.rs | 10 +-- src/sign.rs | 43 ++++++++--- tests/cast.rs | 4 +- 14 files changed, 419 insertions(+), 283 deletions(-) diff --git a/src/bounds.rs b/src/bounds.rs index 744c5ba..a133d7a 100644 --- a/src/bounds.rs +++ b/src/bounds.rs @@ -1,9 +1,9 @@ -use core::{usize, u8, u16, u32, u64}; -use core::{isize, i8, i16, i32, i64}; -use core::{f32, f64}; use core::num::Wrapping; +use core::{f32, f64}; #[cfg(has_i128)] use core::{i128, u128}; +use core::{i16, i32, i64, i8, isize}; +use core::{u16, u32, u64, u8, usize}; /// Numbers which have upper and lower bounds pub trait Bounded { @@ -18,33 +18,41 @@ macro_rules! bounded_impl { ($t:ty, $min:expr, $max:expr) => { impl Bounded for $t { #[inline] - fn min_value() -> $t { $min } + fn min_value() -> $t { + $min + } #[inline] - fn max_value() -> $t { $max } + fn max_value() -> $t { + $max + } } - } + }; } bounded_impl!(usize, usize::MIN, usize::MAX); -bounded_impl!(u8, u8::MIN, u8::MAX); -bounded_impl!(u16, u16::MIN, u16::MAX); -bounded_impl!(u32, u32::MIN, u32::MAX); -bounded_impl!(u64, u64::MIN, u64::MAX); +bounded_impl!(u8, u8::MIN, u8::MAX); +bounded_impl!(u16, u16::MIN, u16::MAX); +bounded_impl!(u32, u32::MIN, u32::MAX); +bounded_impl!(u64, u64::MIN, u64::MAX); #[cfg(has_i128)] -bounded_impl!(u128, u128::MIN, u128::MAX); +bounded_impl!(u128, u128::MIN, u128::MAX); bounded_impl!(isize, isize::MIN, isize::MAX); -bounded_impl!(i8, i8::MIN, i8::MAX); -bounded_impl!(i16, i16::MIN, i16::MAX); -bounded_impl!(i32, i32::MIN, i32::MAX); -bounded_impl!(i64, i64::MIN, i64::MAX); +bounded_impl!(i8, i8::MIN, i8::MAX); +bounded_impl!(i16, i16::MIN, i16::MAX); +bounded_impl!(i32, i32::MIN, i32::MAX); +bounded_impl!(i64, i64::MIN, i64::MAX); #[cfg(has_i128)] -bounded_impl!(i128, i128::MIN, i128::MAX); +bounded_impl!(i128, i128::MIN, i128::MAX); impl Bounded for Wrapping { - fn min_value() -> Self { Wrapping(T::min_value()) } - fn max_value() -> Self { Wrapping(T::max_value()) } + fn min_value() -> Self { + Wrapping(T::min_value()) + } + fn max_value() -> Self { + Wrapping(T::max_value()) + } } bounded_impl!(f32, f32::MIN, f32::MAX); @@ -59,9 +67,9 @@ macro_rules! for_each_tuple_ { ); } macro_rules! for_each_tuple { - ( $m:ident ) => ( + ($m:ident) => { for_each_tuple_! { $m !! A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, } - ); + }; } macro_rules! bounded_tuple { @@ -82,7 +90,6 @@ macro_rules! bounded_tuple { for_each_tuple!(bounded_tuple); bounded_impl!(f64, f64::MIN, f64::MAX); - #[test] fn wrapping_bounded() { macro_rules! test_wrapping_bounded { diff --git a/src/cast.rs b/src/cast.rs index c692df8..20e9340 100644 --- a/src/cast.rs +++ b/src/cast.rs @@ -1,10 +1,10 @@ -use core::{i8, i16, i32, i64, isize}; -use core::{u8, u16, u32, u64, usize}; -use core::{f32, f64}; use core::mem::size_of; use core::num::Wrapping; +use core::{f32, f64}; #[cfg(has_i128)] use core::{i128, u128}; +use core::{i16, i32, i64, i8, isize}; +use core::{u16, u32, u64, u8, usize}; use float::FloatCore; @@ -137,7 +137,7 @@ macro_rules! impl_to_primitive_int_to_uint { } macro_rules! impl_to_primitive_int { - ($T:ident) => ( + ($T:ident) => { impl ToPrimitive for $T { impl_to_primitive_int_to_int! { $T: fn to_isize -> isize; @@ -160,11 +160,15 @@ macro_rules! impl_to_primitive_int { } #[inline] - fn to_f32(&self) -> Option { Some(*self as f32) } + fn to_f32(&self) -> Option { + Some(*self as f32) + } #[inline] - fn to_f64(&self) -> Option { Some(*self as f64) } + fn to_f64(&self) -> Option { + Some(*self as f64) + } } - ) + }; } impl_to_primitive_int!(isize); @@ -206,7 +210,7 @@ macro_rules! impl_to_primitive_uint_to_uint { } macro_rules! impl_to_primitive_uint { - ($T:ident) => ( + ($T:ident) => { impl ToPrimitive for $T { impl_to_primitive_uint_to_int! { $T: fn to_isize -> isize; @@ -229,11 +233,15 @@ macro_rules! impl_to_primitive_uint { } #[inline] - fn to_f32(&self) -> Option { Some(*self as f32) } + fn to_f32(&self) -> Option { + Some(*self as f32) + } #[inline] - fn to_f64(&self) -> Option { Some(*self as f64) } + fn to_f64(&self) -> Option { + Some(*self as f64) + } } - ) + }; } impl_to_primitive_uint!(usize); @@ -319,7 +327,7 @@ macro_rules! impl_to_primitive_float_to_unsigned_int { } macro_rules! impl_to_primitive_float { - ($T:ident) => ( + ($T:ident) => { impl ToPrimitive for $T { impl_to_primitive_float_to_signed_int! { $T: fn to_isize -> isize; @@ -346,7 +354,7 @@ macro_rules! impl_to_primitive_float { fn to_f64 -> f64; } } - ) + }; } impl_to_primitive_float!(f32); @@ -463,48 +471,89 @@ pub trait FromPrimitive: Sized { } macro_rules! impl_from_primitive { - ($T:ty, $to_ty:ident) => ( + ($T:ty, $to_ty:ident) => { #[allow(deprecated)] impl FromPrimitive for $T { - #[inline] fn from_isize(n: isize) -> Option<$T> { n.$to_ty() } - #[inline] fn from_i8(n: i8) -> Option<$T> { n.$to_ty() } - #[inline] fn from_i16(n: i16) -> Option<$T> { n.$to_ty() } - #[inline] fn from_i32(n: i32) -> Option<$T> { n.$to_ty() } - #[inline] fn from_i64(n: i64) -> Option<$T> { n.$to_ty() } + #[inline] + fn from_isize(n: isize) -> Option<$T> { + n.$to_ty() + } + #[inline] + fn from_i8(n: i8) -> Option<$T> { + n.$to_ty() + } + #[inline] + fn from_i16(n: i16) -> Option<$T> { + n.$to_ty() + } + #[inline] + fn from_i32(n: i32) -> Option<$T> { + n.$to_ty() + } + #[inline] + fn from_i64(n: i64) -> Option<$T> { + n.$to_ty() + } #[cfg(has_i128)] - #[inline] fn from_i128(n: i128) -> Option<$T> { n.$to_ty() } + #[inline] + fn from_i128(n: i128) -> Option<$T> { + n.$to_ty() + } - #[inline] fn from_usize(n: usize) -> Option<$T> { n.$to_ty() } - #[inline] fn from_u8(n: u8) -> Option<$T> { n.$to_ty() } - #[inline] fn from_u16(n: u16) -> Option<$T> { n.$to_ty() } - #[inline] fn from_u32(n: u32) -> Option<$T> { n.$to_ty() } - #[inline] fn from_u64(n: u64) -> Option<$T> { n.$to_ty() } + #[inline] + fn from_usize(n: usize) -> Option<$T> { + n.$to_ty() + } + #[inline] + fn from_u8(n: u8) -> Option<$T> { + n.$to_ty() + } + #[inline] + fn from_u16(n: u16) -> Option<$T> { + n.$to_ty() + } + #[inline] + fn from_u32(n: u32) -> Option<$T> { + n.$to_ty() + } + #[inline] + fn from_u64(n: u64) -> Option<$T> { + n.$to_ty() + } #[cfg(has_i128)] - #[inline] fn from_u128(n: u128) -> Option<$T> { n.$to_ty() } + #[inline] + fn from_u128(n: u128) -> Option<$T> { + n.$to_ty() + } - #[inline] fn from_f32(n: f32) -> Option<$T> { n.$to_ty() } - #[inline] fn from_f64(n: f64) -> Option<$T> { n.$to_ty() } + #[inline] + fn from_f32(n: f32) -> Option<$T> { + n.$to_ty() + } + #[inline] + fn from_f64(n: f64) -> Option<$T> { + n.$to_ty() + } } - ) + }; } impl_from_primitive!(isize, to_isize); -impl_from_primitive!(i8, to_i8); -impl_from_primitive!(i16, to_i16); -impl_from_primitive!(i32, to_i32); -impl_from_primitive!(i64, to_i64); +impl_from_primitive!(i8, to_i8); +impl_from_primitive!(i16, to_i16); +impl_from_primitive!(i32, to_i32); +impl_from_primitive!(i64, to_i64); #[cfg(has_i128)] -impl_from_primitive!(i128, to_i128); +impl_from_primitive!(i128, to_i128); impl_from_primitive!(usize, to_usize); -impl_from_primitive!(u8, to_u8); -impl_from_primitive!(u16, to_u16); -impl_from_primitive!(u32, to_u32); -impl_from_primitive!(u64, to_u64); +impl_from_primitive!(u8, to_u8); +impl_from_primitive!(u16, to_u16); +impl_from_primitive!(u32, to_u32); +impl_from_primitive!(u64, to_u64); #[cfg(has_i128)] -impl_from_primitive!(u128, to_u128); -impl_from_primitive!(f32, to_f32); -impl_from_primitive!(f64, to_f64); - +impl_from_primitive!(u128, to_u128); +impl_from_primitive!(f32, to_f32); +impl_from_primitive!(f64, to_f64); macro_rules! impl_to_primitive_wrapping { ($( $(#[$cfg:meta])* fn $method:ident -> $i:ident ; )*) => {$( @@ -572,7 +621,6 @@ impl FromPrimitive for Wrapping { } } - /// Cast from one machine scalar to another. /// /// # Examples @@ -596,7 +644,7 @@ pub trait NumCast: Sized + ToPrimitive { } macro_rules! impl_num_cast { - ($T:ty, $conv:ident) => ( + ($T:ty, $conv:ident) => { impl NumCast for $T { #[inline] #[allow(deprecated)] @@ -606,25 +654,25 @@ macro_rules! impl_num_cast { n.$conv() } } - ) + }; } -impl_num_cast!(u8, to_u8); -impl_num_cast!(u16, to_u16); -impl_num_cast!(u32, to_u32); -impl_num_cast!(u64, to_u64); +impl_num_cast!(u8, to_u8); +impl_num_cast!(u16, to_u16); +impl_num_cast!(u32, to_u32); +impl_num_cast!(u64, to_u64); #[cfg(has_i128)] -impl_num_cast!(u128, to_u128); +impl_num_cast!(u128, to_u128); impl_num_cast!(usize, to_usize); -impl_num_cast!(i8, to_i8); -impl_num_cast!(i16, to_i16); -impl_num_cast!(i32, to_i32); -impl_num_cast!(i64, to_i64); +impl_num_cast!(i8, to_i8); +impl_num_cast!(i16, to_i16); +impl_num_cast!(i32, to_i32); +impl_num_cast!(i64, to_i64); #[cfg(has_i128)] -impl_num_cast!(i128, to_i128); +impl_num_cast!(i128, to_i128); impl_num_cast!(isize, to_isize); -impl_num_cast!(f32, to_f32); -impl_num_cast!(f64, to_f64); +impl_num_cast!(f32, to_f32); +impl_num_cast!(f64, to_f64); impl NumCast for Wrapping { fn from(n: U) -> Option { @@ -645,20 +693,20 @@ impl NumCast for Wrapping { /// let three: i32 = (3.14159265f32).as_(); /// assert_eq!(three, 3); /// ``` -/// +/// /// # Safety -/// +/// /// Currently, some uses of the `as` operator are not entirely safe. /// In particular, it is undefined behavior if: -/// +/// /// - A truncated floating point value cannot fit in the target integer /// type ([#10184](https://github.com/rust-lang/rust/issues/10184)); -/// +/// /// ```ignore /// # use num_traits::AsPrimitive; /// let x: u8 = (1.04E+17).as_(); // UB /// ``` -/// +/// /// - Or a floating point value does not fit in another floating /// point type ([#15536](https://github.com/rust-lang/rust/issues/15536)). /// @@ -666,10 +714,10 @@ impl NumCast for Wrapping { /// # use num_traits::AsPrimitive; /// let x: f32 = (1e300f64).as_(); // UB /// ``` -/// +/// pub trait AsPrimitive: 'static + Copy where - T: 'static + Copy + T: 'static + Copy, { /// Convert a value to another, using the `as` operator. fn as_(self) -> T; @@ -712,4 +760,3 @@ impl_as_primitive!(f32 => { f32, f64 }); impl_as_primitive!(f64 => { f32, f64 }); impl_as_primitive!(char => { char }); impl_as_primitive!(bool => {}); - diff --git a/src/float.rs b/src/float.rs index 4320543..c91233c 100644 --- a/src/float.rs +++ b/src/float.rs @@ -1,6 +1,6 @@ use core::mem; -use core::ops::Neg; use core::num::FpCategory; +use core::ops::Neg; use core::f32; use core::f64; @@ -586,7 +586,11 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { if other.is_nan() { return self; } - if self < other { self } else { other } + if self < other { + self + } else { + other + } } /// Returns the maximum of the two numbers. @@ -616,7 +620,11 @@ pub trait FloatCore: Num + NumCast + Neg + PartialOrd + Copy { if other.is_nan() { return self; } - if self > other { self } else { other } + if self > other { + self + } else { + other + } } /// Returns the reciprocal (multiplicative inverse) of the number. @@ -887,13 +895,7 @@ impl FloatCore for f64 { /// /// This trait is only available with the `std` feature. #[cfg(feature = "std")] -pub trait Float - : Num - + Copy - + NumCast - + PartialOrd - + Neg -{ +pub trait Float: Num + Copy + NumCast + PartialOrd + Neg { /// Returns the `NaN` value. /// /// ``` @@ -1777,7 +1779,6 @@ pub trait Float /// ``` fn atanh(self) -> Self; - /// Returns the mantissa, base 2 exponent, and sign as integers, respectively. /// The original number can be recovered by `sign * mantissa * 2 ^ exponent`. /// @@ -1802,7 +1803,7 @@ pub trait Float #[cfg(feature = "std")] macro_rules! float_impl { - ($T:ident $decode:ident) => ( + ($T:ident $decode:ident) => { impl Float for $T { constant! { nan() -> $T::NAN; @@ -1876,16 +1877,12 @@ macro_rules! float_impl { Self::atanh(self) -> Self; } } - ) + }; } fn integer_decode_f32(f: f32) -> (u64, i16, i8) { let bits: u32 = unsafe { mem::transmute(f) }; - let sign: i8 = if bits >> 31 == 0 { - 1 - } else { - -1 - }; + let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 }; let mut exponent: i16 = ((bits >> 23) & 0xff) as i16; let mantissa = if exponent == 0 { (bits & 0x7fffff) << 1 @@ -1899,11 +1896,7 @@ fn integer_decode_f32(f: f32) -> (u64, i16, i8) { fn integer_decode_f64(f: f64) -> (u64, i16, i8) { let bits: u64 = unsafe { mem::transmute(f) }; - let sign: i8 = if bits >> 63 == 0 { - 1 - } else { - -1 - }; + let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 }; let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16; let mantissa = if exponent == 0 { (bits & 0xfffffffffffff) << 1 @@ -2023,6 +2016,9 @@ mod tests { fn to_degrees_rounding() { use float::FloatCore; - assert_eq!(FloatCore::to_degrees(1_f32), 57.2957795130823208767981548141051703); + assert_eq!( + FloatCore::to_degrees(1_f32), + 57.2957795130823208767981548141051703 + ); } } diff --git a/src/identities.rs b/src/identities.rs index 8ef9ff1..12b9fca 100644 --- a/src/identities.rs +++ b/src/identities.rs @@ -1,5 +1,5 @@ -use core::ops::{Add, Mul}; use core::num::Wrapping; +use core::ops::{Add, Mul}; /// Defines an additive identity element for `Self`. pub trait Zero: Sized + Add { @@ -29,33 +29,40 @@ macro_rules! zero_impl { ($t:ty, $v:expr) => { impl Zero for $t { #[inline] - fn zero() -> $t { $v } + fn zero() -> $t { + $v + } #[inline] - fn is_zero(&self) -> bool { *self == $v } + fn is_zero(&self) -> bool { + *self == $v + } } - } + }; } zero_impl!(usize, 0); -zero_impl!(u8, 0); -zero_impl!(u16, 0); -zero_impl!(u32, 0); -zero_impl!(u64, 0); +zero_impl!(u8, 0); +zero_impl!(u16, 0); +zero_impl!(u32, 0); +zero_impl!(u64, 0); #[cfg(has_i128)] -zero_impl!(u128, 0); +zero_impl!(u128, 0); zero_impl!(isize, 0); -zero_impl!(i8, 0); -zero_impl!(i16, 0); -zero_impl!(i32, 0); -zero_impl!(i64, 0); +zero_impl!(i8, 0); +zero_impl!(i16, 0); +zero_impl!(i32, 0); +zero_impl!(i64, 0); #[cfg(has_i128)] -zero_impl!(i128, 0); +zero_impl!(i128, 0); zero_impl!(f32, 0.0); zero_impl!(f64, 0.0); -impl Zero for Wrapping where Wrapping: Add> { +impl Zero for Wrapping +where + Wrapping: Add>, +{ fn is_zero(&self) -> bool { self.0.is_zero() } @@ -64,7 +71,6 @@ impl Zero for Wrapping where Wrapping: Add> { } } - /// Defines a multiplicative identity element for `Self`. pub trait One: Sized + Mul { /// Returns the multiplicative identity element of `Self`, `1`. @@ -90,7 +96,10 @@ pub trait One: Sized + Mul { /// After a semver bump, this method will be required, and the /// `where Self: PartialEq` bound will be removed. #[inline] - fn is_one(&self) -> bool where Self: PartialEq { + fn is_one(&self) -> bool + where + Self: PartialEq, + { *self == Self::one() } } @@ -99,31 +108,36 @@ macro_rules! one_impl { ($t:ty, $v:expr) => { impl One for $t { #[inline] - fn one() -> $t { $v } + fn one() -> $t { + $v + } } - } + }; } one_impl!(usize, 1); -one_impl!(u8, 1); -one_impl!(u16, 1); -one_impl!(u32, 1); -one_impl!(u64, 1); +one_impl!(u8, 1); +one_impl!(u16, 1); +one_impl!(u32, 1); +one_impl!(u64, 1); #[cfg(has_i128)] -one_impl!(u128, 1); +one_impl!(u128, 1); one_impl!(isize, 1); -one_impl!(i8, 1); -one_impl!(i16, 1); -one_impl!(i32, 1); -one_impl!(i64, 1); +one_impl!(i8, 1); +one_impl!(i16, 1); +one_impl!(i32, 1); +one_impl!(i64, 1); #[cfg(has_i128)] -one_impl!(i128, 1); +one_impl!(i128, 1); one_impl!(f32, 1.0); one_impl!(f64, 1.0); -impl One for Wrapping where Wrapping: Mul> { +impl One for Wrapping +where + Wrapping: Mul>, +{ fn one() -> Self { Wrapping(T::one()) } @@ -132,11 +146,16 @@ impl One for Wrapping where Wrapping: Mul> { // Some helper functions provided for backwards compatibility. /// Returns the additive identity, `0`. -#[inline(always)] pub fn zero() -> T { Zero::zero() } +#[inline(always)] +pub fn zero() -> T { + Zero::zero() +} /// Returns the multiplicative identity, `1`. -#[inline(always)] pub fn one() -> T { One::one() } - +#[inline(always)] +pub fn one() -> T { + One::one() +} #[test] fn wrapping_identities() { diff --git a/src/int.rs b/src/int.rs index 1e8275f..22f2713 100644 --- a/src/int.rs +++ b/src/int.rs @@ -1,26 +1,29 @@ -use core::ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr}; +use core::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr}; -use {Num, NumCast}; use bounds::Bounded; use ops::checked::*; use ops::saturating::Saturating; +use {Num, NumCast}; -pub trait PrimInt - : Sized +pub trait PrimInt: + Sized + Copy - + Num + NumCast + + Num + + NumCast + Bounded - + PartialOrd + Ord + Eq - + Not - + BitAnd - + BitOr - + BitXor - + Shl - + Shr - + CheckedAdd - + CheckedSub - + CheckedMul - + CheckedDiv + + PartialOrd + + Ord + + Eq + + Not + + BitAnd + + BitOr + + BitXor + + Shl + + Shr + + CheckedAdd + + CheckedSub + + CheckedMul + + CheckedDiv + Saturating { /// Returns the number of ones in the binary representation of `self`. @@ -278,7 +281,7 @@ pub trait PrimInt } macro_rules! prim_int_impl { - ($T:ty, $S:ty, $U:ty) => ( + ($T:ty, $S:ty, $U:ty) => { impl PrimInt for $T { #[inline] fn count_ones(self) -> u32 { @@ -360,21 +363,21 @@ macro_rules! prim_int_impl { <$T>::pow(self, exp) } } - ) + }; } // prim_int_impl!(type, signed, unsigned); -prim_int_impl!(u8, i8, u8); -prim_int_impl!(u16, i16, u16); -prim_int_impl!(u32, i32, u32); -prim_int_impl!(u64, i64, u64); +prim_int_impl!(u8, i8, u8); +prim_int_impl!(u16, i16, u16); +prim_int_impl!(u32, i32, u32); +prim_int_impl!(u64, i64, u64); #[cfg(has_i128)] -prim_int_impl!(u128, i128, u128); +prim_int_impl!(u128, i128, u128); prim_int_impl!(usize, isize, usize); -prim_int_impl!(i8, i8, u8); -prim_int_impl!(i16, i16, u16); -prim_int_impl!(i32, i32, u32); -prim_int_impl!(i64, i64, u64); +prim_int_impl!(i8, i8, u8); +prim_int_impl!(i16, i16, u16); +prim_int_impl!(i32, i32, u32); +prim_int_impl!(i64, i64, u64); #[cfg(has_i128)] -prim_int_impl!(i128, i128, u128); +prim_int_impl!(i128, i128, u128); prim_int_impl!(isize, isize, usize); diff --git a/src/lib.rs b/src/lib.rs index 53d55a5..c1c7327 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,53 +15,51 @@ //! The `num-traits` crate is tested for rustc 1.8 and greater. #![doc(html_root_url = "https://docs.rs/num-traits/0.2")] - #![deny(unconditional_recursion)] - #![no_std] #[cfg(feature = "std")] extern crate std; -use core::ops::{Add, Sub, Mul, Div, Rem}; -use core::ops::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign}; -use core::num::Wrapping; use core::fmt; +use core::num::Wrapping; +use core::ops::{Add, Div, Mul, Rem, Sub}; +use core::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign}; pub use bounds::Bounded; #[cfg(feature = "std")] 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 cast::{cast, AsPrimitive, FromPrimitive, NumCast, ToPrimitive}; +pub use identities::{one, zero, One, Zero}; +pub use int::PrimInt; +pub use ops::checked::{ + CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr, CheckedSub, +}; pub use ops::inv::Inv; -pub use ops::checked::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv, - CheckedRem, CheckedNeg, CheckedShl, CheckedShr}; -pub use ops::wrapping::{WrappingAdd, WrappingMul, WrappingSub}; pub use ops::mul_add::{MulAdd, MulAddAssign}; pub use ops::saturating::Saturating; -pub use sign::{Signed, Unsigned, abs, abs_sub, signum}; -pub use cast::{AsPrimitive, FromPrimitive, ToPrimitive, NumCast, cast}; -pub use int::PrimInt; -pub use pow::{Pow, pow, checked_pow}; +pub use ops::wrapping::{WrappingAdd, WrappingMul, WrappingSub}; +pub use pow::{checked_pow, pow, Pow}; +pub use sign::{abs, abs_sub, signum, Signed, Unsigned}; #[macro_use] mod macros; -pub mod identities; -pub mod sign; -pub mod ops; pub mod bounds; +pub mod cast; pub mod float; +pub mod identities; +pub mod int; +pub mod ops; +pub mod pow; #[cfg(feature = "std")] pub mod real; -pub mod cast; -pub mod int; -pub mod pow; +pub mod sign; /// The base trait for numeric types, covering `0` and `1` values, /// comparisons, basic numeric operations, and string conversion. -pub trait Num: PartialEq + Zero + One + NumOps -{ +pub trait Num: PartialEq + Zero + One + NumOps { type FromStrRadixErr; /// Convert from a string and radix <= 36. @@ -83,68 +81,81 @@ pub trait Num: PartialEq + Zero + One + NumOps /// The trait for types implementing basic numeric operations /// /// This is automatically implemented for types which implement the operators. -pub trait NumOps - : Add +pub trait NumOps: + Add + Sub + Mul + Div + Rem -{} +{ +} impl NumOps for T -where T: Add - + Sub - + Mul - + Div - + Rem -{} +where + T: Add + + Sub + + Mul + + Div + + Rem, +{ +} /// The trait for `Num` types which also implement numeric operations taking /// the second operand by reference. /// /// This is automatically implemented for types which implement the operators. pub trait NumRef: Num + for<'r> NumOps<&'r Self> {} -impl NumRef for T where T: Num + for<'r> NumOps<&'r T> {} +impl NumRef for T +where + T: Num + for<'r> NumOps<&'r T>, +{ +} /// The trait for references which implement numeric operations, taking the /// second operand either by value or by reference. /// /// This is automatically implemented for types which implement the operators. pub trait RefNum: NumOps + for<'r> NumOps<&'r Base, Base> {} -impl RefNum for T where T: NumOps + for<'r> NumOps<&'r Base, Base> {} +impl RefNum for T +where + T: NumOps + for<'r> NumOps<&'r Base, Base>, +{ +} /// The trait for types implementing numeric assignment operators (like `+=`). /// /// This is automatically implemented for types which implement the operators. -pub trait NumAssignOps - : AddAssign - + SubAssign - + MulAssign - + DivAssign - + RemAssign -{} +pub trait NumAssignOps: + AddAssign + SubAssign + MulAssign + DivAssign + RemAssign +{ +} impl NumAssignOps for T -where T: AddAssign - + SubAssign - + MulAssign - + DivAssign - + RemAssign -{} +where + T: AddAssign + SubAssign + MulAssign + DivAssign + RemAssign, +{ +} /// The trait for `Num` types which also implement assignment operators. /// /// This is automatically implemented for types which implement the operators. pub trait NumAssign: Num + NumAssignOps {} -impl NumAssign for T where T: Num + NumAssignOps {} +impl NumAssign for T +where + T: Num + NumAssignOps, +{ +} /// The trait for `NumAssign` types which also implement assignment operations /// taking the second operand by reference. /// /// This is automatically implemented for types which implement the operators. pub trait NumAssignRef: NumAssign + for<'r> NumAssignOps<&'r Self> {} -impl NumAssignRef for T where T: NumAssign + for<'r> NumAssignOps<&'r T> {} - +impl NumAssignRef for T +where + T: NumAssign + for<'r> NumAssignOps<&'r T>, +{ +} macro_rules! int_trait_impl { ($name:ident for $($t:ty)*) => ($( @@ -164,9 +175,12 @@ int_trait_impl!(Num for usize u8 u16 u32 u64 isize i8 i16 i32 i64); int_trait_impl!(Num for u128 i128); impl Num for Wrapping - where Wrapping: - Add> + Sub> - + Mul> + Div> + Rem> +where + Wrapping: Add> + + Sub> + + Mul> + + Div> + + Rem>, { type FromStrRadixErr = T::FromStrRadixErr; fn from_str_radix(str: &str, radix: u32) -> Result { @@ -174,7 +188,6 @@ impl Num for Wrapping } } - #[derive(Debug)] pub enum FloatErrorKind { Empty, @@ -442,7 +455,8 @@ fn check_numref_ops() { #[test] fn check_refnum_ops() { fn compute(x: &T, y: T) -> T - where for<'a> &'a T: RefNum + where + for<'a> &'a T: RefNum, { &(&(&(&(x * y) / y) % y) + y) - y } @@ -452,7 +466,8 @@ fn check_refnum_ops() { #[test] fn check_refref_ops() { fn compute(x: &T, y: &T) -> T - where for<'a> &'a T: RefNum + where + for<'a> &'a T: RefNum, { &(&(&(&(x * y) / y) % y) + y) - y } diff --git a/src/ops/checked.rs b/src/ops/checked.rs index 1c2b2f5..b386e97 100644 --- a/src/ops/checked.rs +++ b/src/ops/checked.rs @@ -1,8 +1,8 @@ -use core::ops::{Add, Sub, Mul, Div, Rem, Shl, Shr}; +use core::ops::{Add, Div, Mul, Rem, Shl, Shr, Sub}; /// Performs addition that returns `None` instead of wrapping around on /// overflow. -pub trait CheckedAdd: Sized + Add { +pub trait CheckedAdd: Sized + Add { /// Adds two numbers, checking for overflow. If overflow happens, `None` is /// returned. fn checked_add(&self, v: &Self) -> Option; @@ -16,7 +16,7 @@ macro_rules! checked_impl { <$t>::$method(*self, *v) } } - } + }; } checked_impl!(CheckedAdd, checked_add, u8); @@ -36,7 +36,7 @@ checked_impl!(CheckedAdd, checked_add, isize); checked_impl!(CheckedAdd, checked_add, i128); /// Performs subtraction that returns `None` instead of wrapping around on underflow. -pub trait CheckedSub: Sized + Sub { +pub trait CheckedSub: Sized + Sub { /// Subtracts two numbers, checking for underflow. If underflow happens, /// `None` is returned. fn checked_sub(&self, v: &Self) -> Option; @@ -60,7 +60,7 @@ checked_impl!(CheckedSub, checked_sub, i128); /// Performs multiplication that returns `None` instead of wrapping around on underflow or /// overflow. -pub trait CheckedMul: Sized + Mul { +pub trait CheckedMul: Sized + Mul { /// Multiplies two numbers, checking for underflow or overflow. If underflow /// or overflow happens, `None` is returned. fn checked_mul(&self, v: &Self) -> Option; @@ -84,7 +84,7 @@ checked_impl!(CheckedMul, checked_mul, i128); /// Performs division that returns `None` instead of panicking on division by zero and instead of /// wrapping around on underflow and overflow. -pub trait CheckedDiv: Sized + Div { +pub trait CheckedDiv: Sized + Div { /// Divides two numbers, checking for underflow, overflow and division by /// zero. If any of that happens, `None` is returned. fn checked_div(&self, v: &Self) -> Option; @@ -155,7 +155,7 @@ macro_rules! checked_impl_unary { <$t>::$method(*self) } } - } + }; } /// Performs negation that returns `None` if the result can't be represented. @@ -196,7 +196,7 @@ checked_impl_unary!(CheckedNeg, checked_neg, isize); checked_impl_unary!(CheckedNeg, checked_neg, i128); /// Performs a left shift that returns `None` on overflow. -pub trait CheckedShl: Sized + Shl { +pub trait CheckedShl: Sized + Shl { /// Shifts a number to the left, checking for overflow. If overflow happens, /// `None` is returned. /// @@ -221,7 +221,7 @@ macro_rules! checked_shift_impl { <$t>::$method(*self, rhs) } } - } + }; } checked_shift_impl!(CheckedShl, checked_shl, u8); @@ -241,7 +241,7 @@ checked_shift_impl!(CheckedShl, checked_shl, isize); checked_shift_impl!(CheckedShl, checked_shl, i128); /// Performs a right shift that returns `None` on overflow. -pub trait CheckedShr: Sized + Shr { +pub trait CheckedShr: Sized + Shr { /// Shifts a number to the left, checking for overflow. If overflow happens, /// `None` is returned. /// diff --git a/src/ops/inv.rs b/src/ops/inv.rs index 38e89ea..7087d09 100644 --- a/src/ops/inv.rs +++ b/src/ops/inv.rs @@ -20,20 +20,28 @@ pub trait Inv { impl Inv for f32 { type Output = f32; #[inline] - fn inv(self) -> f32 { 1.0 / self } + fn inv(self) -> f32 { + 1.0 / self + } } impl Inv for f64 { type Output = f64; #[inline] - fn inv(self) -> f64 { 1.0 / self } + fn inv(self) -> f64 { + 1.0 / self + } } impl<'a> Inv for &'a f32 { type Output = f32; #[inline] - fn inv(self) -> f32 { 1.0 / *self } + fn inv(self) -> f32 { + 1.0 / *self + } } impl<'a> Inv for &'a f64 { type Output = f64; #[inline] - fn inv(self) -> f64 { 1.0 / *self } + fn inv(self) -> f64 { + 1.0 / *self + } } diff --git a/src/ops/mod.rs b/src/ops/mod.rs index 93b5195..fd1695d 100644 --- a/src/ops/mod.rs +++ b/src/ops/mod.rs @@ -1,5 +1,5 @@ -pub mod saturating; pub mod checked; -pub mod wrapping; pub mod inv; pub mod mul_add; +pub mod saturating; +pub mod wrapping; diff --git a/src/ops/wrapping.rs b/src/ops/wrapping.rs index b6bd136..bab70b2 100644 --- a/src/ops/wrapping.rs +++ b/src/ops/wrapping.rs @@ -1,5 +1,5 @@ -use core::ops::{Add, Sub, Mul}; use core::num::Wrapping; +use core::ops::{Add, Mul, Sub}; macro_rules! wrapping_impl { ($trait_name:ident, $method:ident, $t:ty) => { @@ -17,11 +17,11 @@ macro_rules! wrapping_impl { <$t>::$method(*self, *v) } } - } + }; } /// Performs addition that wraps around on overflow. -pub trait WrappingAdd: Sized + Add { +pub trait WrappingAdd: Sized + Add { /// Wrapping (modular) addition. Computes `self + other`, wrapping around at the boundary of /// the type. fn wrapping_add(&self, v: &Self) -> Self; @@ -44,7 +44,7 @@ wrapping_impl!(WrappingAdd, wrapping_add, isize); wrapping_impl!(WrappingAdd, wrapping_add, i128); /// Performs subtraction that wraps around on overflow. -pub trait WrappingSub: Sized + Sub { +pub trait WrappingSub: Sized + Sub { /// Wrapping (modular) subtraction. Computes `self - other`, wrapping around at the boundary /// of the type. fn wrapping_sub(&self, v: &Self) -> Self; @@ -67,7 +67,7 @@ wrapping_impl!(WrappingSub, wrapping_sub, isize); wrapping_impl!(WrappingSub, wrapping_sub, i128); /// Performs multiplication that wraps around on overflow. -pub trait WrappingMul: Sized + Mul { +pub trait WrappingMul: Sized + Mul { /// Wrapping (modular) multiplication. Computes `self * other`, wrapping around at the boundary /// of the type. fn wrapping_mul(&self, v: &Self) -> Self; @@ -90,28 +90,42 @@ wrapping_impl!(WrappingMul, wrapping_mul, isize); wrapping_impl!(WrappingMul, wrapping_mul, i128); // Well this is a bit funny, but all the more appropriate. -impl WrappingAdd for Wrapping where Wrapping: Add> { +impl WrappingAdd for Wrapping +where + Wrapping: Add>, +{ fn wrapping_add(&self, v: &Self) -> Self { Wrapping(self.0.wrapping_add(&v.0)) } } -impl WrappingSub for Wrapping where Wrapping: Sub> { +impl WrappingSub for Wrapping +where + Wrapping: Sub>, +{ fn wrapping_sub(&self, v: &Self) -> Self { Wrapping(self.0.wrapping_sub(&v.0)) } } -impl WrappingMul for Wrapping where Wrapping: Mul> { +impl WrappingMul for Wrapping +where + Wrapping: Mul>, +{ fn wrapping_mul(&self, v: &Self) -> Self { Wrapping(self.0.wrapping_mul(&v.0)) } } - #[test] fn test_wrapping_traits() { - fn wrapping_add(a: T, b: T) -> T { a.wrapping_add(&b) } - fn wrapping_sub(a: T, b: T) -> T { a.wrapping_sub(&b) } - fn wrapping_mul(a: T, b: T) -> T { a.wrapping_mul(&b) } + fn wrapping_add(a: T, b: T) -> T { + a.wrapping_add(&b) + } + fn wrapping_sub(a: T, b: T) -> T { + a.wrapping_sub(&b) + } + fn wrapping_mul(a: T, b: T) -> T { + a.wrapping_mul(&b) + } assert_eq!(wrapping_add(255, 1), 0u8); assert_eq!(wrapping_sub(0, 1), 255u8); assert_eq!(wrapping_mul(255, 2), 254u8); diff --git a/src/pow.rs b/src/pow.rs index 2c0723d..7ef8d3f 100644 --- a/src/pow.rs +++ b/src/pow.rs @@ -1,6 +1,6 @@ -use core::ops::Mul; use core::num::Wrapping; -use {One, CheckedMul}; +use core::ops::Mul; +use {CheckedMul, One}; /// Binary operator for raising a value to a power. pub trait Pow { @@ -183,13 +183,17 @@ mod float_impls { /// ``` #[inline] pub fn pow>(mut base: T, mut exp: usize) -> T { - if exp == 0 { return T::one() } + if exp == 0 { + return T::one(); + } while exp & 1 == 0 { base = base.clone() * base; exp >>= 1; } - if exp == 1 { return base } + if exp == 1 { + return base; + } let mut acc = base.clone(); while exp > 1 { @@ -217,19 +221,27 @@ pub fn pow>(mut base: T, mut exp: usize) -> /// ``` #[inline] pub fn checked_pow(mut base: T, mut exp: usize) -> Option { - if exp == 0 { return Some(T::one()) } + if exp == 0 { + return Some(T::one()); + } macro_rules! optry { - ( $ expr : expr ) => { - if let Some(val) = $expr { val } else { return None } - } + ($expr:expr) => { + if let Some(val) = $expr { + val + } else { + return None; + } + }; } while exp & 1 == 0 { base = optry!(base.checked_mul(&base)); exp >>= 1; } - if exp == 1 { return Some(base) } + if exp == 1 { + return Some(base); + } let mut acc = base.clone(); while exp > 1 { diff --git a/src/real.rs b/src/real.rs index 8c713d3..23ac6b8 100644 --- a/src/real.rs +++ b/src/real.rs @@ -1,6 +1,6 @@ use std::ops::Neg; -use {Num, NumCast, Float}; +use {Float, Num, NumCast}; // NOTE: These doctests have the same issue as those in src/float.rs. // They're testing the inherent methods directly, and not those of `Real`. @@ -12,13 +12,7 @@ use {Num, NumCast, Float}; /// for a list of data types that could meaningfully implement this trait. /// /// This trait is only available with the `std` feature. -pub trait Real - : Num - + Copy - + NumCast - + PartialOrd - + Neg -{ +pub trait Real: Num + Copy + NumCast + PartialOrd + Neg { /// Returns the smallest finite value that this type can represent. /// /// ``` diff --git a/src/sign.rs b/src/sign.rs index 779d058..cb1d9fb 100644 --- a/src/sign.rs +++ b/src/sign.rs @@ -1,8 +1,8 @@ -use core::ops::Neg; use core::num::Wrapping; +use core::ops::Neg; -use Num; use float::FloatCore; +use Num; /// Useful functions for signed numbers (i.e. numbers that can be negative). pub trait Signed: Sized + Num + Neg { @@ -77,7 +77,9 @@ signed_impl!(isize i8 i16 i32 i64); #[cfg(has_i128)] signed_impl!(i128); -impl Signed for Wrapping where Wrapping: Num + Neg> +impl Signed for Wrapping +where + Wrapping: Num + Neg>, { #[inline] fn abs(&self) -> Self { @@ -95,10 +97,14 @@ impl Signed for Wrapping where Wrapping: Num + Neg bool { self.0.is_positive() } + fn is_positive(&self) -> bool { + self.0.is_positive() + } #[inline] - fn is_negative(&self) -> bool { self.0.is_negative() } + fn is_negative(&self) -> bool { + self.0.is_negative() + } } macro_rules! signed_float_impl { @@ -115,7 +121,11 @@ macro_rules! signed_float_impl { /// and `other` is returned. #[inline] fn abs_sub(&self, other: &$t) -> $t { - if *self <= *other { 0. } else { *self - *other } + if *self <= *other { + 0. + } else { + *self - *other + } } /// # Returns @@ -130,13 +140,17 @@ macro_rules! signed_float_impl { /// Returns `true` if the number is positive, including `+0.0` and `INFINITY` #[inline] - fn is_positive(&self) -> bool { FloatCore::is_sign_positive(*self) } + fn is_positive(&self) -> bool { + FloatCore::is_sign_positive(*self) + } /// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY` #[inline] - fn is_negative(&self) -> bool { FloatCore::is_sign_negative(*self) } + fn is_negative(&self) -> bool { + FloatCore::is_sign_negative(*self) + } } - } + }; } signed_float_impl!(f32); @@ -174,7 +188,10 @@ pub fn abs_sub(x: T, y: T) -> T { /// * `0` if the number is zero /// * `1` if the number is positive /// * `-1` if the number is negative -#[inline(always)] pub fn signum(value: T) -> T { value.signum() } +#[inline(always)] +pub fn signum(value: T) -> T { + value.signum() +} /// A trait for values which cannot be negative pub trait Unsigned: Num {} @@ -189,7 +206,11 @@ empty_trait_impl!(Unsigned for usize u8 u16 u32 u64); #[cfg(has_i128)] empty_trait_impl!(Unsigned for u128); -impl Unsigned for Wrapping where Wrapping: Num {} +impl Unsigned for Wrapping +where + Wrapping: Num, +{ +} #[test] fn unsigned_wrapping_is_unsigned() { diff --git a/tests/cast.rs b/tests/cast.rs index 63643eb..b3f3108 100644 --- a/tests/cast.rs +++ b/tests/cast.rs @@ -11,11 +11,11 @@ extern crate num_traits; use num_traits::cast::*; use num_traits::Bounded; -use core::{i8, i16, i32, i64, isize}; -use core::{u8, u16, u32, u64, usize}; use core::{f32, f64}; #[cfg(has_i128)] use core::{i128, u128}; +use core::{i16, i32, i64, i8, isize}; +use core::{u16, u32, u64, u8, usize}; use core::fmt::Debug; use core::mem;