Take parameters by-value in Signed trait

This commit is contained in:
Brendan Zabarauskas 2014-11-09 16:48:49 +11:00
parent e4ead7b034
commit 8666812dce
4 changed files with 51 additions and 51 deletions

View File

@ -162,13 +162,13 @@ pub trait Signed: Num + Neg<Self> {
/// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`.
///
/// For signed integers, `::MIN` will be returned if the number is `::MIN`.
fn abs(&self) -> Self;
fn abs(self) -> Self;
/// The positive difference of two numbers.
///
/// Returns `zero` if the number is less than or equal to `other`, otherwise the difference
/// between `self` and `other` is returned.
fn abs_sub(&self, other: &Self) -> Self;
fn abs_sub(self, other: Self) -> Self;
/// Returns the sign of the number.
///
@ -183,31 +183,31 @@ pub trait Signed: Num + Neg<Self> {
/// * `0` if the number is zero
/// * `1` if the number is positive
/// * `-1` if the number is negative
fn signum(&self) -> Self;
fn signum(self) -> Self;
/// Returns true if the number is positive and false if the number is zero or negative.
fn is_positive(&self) -> bool;
fn is_positive(self) -> bool;
/// Returns true if the number is negative and false if the number is zero or positive.
fn is_negative(&self) -> bool;
fn is_negative(self) -> bool;
}
macro_rules! signed_impl(
($($t:ty)*) => ($(
impl Signed for $t {
($($T:ty)*) => ($(
impl Signed for $T {
#[inline]
fn abs(&self) -> $t {
if self.is_negative() { -*self } else { *self }
fn abs(self) -> $T {
if self.is_negative() { -self } else { self }
}
#[inline]
fn abs_sub(&self, other: &$t) -> $t {
if *self <= *other { 0 } else { *self - *other }
fn abs_sub(self, other: $T) -> $T {
if self <= other { 0 } else { self - other }
}
#[inline]
fn signum(&self) -> $t {
match *self {
fn signum(self) -> $T {
match self {
n if n > 0 => 1,
0 => 0,
_ => -1,
@ -215,10 +215,10 @@ fn signum(&self) -> $t {
}
#[inline]
fn is_positive(&self) -> bool { *self > 0 }
fn is_positive(self) -> bool { self > 0 }
#[inline]
fn is_negative(&self) -> bool { *self < 0 }
fn is_negative(self) -> bool { self < 0 }
}
)*)
)
@ -226,21 +226,21 @@ fn is_negative(&self) -> bool { *self < 0 }
signed_impl!(int i8 i16 i32 i64)
macro_rules! signed_float_impl(
($t:ty, $nan:expr, $inf:expr, $neg_inf:expr, $fabs:path, $fcopysign:path, $fdim:ident) => {
impl Signed for $t {
($T:ty, $nan:expr, $inf:expr, $neg_inf:expr, $fabs:path, $fcopysign:path, $fdim:ident) => {
impl Signed for $T {
/// Computes the absolute value. Returns `NAN` if the number is `NAN`.
#[inline]
fn abs(&self) -> $t {
unsafe { $fabs(*self) }
fn abs(self) -> $T {
unsafe { $fabs(self) }
}
/// The positive difference of two numbers. Returns `0.0` if the number is
/// less than or equal to `other`, otherwise the difference between`self`
/// and `other` is returned.
#[inline]
fn abs_sub(&self, other: &$t) -> $t {
extern { fn $fdim(a: $t, b: $t) -> $t; }
unsafe { $fdim(*self, *other) }
fn abs_sub(self, other: $T) -> $T {
extern { fn $fdim(a: $T, b: $T) -> $T; }
unsafe { $fdim(self, other) }
}
/// # Returns
@ -249,19 +249,19 @@ fn abs_sub(&self, other: &$t) -> $t {
/// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
/// - `NAN` if the number is NaN
#[inline]
fn signum(&self) -> $t {
fn signum(self) -> $T {
if self != self { $nan } else {
unsafe { $fcopysign(1.0, *self) }
unsafe { $fcopysign(1.0, self) }
}
}
/// Returns `true` if the number is positive, including `+0.0` and `INFINITY`
#[inline]
fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == $inf }
fn is_positive(self) -> bool { self > 0.0 || (1.0 / self) == $inf }
/// Returns `true` if the number is negative, including `-0.0` and `NEG_INFINITY`
#[inline]
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == $neg_inf }
fn is_negative(self) -> bool { self < 0.0 || (1.0 / self) == $neg_inf }
}
}
)
@ -287,7 +287,7 @@ pub fn abs<T: Signed>(value: T) -> T {
/// between `x` and `y` is returned.
#[inline(always)]
pub fn abs_sub<T: Signed>(x: T, y: T) -> T {
x.abs_sub(&y)
x.abs_sub(y)
}
/// Returns the sign of the number.

View File

@ -39,10 +39,10 @@ pub fn test_abs() {
#[test]
fn test_abs_sub() {
assert!((-1 as $T).abs_sub(&(1 as $T)) == 0 as $T);
assert!((1 as $T).abs_sub(&(1 as $T)) == 0 as $T);
assert!((1 as $T).abs_sub(&(0 as $T)) == 1 as $T);
assert!((1 as $T).abs_sub(&(-1 as $T)) == 2 as $T);
assert!((-1 as $T).abs_sub(1 as $T) == 0 as $T);
assert!((1 as $T).abs_sub(1 as $T) == 0 as $T);
assert!((1 as $T).abs_sub(0 as $T) == 1 as $T);
assert!((1 as $T).abs_sub(-1 as $T) == 2 as $T);
}
#[test]

View File

@ -593,20 +593,20 @@ pub fn test_abs() {
#[test]
fn test_abs_sub() {
assert_eq!((-1f32).abs_sub(&1f32), 0f32);
assert_eq!(1f32.abs_sub(&1f32), 0f32);
assert_eq!(1f32.abs_sub(&0f32), 1f32);
assert_eq!(1f32.abs_sub(&-1f32), 2f32);
assert_eq!(NEG_INFINITY.abs_sub(&0f32), 0f32);
assert_eq!(INFINITY.abs_sub(&1f32), INFINITY);
assert_eq!(0f32.abs_sub(&NEG_INFINITY), INFINITY);
assert_eq!(0f32.abs_sub(&INFINITY), 0f32);
assert_eq!((-1f32).abs_sub(1f32), 0f32);
assert_eq!(1f32.abs_sub(1f32), 0f32);
assert_eq!(1f32.abs_sub(0f32), 1f32);
assert_eq!(1f32.abs_sub(-1f32), 2f32);
assert_eq!(NEG_INFINITY.abs_sub(0f32), 0f32);
assert_eq!(INFINITY.abs_sub(1f32), INFINITY);
assert_eq!(0f32.abs_sub(NEG_INFINITY), INFINITY);
assert_eq!(0f32.abs_sub(INFINITY), 0f32);
}
#[test]
fn test_abs_sub_nowin() {
assert!(NAN.abs_sub(&-1f32).is_nan());
assert!(1f32.abs_sub(&NAN).is_nan());
assert!(NAN.abs_sub(-1f32).is_nan());
assert!(1f32.abs_sub(NAN).is_nan());
}
#[test]

View File

@ -591,20 +591,20 @@ pub fn test_abs() {
#[test]
fn test_abs_sub() {
assert_eq!((-1f64).abs_sub(&1f64), 0f64);
assert_eq!(1f64.abs_sub(&1f64), 0f64);
assert_eq!(1f64.abs_sub(&0f64), 1f64);
assert_eq!(1f64.abs_sub(&-1f64), 2f64);
assert_eq!(NEG_INFINITY.abs_sub(&0f64), 0f64);
assert_eq!(INFINITY.abs_sub(&1f64), INFINITY);
assert_eq!(0f64.abs_sub(&NEG_INFINITY), INFINITY);
assert_eq!(0f64.abs_sub(&INFINITY), 0f64);
assert_eq!((-1f64).abs_sub(1f64), 0f64);
assert_eq!(1f64.abs_sub(1f64), 0f64);
assert_eq!(1f64.abs_sub(0f64), 1f64);
assert_eq!(1f64.abs_sub(-1f64), 2f64);
assert_eq!(NEG_INFINITY.abs_sub(0f64), 0f64);
assert_eq!(INFINITY.abs_sub(1f64), INFINITY);
assert_eq!(0f64.abs_sub(NEG_INFINITY), INFINITY);
assert_eq!(0f64.abs_sub(INFINITY), 0f64);
}
#[test]
fn test_abs_sub_nowin() {
assert!(NAN.abs_sub(&-1f64).is_nan());
assert!(1f64.abs_sub(&NAN).is_nan());
assert!(NAN.abs_sub(-1f64).is_nan());
assert!(1f64.abs_sub(NAN).is_nan());
}
#[test]