Skip to content

Commit 6727c6f

Browse files
committedFeb 13, 2013
auto merge of #4881 : bjz/rust/incoming, r=catamorphism
2 parents 91c59f5 + 48b2141 commit 6727c6f

24 files changed

+1044
-91
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;
202+
pub use num::{Num, NumCast};
203203
pub use ptr::Ptr;
204204
pub use to_str::ToStr;
205205
pub use clone::Clone;

‎src/libcore/num/f32.rs

+82-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use cmath;
1414
use cmp;
1515
use libc::{c_float, c_int};
1616
use num;
17+
use num::NumCast;
1718
use option::Option;
1819
use from_str;
1920
use to_str;
@@ -283,11 +284,6 @@ impl f32: num::Num {
283284
pure fn modulo(&self, other: &f32) -> f32 { return *self % *other; }
284285
#[inline(always)]
285286
pure fn neg(&self) -> f32 { return -*self; }
286-
287-
#[inline(always)]
288-
pure fn to_int(&self) -> int { return *self as int; }
289-
#[inline(always)]
290-
static pure fn from_int(n: int) -> f32 { return n as f32; }
291287
}
292288

293289
impl f32: num::Zero {
@@ -300,6 +296,30 @@ impl f32: num::One {
300296
static pure fn one() -> f32 { 1.0 }
301297
}
302298

299+
pub impl f32: NumCast {
300+
/**
301+
* Cast `n` to an `f32`
302+
*/
303+
#[inline(always)]
304+
static pure fn from<N:NumCast>(n: N) -> f32 { n.to_f32() }
305+
306+
#[inline(always)] pure fn to_u8(&self) -> u8 { *self as u8 }
307+
#[inline(always)] pure fn to_u16(&self) -> u16 { *self as u16 }
308+
#[inline(always)] pure fn to_u32(&self) -> u32 { *self as u32 }
309+
#[inline(always)] pure fn to_u64(&self) -> u64 { *self as u64 }
310+
#[inline(always)] pure fn to_uint(&self) -> uint { *self as uint }
311+
312+
#[inline(always)] pure fn to_i8(&self) -> i8 { *self as i8 }
313+
#[inline(always)] pure fn to_i16(&self) -> i16 { *self as i16 }
314+
#[inline(always)] pure fn to_i32(&self) -> i32 { *self as i32 }
315+
#[inline(always)] pure fn to_i64(&self) -> i64 { *self as i64 }
316+
#[inline(always)] pure fn to_int(&self) -> int { *self as int }
317+
318+
#[inline(always)] pure fn to_f32(&self) -> f32 { *self }
319+
#[inline(always)] pure fn to_f64(&self) -> f64 { *self as f64 }
320+
#[inline(always)] pure fn to_float(&self) -> float { *self as float }
321+
}
322+
303323
#[abi="rust-intrinsic"]
304324
pub extern {
305325
fn floorf32(val: f32) -> f32;
@@ -545,6 +565,63 @@ impl f32: num::FromStrRadix {
545565
}
546566
}
547567

568+
#[test]
569+
pub fn test_num() {
570+
let ten: f32 = num::cast(10);
571+
let two: f32 = num::cast(2);
572+
573+
assert (ten.add(&two) == num::cast(12));
574+
assert (ten.sub(&two) == num::cast(8));
575+
assert (ten.mul(&two) == num::cast(20));
576+
assert (ten.div(&two) == num::cast(5));
577+
assert (ten.modulo(&two) == num::cast(0));
578+
}
579+
580+
#[test]
581+
fn test_numcast() {
582+
assert (20u == 20f32.to_uint());
583+
assert (20u8 == 20f32.to_u8());
584+
assert (20u16 == 20f32.to_u16());
585+
assert (20u32 == 20f32.to_u32());
586+
assert (20u64 == 20f32.to_u64());
587+
assert (20i == 20f32.to_int());
588+
assert (20i8 == 20f32.to_i8());
589+
assert (20i16 == 20f32.to_i16());
590+
assert (20i32 == 20f32.to_i32());
591+
assert (20i64 == 20f32.to_i64());
592+
assert (20f == 20f32.to_float());
593+
assert (20f32 == 20f32.to_f32());
594+
assert (20f64 == 20f32.to_f64());
595+
596+
assert (20f32 == NumCast::from(20u));
597+
assert (20f32 == NumCast::from(20u8));
598+
assert (20f32 == NumCast::from(20u16));
599+
assert (20f32 == NumCast::from(20u32));
600+
assert (20f32 == NumCast::from(20u64));
601+
assert (20f32 == NumCast::from(20i));
602+
assert (20f32 == NumCast::from(20i8));
603+
assert (20f32 == NumCast::from(20i16));
604+
assert (20f32 == NumCast::from(20i32));
605+
assert (20f32 == NumCast::from(20i64));
606+
assert (20f32 == NumCast::from(20f));
607+
assert (20f32 == NumCast::from(20f32));
608+
assert (20f32 == NumCast::from(20f64));
609+
610+
assert (20f32 == num::cast(20u));
611+
assert (20f32 == num::cast(20u8));
612+
assert (20f32 == num::cast(20u16));
613+
assert (20f32 == num::cast(20u32));
614+
assert (20f32 == num::cast(20u64));
615+
assert (20f32 == num::cast(20i));
616+
assert (20f32 == num::cast(20i8));
617+
assert (20f32 == num::cast(20i16));
618+
assert (20f32 == num::cast(20i32));
619+
assert (20f32 == num::cast(20i64));
620+
assert (20f32 == num::cast(20f));
621+
assert (20f32 == num::cast(20f32));
622+
assert (20f32 == num::cast(20f64));
623+
}
624+
548625
//
549626
// Local Variables:
550627
// mode: rust

‎src/libcore/num/f64.rs

+80-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use cmp;
1515
use libc::{c_double, c_int};
1616
use libc;
1717
use num;
18+
use num::NumCast;
1819
use option::Option;
1920
use to_str;
2021
use from_str;
@@ -307,11 +308,30 @@ impl f64: num::Num {
307308
pure fn modulo(&self, other: &f64) -> f64 { return *self % *other; }
308309
#[inline(always)]
309310
pure fn neg(&self) -> f64 { return -*self; }
311+
}
310312

313+
pub impl f64: NumCast {
314+
/**
315+
* Cast `n` to an `f64`
316+
*/
311317
#[inline(always)]
312-
pure fn to_int(&self) -> int { return *self as int; }
313-
#[inline(always)]
314-
static pure fn from_int(n: int) -> f64 { return n as f64; }
318+
static pure fn from<N:NumCast>(n: N) -> f64 { n.to_f64() }
319+
320+
#[inline(always)] pure fn to_u8(&self) -> u8 { *self as u8 }
321+
#[inline(always)] pure fn to_u16(&self) -> u16 { *self as u16 }
322+
#[inline(always)] pure fn to_u32(&self) -> u32 { *self as u32 }
323+
#[inline(always)] pure fn to_u64(&self) -> u64 { *self as u64 }
324+
#[inline(always)] pure fn to_uint(&self) -> uint { *self as uint }
325+
326+
#[inline(always)] pure fn to_i8(&self) -> i8 { *self as i8 }
327+
#[inline(always)] pure fn to_i16(&self) -> i16 { *self as i16 }
328+
#[inline(always)] pure fn to_i32(&self) -> i32 { *self as i32 }
329+
#[inline(always)] pure fn to_i64(&self) -> i64 { *self as i64 }
330+
#[inline(always)] pure fn to_int(&self) -> int { *self as int }
331+
332+
#[inline(always)] pure fn to_f32(&self) -> f32 { *self as f32 }
333+
#[inline(always)] pure fn to_f64(&self) -> f64 { *self }
334+
#[inline(always)] pure fn to_float(&self) -> float { *self as float }
315335
}
316336

317337
impl f64: num::Zero {
@@ -569,6 +589,63 @@ impl f64: num::FromStrRadix {
569589
}
570590
}
571591

592+
#[test]
593+
pub fn test_num() {
594+
let ten: f64 = num::cast(10);
595+
let two: f64 = num::cast(2);
596+
597+
assert (ten.add(&two) == num::cast(12));
598+
assert (ten.sub(&two) == num::cast(8));
599+
assert (ten.mul(&two) == num::cast(20));
600+
assert (ten.div(&two) == num::cast(5));
601+
assert (ten.modulo(&two) == num::cast(0));
602+
}
603+
604+
#[test]
605+
fn test_numcast() {
606+
assert (20u == 20f64.to_uint());
607+
assert (20u8 == 20f64.to_u8());
608+
assert (20u16 == 20f64.to_u16());
609+
assert (20u32 == 20f64.to_u32());
610+
assert (20u64 == 20f64.to_u64());
611+
assert (20i == 20f64.to_int());
612+
assert (20i8 == 20f64.to_i8());
613+
assert (20i16 == 20f64.to_i16());
614+
assert (20i32 == 20f64.to_i32());
615+
assert (20i64 == 20f64.to_i64());
616+
assert (20f == 20f64.to_float());
617+
assert (20f32 == 20f64.to_f32());
618+
assert (20f64 == 20f64.to_f64());
619+
620+
assert (20f64 == NumCast::from(20u));
621+
assert (20f64 == NumCast::from(20u8));
622+
assert (20f64 == NumCast::from(20u16));
623+
assert (20f64 == NumCast::from(20u32));
624+
assert (20f64 == NumCast::from(20u64));
625+
assert (20f64 == NumCast::from(20i));
626+
assert (20f64 == NumCast::from(20i8));
627+
assert (20f64 == NumCast::from(20i16));
628+
assert (20f64 == NumCast::from(20i32));
629+
assert (20f64 == NumCast::from(20i64));
630+
assert (20f64 == NumCast::from(20f));
631+
assert (20f64 == NumCast::from(20f32));
632+
assert (20f64 == NumCast::from(20f64));
633+
634+
assert (20f64 == num::cast(20u));
635+
assert (20f64 == num::cast(20u8));
636+
assert (20f64 == num::cast(20u16));
637+
assert (20f64 == num::cast(20u32));
638+
assert (20f64 == num::cast(20u64));
639+
assert (20f64 == num::cast(20i));
640+
assert (20f64 == num::cast(20i8));
641+
assert (20f64 == num::cast(20i16));
642+
assert (20f64 == num::cast(20i32));
643+
assert (20f64 == num::cast(20i64));
644+
assert (20f64 == num::cast(20f));
645+
assert (20f64 == num::cast(20f32));
646+
assert (20f64 == num::cast(20f64));
647+
}
648+
572649
//
573650
// Local Variables:
574651
// mode: rust

‎src/libcore/num/float.rs

+78-20
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use cmp::{Eq, Ord};
2626
use cmp;
2727
use f64;
2828
use num;
29-
use num::Num::from_int;
29+
use num::NumCast;
3030
use option::{None, Option, Some};
3131
use str;
3232
use uint;
@@ -417,11 +417,6 @@ impl float: num::Num {
417417
pure fn modulo(&self, other: &float) -> float { return *self % *other; }
418418
#[inline(always)]
419419
pure fn neg(&self) -> float { return -*self; }
420-
421-
#[inline(always)]
422-
pure fn to_int(&self) -> int { return *self as int; }
423-
#[inline(always)]
424-
static pure fn from_int(&self, n: int) -> float { return n as float; }
425420
}
426421
427422
impl float: num::Zero {
@@ -434,6 +429,30 @@ impl float: num::One {
434429
static pure fn one() -> float { 1.0 }
435430
}
436431
432+
pub impl float: NumCast {
433+
/**
434+
* Cast `n` to a `float`
435+
*/
436+
#[inline(always)]
437+
static pure fn from<N:NumCast>(n: N) -> float { n.to_float() }
438+
439+
#[inline(always)] pure fn to_u8(&self) -> u8 { *self as u8 }
440+
#[inline(always)] pure fn to_u16(&self) -> u16 { *self as u16 }
441+
#[inline(always)] pure fn to_u32(&self) -> u32 { *self as u32 }
442+
#[inline(always)] pure fn to_u64(&self) -> u64 { *self as u64 }
443+
#[inline(always)] pure fn to_uint(&self) -> uint { *self as uint }
444+
445+
#[inline(always)] pure fn to_i8(&self) -> i8 { *self as i8 }
446+
#[inline(always)] pure fn to_i16(&self) -> i16 { *self as i16 }
447+
#[inline(always)] pure fn to_i32(&self) -> i32 { *self as i32 }
448+
#[inline(always)] pure fn to_i64(&self) -> i64 { *self as i64 }
449+
#[inline(always)] pure fn to_int(&self) -> int { *self as int }
450+
451+
#[inline(always)] pure fn to_f32(&self) -> f32 { *self as f32 }
452+
#[inline(always)] pure fn to_f64(&self) -> f64 { *self as f64 }
453+
#[inline(always)] pure fn to_float(&self) -> float { *self }
454+
}
455+
437456
impl float: num::Round {
438457
#[inline(always)]
439458
pure fn round(&self, mode: num::RoundMode) -> float {
@@ -657,21 +676,60 @@ pub fn test_round() {
657676
}
658677

659678
#[test]
660-
pub fn test_traits() {
661-
fn test<U:num::Num cmp::Eq>(ten: &U) {
662-
assert (ten.to_int() == 10);
663-
664-
let two: U = from_int(2);
665-
assert (two.to_int() == 2);
666-
667-
assert (ten.add(&two) == from_int(12));
668-
assert (ten.sub(&two) == from_int(8));
669-
assert (ten.mul(&two) == from_int(20));
670-
assert (ten.div(&two) == from_int(5));
671-
assert (ten.modulo(&two) == from_int(0));
672-
}
679+
pub fn test_num() {
680+
let ten: float = num::cast(10);
681+
let two: float = num::cast(2);
673682

674-
test(&10.0);
683+
assert (ten.add(&two) == num::cast(12));
684+
assert (ten.sub(&two) == num::cast(8));
685+
assert (ten.mul(&two) == num::cast(20));
686+
assert (ten.div(&two) == num::cast(5));
687+
assert (ten.modulo(&two) == num::cast(0));
688+
}
689+
690+
#[test]
691+
fn test_numcast() {
692+
assert (20u == 20f.to_uint());
693+
assert (20u8 == 20f.to_u8());
694+
assert (20u16 == 20f.to_u16());
695+
assert (20u32 == 20f.to_u32());
696+
assert (20u64 == 20f.to_u64());
697+
assert (20i == 20f.to_int());
698+
assert (20i8 == 20f.to_i8());
699+
assert (20i16 == 20f.to_i16());
700+
assert (20i32 == 20f.to_i32());
701+
assert (20i64 == 20f.to_i64());
702+
assert (20f == 20f.to_float());
703+
assert (20f32 == 20f.to_f32());
704+
assert (20f64 == 20f.to_f64());
705+
706+
assert (20f == NumCast::from(20u));
707+
assert (20f == NumCast::from(20u8));
708+
assert (20f == NumCast::from(20u16));
709+
assert (20f == NumCast::from(20u32));
710+
assert (20f == NumCast::from(20u64));
711+
assert (20f == NumCast::from(20i));
712+
assert (20f == NumCast::from(20i8));
713+
assert (20f == NumCast::from(20i16));
714+
assert (20f == NumCast::from(20i32));
715+
assert (20f == NumCast::from(20i64));
716+
assert (20f == NumCast::from(20f));
717+
assert (20f == NumCast::from(20f32));
718+
assert (20f == NumCast::from(20f64));
719+
720+
assert (20f == num::cast(20u));
721+
assert (20f == num::cast(20u8));
722+
assert (20f == num::cast(20u16));
723+
assert (20f == num::cast(20u32));
724+
assert (20f == num::cast(20u64));
725+
assert (20f == num::cast(20i));
726+
assert (20f == num::cast(20i8));
727+
assert (20f == num::cast(20i16));
728+
assert (20f == num::cast(20i32));
729+
assert (20f == num::cast(20i64));
730+
assert (20f == num::cast(20f));
731+
assert (20f == num::cast(20f32));
732+
assert (20f == num::cast(20f64));
675733
}
676734

677735

0 commit comments

Comments
 (0)
Please sign in to comment.