core: Add a Zero and One trait to num

I believe these are the last traits we need in order
to start grouping our numerical types into mathematical
groups and rings.
This commit is contained in:
Erick Tryzelaar 2012-12-20 07:14:38 -08:00
parent 8554d5e710
commit e8102e73a9
6 changed files with 48 additions and 0 deletions

View File

@ -174,6 +174,14 @@ impl f32: num::Num {
static pure fn from_int(n: int) -> f32 { return n as f32; }
}
impl f32: num::Zero {
static pure fn zero() -> f32 { 0.0 }
}
impl f32: num::One {
static pure fn one() -> f32 { 1.0 }
}
//
// Local Variables:
// mode: rust

View File

@ -193,6 +193,14 @@ impl f64: num::Num {
static pure fn from_int(n: int) -> f64 { return n as f64; }
}
impl f64: num::Zero {
static pure fn zero() -> f64 { 0.0 }
}
impl f64: num::One {
static pure fn one() -> f64 { 1.0 }
}
//
// Local Variables:
// mode: rust

View File

@ -443,6 +443,14 @@ impl float: num::Num {
static pure fn from_int(&self, n: int) -> float { return n as float; }
}
impl float: num::Zero {
static pure fn zero() -> float { 0.0 }
}
impl float: num::One {
static pure fn one() -> float { 1.0 }
}
#[test]
pub fn test_from_str() {
assert from_str(~"3") == Some(3.);

View File

@ -91,6 +91,14 @@ impl T: num::Num {
static pure fn from_int(n: int) -> T { return n as T; }
}
impl T: num::Zero {
static pure fn zero() -> T { 0 }
}
impl T: num::One {
static pure fn one() -> T { 1 }
}
impl T: iter::Times {
#[inline(always)]
#[doc = "A convenience form for basic iteration. Given a variable `x` \

View File

@ -22,3 +22,11 @@ pub trait Num {
pure fn to_int(&self) -> int;
static pure fn from_int(n: int) -> self;
}
pub trait Zero {
static pure fn zero() -> self;
}
pub trait One {
static pure fn one() -> self;
}

View File

@ -85,6 +85,14 @@ impl T: num::Num {
static pure fn from_int(n: int) -> T { return n as T; }
}
impl T: num::Zero {
static pure fn zero() -> T { 0 }
}
impl T: num::One {
static pure fn one() -> T { 1 }
}
impl T: iter::Times {
#[inline(always)]
#[doc = "A convenience form for basic iteration. Given a variable `x` \