Skip to content

Commit 6366e74

Browse files
committed
auto merge of #4910 : pcwalton/rust/num-simplification, r=pcwalton
Sadly I could not use trait inheritance due to a type parameter substitution bug. r? @brson
2 parents 6efa354 + 216e85f commit 6366e74

15 files changed

+165
-118
lines changed

src/libcore/core.rc

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ pub use vec::{OwnedVector, OwnedCopyableVector};
199199
pub use iter::{BaseIter, ExtendedIter, EqIter, CopyableIter};
200200
pub use iter::{CopyableOrderedIter, CopyableNonstrictIter, Times};
201201

202-
pub use num::{Num, NumCast};
202+
pub use num::NumCast;
203203
pub use ptr::Ptr;
204204
pub use to_str::ToStr;
205205
pub use clone::Clone;

src/libcore/num/f32.rs

+27-16
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
use cmath;
1414
use cmp;
1515
use libc::{c_float, c_int};
16-
use num;
1716
use num::NumCast;
17+
use num;
18+
use ops;
1819
use option::Option;
1920
use from_str;
2021
use to_str;
@@ -271,21 +272,6 @@ impl f32 : cmp::Ord {
271272
pure fn gt(&self, other: &f32) -> bool { (*self) > (*other) }
272273
}
273274

274-
impl f32: num::Num {
275-
#[inline(always)]
276-
pure fn add(&self, other: &f32) -> f32 { return *self + *other; }
277-
#[inline(always)]
278-
pure fn sub(&self, other: &f32) -> f32 { return *self - *other; }
279-
#[inline(always)]
280-
pure fn mul(&self, other: &f32) -> f32 { return *self * *other; }
281-
#[inline(always)]
282-
pure fn div(&self, other: &f32) -> f32 { return *self / *other; }
283-
#[inline(always)]
284-
pure fn modulo(&self, other: &f32) -> f32 { return *self % *other; }
285-
#[inline(always)]
286-
pure fn neg(&self) -> f32 { return -*self; }
287-
}
288-
289275
impl f32: num::Zero {
290276
#[inline(always)]
291277
static pure fn zero() -> f32 { 0.0 }
@@ -320,6 +306,31 @@ pub impl f32: NumCast {
320306
#[inline(always)] pure fn to_float(&self) -> float { *self as float }
321307
}
322308

309+
#[cfg(notest)]
310+
impl ops::Add<f32,f32> for f32 {
311+
pure fn add(&self, other: &f32) -> f32 { *self + *other }
312+
}
313+
#[cfg(notest)]
314+
impl ops::Sub<f32,f32> for f32 {
315+
pure fn sub(&self, other: &f32) -> f32 { *self - *other }
316+
}
317+
#[cfg(notest)]
318+
impl ops::Mul<f32,f32> for f32 {
319+
pure fn mul(&self, other: &f32) -> f32 { *self * *other }
320+
}
321+
#[cfg(notest)]
322+
impl ops::Div<f32,f32> for f32 {
323+
pure fn div(&self, other: &f32) -> f32 { *self / *other }
324+
}
325+
#[cfg(notest)]
326+
impl ops::Modulo<f32,f32> for f32 {
327+
pure fn modulo(&self, other: &f32) -> f32 { *self % *other }
328+
}
329+
#[cfg(notest)]
330+
impl ops::Neg<f32> for f32 {
331+
pure fn neg(&self) -> f32 { -*self }
332+
}
333+
323334
#[abi="rust-intrinsic"]
324335
pub extern {
325336
fn floorf32(val: f32) -> f32;

src/libcore/num/f64.rs

+27-16
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ use cmath;
1414
use cmp;
1515
use libc::{c_double, c_int};
1616
use libc;
17-
use num;
1817
use num::NumCast;
18+
use num;
19+
use ops;
1920
use option::Option;
2021
use to_str;
2122
use from_str;
@@ -296,21 +297,6 @@ impl f64 : cmp::Ord {
296297
pure fn gt(&self, other: &f64) -> bool { (*self) > (*other) }
297298
}
298299

299-
impl f64: num::Num {
300-
#[inline(always)]
301-
pure fn add(&self, other: &f64) -> f64 { return *self + *other; }
302-
#[inline(always)]
303-
pure fn sub(&self, other: &f64) -> f64 { return *self - *other; }
304-
#[inline(always)]
305-
pure fn mul(&self, other: &f64) -> f64 { return *self * *other; }
306-
#[inline(always)]
307-
pure fn div(&self, other: &f64) -> f64 { return *self / *other; }
308-
#[inline(always)]
309-
pure fn modulo(&self, other: &f64) -> f64 { return *self % *other; }
310-
#[inline(always)]
311-
pure fn neg(&self) -> f64 { return -*self; }
312-
}
313-
314300
pub impl f64: NumCast {
315301
/**
316302
* Cast `n` to an `f64`
@@ -345,6 +331,31 @@ impl f64: num::One {
345331
static pure fn one() -> f64 { 1.0 }
346332
}
347333

334+
#[cfg(notest)]
335+
impl ops::Add<f64,f64> for f64 {
336+
pure fn add(&self, other: &f64) -> f64 { *self + *other }
337+
}
338+
#[cfg(notest)]
339+
impl ops::Sub<f64,f64> for f64 {
340+
pure fn sub(&self, other: &f64) -> f64 { *self - *other }
341+
}
342+
#[cfg(notest)]
343+
impl ops::Mul<f64,f64> for f64 {
344+
pure fn mul(&self, other: &f64) -> f64 { *self * *other }
345+
}
346+
#[cfg(notest)]
347+
impl ops::Div<f64,f64> for f64 {
348+
pure fn div(&self, other: &f64) -> f64 { *self / *other }
349+
}
350+
#[cfg(notest)]
351+
impl ops::Modulo<f64,f64> for f64 {
352+
pure fn modulo(&self, other: &f64) -> f64 { *self % *other }
353+
}
354+
#[cfg(notest)]
355+
impl ops::Neg<f64> for f64 {
356+
pure fn neg(&self) -> f64 { -*self }
357+
}
358+
348359
#[abi="rust-intrinsic"]
349360
pub extern {
350361
fn floorf64(val: f64) -> f64;

src/libcore/num/float.rs

+27-16
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ use m_float = f64;
2525
use cmp::{Eq, Ord};
2626
use cmp;
2727
use f64;
28-
use num;
2928
use num::NumCast;
29+
use num;
30+
use ops;
3031
use option::{None, Option, Some};
3132
use str;
3233
use uint;
@@ -404,21 +405,6 @@ impl float : Ord {
404405
pure fn gt(&self, other: &float) -> bool { (*self) > (*other) }
405406
}
406407
407-
impl float: num::Num {
408-
#[inline(always)]
409-
pub pure fn add(&self, other: &float) -> float { return *self + *other; }
410-
#[inline(always)]
411-
pub pure fn sub(&self, other: &float) -> float { return *self - *other; }
412-
#[inline(always)]
413-
pub pure fn mul(&self, other: &float) -> float { return *self * *other; }
414-
#[inline(always)]
415-
pub pure fn div(&self, other: &float) -> float { return *self / *other; }
416-
#[inline(always)]
417-
pure fn modulo(&self, other: &float) -> float { return *self % *other; }
418-
#[inline(always)]
419-
pure fn neg(&self) -> float { return -*self; }
420-
}
421-
422408
impl float: num::Zero {
423409
#[inline(always)]
424410
static pure fn zero() -> float { 0.0 }
@@ -486,6 +472,31 @@ impl float: num::Round {
486472
}
487473
}
488474
475+
#[cfg(notest)]
476+
impl ops::Add<float,float> for float {
477+
pure fn add(&self, other: &float) -> float { *self + *other }
478+
}
479+
#[cfg(notest)]
480+
impl ops::Sub<float,float> for float {
481+
pure fn sub(&self, other: &float) -> float { *self - *other }
482+
}
483+
#[cfg(notest)]
484+
impl ops::Mul<float,float> for float {
485+
pure fn mul(&self, other: &float) -> float { *self * *other }
486+
}
487+
#[cfg(notest)]
488+
impl ops::Div<float,float> for float {
489+
pure fn div(&self, other: &float) -> float { *self / *other }
490+
}
491+
#[cfg(notest)]
492+
impl ops::Modulo<float,float> for float {
493+
pure fn modulo(&self, other: &float) -> float { *self % *other }
494+
}
495+
#[cfg(notest)]
496+
impl ops::Neg<float> for float {
497+
pure fn neg(&self) -> float { -*self }
498+
}
499+
489500
#[test]
490501
pub fn test_from_str() {
491502
assert from_str(~"3") == Some(3.);

src/libcore/num/int-template.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,6 @@ impl T : Eq {
166166
pure fn ne(&self, other: &T) -> bool { return (*self) != (*other); }
167167
}
168168
169-
impl T: num::Num {
170-
#[inline(always)]
171-
pure fn add(&self, other: &T) -> T { return *self + *other; }
172-
#[inline(always)]
173-
pure fn sub(&self, other: &T) -> T { return *self - *other; }
174-
#[inline(always)]
175-
pure fn mul(&self, other: &T) -> T { return *self * *other; }
176-
#[inline(always)]
177-
pure fn div(&self, other: &T) -> T { return *self / *other; }
178-
#[inline(always)]
179-
pure fn modulo(&self, other: &T) -> T { return *self % *other; }
180-
#[inline(always)]
181-
pure fn neg(&self) -> T { return -*self; }
182-
}
183-
184169
impl T: num::Zero {
185170
#[inline(always)]
186171
static pure fn zero() -> T { 0 }
@@ -203,6 +188,31 @@ impl T: num::Round {
203188
pure fn fract(&self) -> T { 0 }
204189
}
205190
191+
#[cfg(notest)]
192+
impl ops::Add<T,T> for T {
193+
pure fn add(&self, other: &T) -> T { *self + *other }
194+
}
195+
#[cfg(notest)]
196+
impl ops::Sub<T,T> for T {
197+
pure fn sub(&self, other: &T) -> T { *self - *other }
198+
}
199+
#[cfg(notest)]
200+
impl ops::Mul<T,T> for T {
201+
pure fn mul(&self, other: &T) -> T { *self * *other }
202+
}
203+
#[cfg(notest)]
204+
impl ops::Div<T,T> for T {
205+
pure fn div(&self, other: &T) -> T { *self / *other }
206+
}
207+
#[cfg(notest)]
208+
impl ops::Modulo<T,T> for T {
209+
pure fn modulo(&self, other: &T) -> T { *self % *other }
210+
}
211+
#[cfg(notest)]
212+
impl ops::Neg<T> for T {
213+
pure fn neg(&self) -> T { -*self }
214+
}
215+
206216
// String conversion functions and impl str -> num
207217
208218
/// Parse a string as a number in base 10.

0 commit comments

Comments
 (0)