about summary refs log tree commit diff
diff options
context:
space:
mode:
authorErick Tryzelaar <erick.tryzelaar@gmail.com>2012-12-20 07:14:38 -0800
committerErick Tryzelaar <erick.tryzelaar@gmail.com>2012-12-20 07:15:04 -0800
commite8102e73a91de261556cddd0d055e69234a37a82 (patch)
treebc02ee400393f97966917048649397d20b73f8b3
parent8554d5e7104be30d20ce8e8dc08239ce20b171c2 (diff)
downloadrust-e8102e73a91de261556cddd0d055e69234a37a82.tar.gz
rust-e8102e73a91de261556cddd0d055e69234a37a82.zip
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.
-rw-r--r--src/libcore/f32.rs8
-rw-r--r--src/libcore/f64.rs8
-rw-r--r--src/libcore/float.rs8
-rw-r--r--src/libcore/int-template.rs8
-rw-r--r--src/libcore/num.rs8
-rw-r--r--src/libcore/uint-template.rs8
6 files changed, 48 insertions, 0 deletions
diff --git a/src/libcore/f32.rs b/src/libcore/f32.rs
index 9fbf902170a..5e34c7c5530 100644
--- a/src/libcore/f32.rs
+++ b/src/libcore/f32.rs
@@ -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
diff --git a/src/libcore/f64.rs b/src/libcore/f64.rs
index 86565f9e252..2e35d0360b6 100644
--- a/src/libcore/f64.rs
+++ b/src/libcore/f64.rs
@@ -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
diff --git a/src/libcore/float.rs b/src/libcore/float.rs
index 2ea9c5925ef..27184a39708 100644
--- a/src/libcore/float.rs
+++ b/src/libcore/float.rs
@@ -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.);
diff --git a/src/libcore/int-template.rs b/src/libcore/int-template.rs
index eed439823fc..c3fe24fff32 100644
--- a/src/libcore/int-template.rs
+++ b/src/libcore/int-template.rs
@@ -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` \
diff --git a/src/libcore/num.rs b/src/libcore/num.rs
index fed9db8767b..4c2daa458a4 100644
--- a/src/libcore/num.rs
+++ b/src/libcore/num.rs
@@ -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;
+}
diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs
index 1a9bca92d1f..80b393a813c 100644
--- a/src/libcore/uint-template.rs
+++ b/src/libcore/uint-template.rs
@@ -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` \