From e85a3d82470e2e45db370b62e4fd54175c4b144d Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Tue, 25 Sep 2012 15:15:49 -0700 Subject: [PATCH] Demode Num trait and impls --- src/libcore/f32.rs | 10 +++--- src/libcore/f64.rs | 10 +++--- src/libcore/float.rs | 36 +++++++++---------- src/libcore/int-template.rs | 20 +++++------ src/libcore/num.rs | 10 +++--- src/libcore/str.rs | 2 +- src/libcore/uint-template.rs | 10 +++--- .../run-pass/numeric-method-autoexport.rs | 20 +++++------ 8 files changed, 59 insertions(+), 59 deletions(-) diff --git a/src/libcore/f32.rs b/src/libcore/f32.rs index a8ca15f6afc..f5e4d629726 100644 --- a/src/libcore/f32.rs +++ b/src/libcore/f32.rs @@ -158,11 +158,11 @@ pure fn logarithm(n: f32, b: f32) -> f32 { } impl f32: num::Num { - pure fn add(&&other: f32) -> f32 { return self + other; } - pure fn sub(&&other: f32) -> f32 { return self - other; } - pure fn mul(&&other: f32) -> f32 { return self * other; } - pure fn div(&&other: f32) -> f32 { return self / other; } - pure fn modulo(&&other: f32) -> f32 { return self % other; } + pure fn add(other: &f32) -> f32 { return self + *other; } + pure fn sub(other: &f32) -> f32 { return self - *other; } + pure fn mul(other: &f32) -> f32 { return self * *other; } + pure fn div(other: &f32) -> f32 { return self / *other; } + pure fn modulo(other: &f32) -> f32 { return self % *other; } pure fn neg() -> f32 { return -self; } pure fn to_int() -> int { return self as int; } diff --git a/src/libcore/f64.rs b/src/libcore/f64.rs index 0be0a059132..56f9cd85db9 100644 --- a/src/libcore/f64.rs +++ b/src/libcore/f64.rs @@ -185,11 +185,11 @@ pure fn logarithm(n: f64, b: f64) -> f64 { } impl f64: num::Num { - pure fn add(&&other: f64) -> f64 { return self + other; } - pure fn sub(&&other: f64) -> f64 { return self - other; } - pure fn mul(&&other: f64) -> f64 { return self * other; } - pure fn div(&&other: f64) -> f64 { return self / other; } - pure fn modulo(&&other: f64) -> f64 { return self % other; } + pure fn add(other: &f64) -> f64 { return self + *other; } + pure fn sub(other: &f64) -> f64 { return self - *other; } + pure fn mul(other: &f64) -> f64 { return self * *other; } + pure fn div(other: &f64) -> f64 { return self / *other; } + pure fn modulo(other: &f64) -> f64 { return self % *other; } pure fn neg() -> f64 { return -self; } pure fn to_int() -> int { return self as int; } diff --git a/src/libcore/float.rs b/src/libcore/float.rs index 2cd95269aaf..eaa51814056 100644 --- a/src/libcore/float.rs +++ b/src/libcore/float.rs @@ -139,7 +139,7 @@ fn to_str_common(num: float, digits: uint, exact: bool) -> ~str { // while we still need digits // build stack of digits - while ii > 0u && (frac >= epsilon_prime || exact) { + while ii > 0 && (frac >= epsilon_prime || exact) { // store the next digit frac *= 10.0; let digit = frac as uint; @@ -153,25 +153,25 @@ fn to_str_common(num: float, digits: uint, exact: bool) -> ~str { let mut acc; let mut racc = ~""; - let mut carry = if frac * 10.0 as uint >= 5u { 1u } else { 0u }; + let mut carry = if frac * 10.0 as uint >= 5 { 1 } else { 0 }; // turn digits into string // using stack of digits - while vec::len(fractionalParts) > 0u { + while fractionalParts.is_not_empty() { let mut adjusted_digit = carry + vec::pop(fractionalParts); - if adjusted_digit == 10u { - carry = 1u; - adjusted_digit %= 10u + if adjusted_digit == 10 { + carry = 1; + adjusted_digit %= 10 } else { - carry = 0u + carry = 0; }; racc = uint::str(adjusted_digit) + racc; } // pad decimals with trailing zeroes - while str::len(racc) < digits && exact { + while racc.len() < digits && exact { racc += ~"0" } @@ -428,11 +428,11 @@ impl float : Ord { } impl float: num::Num { - pure fn add(&&other: float) -> float { return self + other; } - pure fn sub(&&other: float) -> float { return self - other; } - pure fn mul(&&other: float) -> float { return self * other; } - pure fn div(&&other: float) -> float { return self / other; } - pure fn modulo(&&other: float) -> float { return self % other; } + pure fn add(other: &float) -> float { return self + *other; } + pure fn sub(other: &float) -> float { return self - *other; } + pure fn mul(other: &float) -> float { return self * *other; } + pure fn div(other: &float) -> float { return self / *other; } + pure fn modulo(other: &float) -> float { return self % *other; } pure fn neg() -> float { return -self; } pure fn to_int() -> int { return self as int; } @@ -540,11 +540,11 @@ fn test_traits() { let two: U = from_int(2); assert (two.to_int() == 2); - assert (ten.add(two) == from_int(12)); - assert (ten.sub(two) == from_int(8)); - assert (ten.mul(two) == from_int(20)); - assert (ten.div(two) == from_int(5)); - assert (ten.modulo(two) == from_int(0)); + assert (ten.add(&two) == from_int(12)); + assert (ten.sub(&two) == from_int(8)); + assert (ten.mul(&two) == from_int(20)); + assert (ten.div(&two) == from_int(5)); + assert (ten.modulo(&two) == from_int(0)); } test(&10.0); diff --git a/src/libcore/int-template.rs b/src/libcore/int-template.rs index e1137e6d269..649400e2360 100644 --- a/src/libcore/int-template.rs +++ b/src/libcore/int-template.rs @@ -81,11 +81,11 @@ impl T : Eq { } impl T: num::Num { - pure fn add(&&other: T) -> T { return self + other; } - pure fn sub(&&other: T) -> T { return self - other; } - pure fn mul(&&other: T) -> T { return self * other; } - pure fn div(&&other: T) -> T { return self / other; } - pure fn modulo(&&other: T) -> T { return self % other; } + pure fn add(other: &T) -> T { return self + *other; } + pure fn sub(other: &T) -> T { return self - *other; } + pure fn mul(other: &T) -> T { return self * *other; } + pure fn div(other: &T) -> T { return self / *other; } + pure fn modulo(other: &T) -> T { return self % *other; } pure fn neg() -> T { return -self; } pure fn to_int() -> int { return self as int; } @@ -250,11 +250,11 @@ fn test_interfaces() { let two: U = from_int(2); assert (two.to_int() == 2); - assert (ten.add(two) == from_int(12)); - assert (ten.sub(two) == from_int(8)); - assert (ten.mul(two) == from_int(20)); - assert (ten.div(two) == from_int(5)); - assert (ten.modulo(two) == from_int(0)); + assert (ten.add(&two) == from_int(12)); + assert (ten.sub(&two) == from_int(8)); + assert (ten.mul(&two) == from_int(20)); + assert (ten.div(&two) == from_int(5)); + assert (ten.modulo(&two) == from_int(0)); assert (ten.neg() == from_int(-10)); } diff --git a/src/libcore/num.rs b/src/libcore/num.rs index d5872933953..585a72d70ae 100644 --- a/src/libcore/num.rs +++ b/src/libcore/num.rs @@ -2,11 +2,11 @@ trait Num { // FIXME: Trait composition. (#2616) - pure fn add(&&other: self) -> self; - pure fn sub(&&other: self) -> self; - pure fn mul(&&other: self) -> self; - pure fn div(&&other: self) -> self; - pure fn modulo(&&other: self) -> self; + pure fn add(other: &self) -> self; + pure fn sub(other: &self) -> self; + pure fn mul(other: &self) -> self; + pure fn div(other: &self) -> self; + pure fn modulo(other: &self) -> self; pure fn neg() -> self; pure fn to_int() -> int; diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 737cd4d9d50..eaaf11dab5d 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -464,7 +464,7 @@ pure fn byte_slice(s: &str, f: fn(v: &[u8]) -> T) -> T { /// Convert a string to a vector of characters pure fn chars(s: &str) -> ~[char] { - let mut buf = ~[], i = 0u; + let mut buf = ~[], i = 0; let len = len(s); while i < len { let {ch, next} = char_range_at(s, i); diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs index dba28ec06e6..f5d2229513d 100644 --- a/src/libcore/uint-template.rs +++ b/src/libcore/uint-template.rs @@ -74,11 +74,11 @@ impl T : Eq { } impl T: num::Num { - pure fn add(&&other: T) -> T { return self + other; } - pure fn sub(&&other: T) -> T { return self - other; } - pure fn mul(&&other: T) -> T { return self * other; } - pure fn div(&&other: T) -> T { return self / other; } - pure fn modulo(&&other: T) -> T { return self % other; } + pure fn add(other: &T) -> T { return self + *other; } + pure fn sub(other: &T) -> T { return self - *other; } + pure fn mul(other: &T) -> T { return self * *other; } + pure fn div(other: &T) -> T { return self / *other; } + pure fn modulo(other: &T) -> T { return self % *other; } pure fn neg() -> T { return -self; } pure fn to_int() -> int { return self as int; } diff --git a/src/test/run-pass/numeric-method-autoexport.rs b/src/test/run-pass/numeric-method-autoexport.rs index 6a3676227cc..f2882b91983 100644 --- a/src/test/run-pass/numeric-method-autoexport.rs +++ b/src/test/run-pass/numeric-method-autoexport.rs @@ -5,11 +5,11 @@ fn main() { // ints // num - assert 15.add(6) == 21; - assert 15i8.add(6i8) == 21i8; - assert 15i16.add(6i16) == 21i16; - assert 15i32.add(6i32) == 21i32; - assert 15i64.add(6i64) == 21i64; + assert 15.add(&6) == 21; + assert 15i8.add(&6i8) == 21i8; + assert 15i16.add(&6i16) == 21i16; + assert 15i32.add(&6i32) == 21i32; + assert 15i64.add(&6i64) == 21i64; // times 15.times(|| false); 15i8.times(|| false); @@ -19,11 +19,11 @@ fn main() { // uints // num - assert 15u.add(6u) == 21u; - assert 15u8.add(6u8) == 21u8; - assert 15u16.add(6u16) == 21u16; - assert 15u32.add(6u32) == 21u32; - assert 15u64.add(6u64) == 21u64; + assert 15u.add(&6u) == 21u; + assert 15u8.add(&6u8) == 21u8; + assert 15u16.add(&6u16) == 21u16; + assert 15u32.add(&6u32) == 21u32; + assert 15u64.add(&6u64) == 21u64; // times 15u.times(|| false); 15u8.times(|| false);