about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorErick Tryzelaar <erick.tryzelaar@gmail.com>2013-09-17 19:28:35 -0700
committerErick Tryzelaar <erick.tryzelaar@gmail.com>2013-10-02 07:55:42 -0700
commit9de7ad2d8c1729b7b11c6d234fc8ef8ce96809bb (patch)
tree431c955595157f7e0c53a77509f9827fd1bcaaa3 /src/libstd
parent5a64e1a35a17c7aebd91e0d2fc9003f08fb5fd6d (diff)
downloadrust-9de7ad2d8c1729b7b11c6d234fc8ef8ce96809bb.tar.gz
rust-9de7ad2d8c1729b7b11c6d234fc8ef8ce96809bb.zip
std: Swap {To,From}Primitive to use the 64bit as the unimplemented version
One downside with this current implementation is that since BigInt's
default is now 64 bit, we can convert larger BigInt's to a primitive,
however the current implementation on 32 bit architectures does not
take advantage of this fact.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/num/num.rs68
1 files changed, 36 insertions, 32 deletions
diff --git a/src/libstd/num/num.rs b/src/libstd/num/num.rs
index a8c85184664..fffa9b49699 100644
--- a/src/libstd/num/num.rs
+++ b/src/libstd/num/num.rs
@@ -351,65 +351,69 @@ pub trait Float: Real
 /// A generic trait for converting a value to a number.
 pub trait ToPrimitive {
     /// Converts the value of `self` to an `int`.
-    fn to_int(&self) -> Option<int>;
+    #[inline]
+    fn to_int(&self) -> Option<int> {
+        // XXX: Check for range.
+        self.to_i64().and_then(|x| Some(x as int))
+    }
 
     /// Converts the value of `self` to an `i8`.
     #[inline]
     fn to_i8(&self) -> Option<i8> {
         // XXX: Check for range.
-        self.to_int().and_then(|x| Some(x as i8))
+        self.to_i64().and_then(|x| Some(x as i8))
     }
 
     /// Converts the value of `self` to an `i16`.
     #[inline]
     fn to_i16(&self) -> Option<i16> {
         // XXX: Check for range.
-        self.to_int().and_then(|x| Some(x as i16))
+        self.to_i64().and_then(|x| Some(x as i16))
     }
 
     /// Converts the value of `self` to an `i32`.
     #[inline]
     fn to_i32(&self) -> Option<i32> {
         // XXX: Check for range.
-        self.to_int().and_then(|x| Some(x as i32))
+        self.to_i64().and_then(|x| Some(x as i32))
     }
 
     /// Converts the value of `self` to an `i64`.
+    fn to_i64(&self) -> Option<i64>;
+
+    /// Converts the value of `self` to an `uint`.
     #[inline]
-    fn to_i64(&self) -> Option<i64> {
+    fn to_uint(&self) -> Option<uint> {
         // XXX: Check for range.
-        self.to_int().and_then(|x| Some(x as i64))
+        self.to_u64().and_then(|x| Some(x as uint))
     }
 
-    /// Converts the value of `self` to an `uint`.
-    fn to_uint(&self) -> Option<uint>;
-
     /// Converts the value of `self` to an `u8`.
     #[inline]
     fn to_u8(&self) -> Option<u8> {
         // XXX: Check for range.
-        self.to_uint().and_then(|x| Some(x as u8))
+        self.to_u64().and_then(|x| Some(x as u8))
     }
 
     /// Converts the value of `self` to an `u16`.
     #[inline]
     fn to_u16(&self) -> Option<u16> {
         // XXX: Check for range.
-        self.to_uint().and_then(|x| Some(x as u16))
+        self.to_u64().and_then(|x| Some(x as u16))
     }
 
     /// Converts the value of `self` to an `u32`.
     #[inline]
     fn to_u32(&self) -> Option<u32> {
         // XXX: Check for range.
-        self.to_uint().and_then(|x| Some(x as u32))
+        self.to_u64().and_then(|x| Some(x as u32))
     }
 
     /// Converts the value of `self` to an `u64`.
     #[inline]
     fn to_u64(&self) -> Option<u64> {
         // XXX: Check for range.
-        self.to_uint().and_then(|x| Some(x as u64))
+        self.to_u64().and_then(|x| Some(x as u64))
     }
 
     /// Converts the value of `self` to an `f32`.
@@ -423,7 +427,7 @@ pub trait ToPrimitive {
     #[inline]
     fn to_f64(&self) -> Option<f64> {
         // XXX: Check for range.
-        self.to_float().and_then(|x| Some(x as f64))
+        self.to_i64().and_then(|x| Some(x as f64))
     }
 }
 
@@ -467,80 +471,80 @@ impl_to_primitive!(float)
 pub trait FromPrimitive {
     /// Convert an `int` to return an optional value of this type. If the
     /// value cannot be represented by this value, the `None` is returned.
-    fn from_int(n: int) -> Option<Self>;
+    #[inline]
+    fn from_int(n: int) -> Option<Self> {
+        FromPrimitive::from_i64(n as i64)
+    }
 
     /// Convert an `i8` to return an optional value of this type. If the
     /// type cannot be represented by this value, the `None` is returned.
     #[inline]
     fn from_i8(n: i8) -> Option<Self> {
-        FromPrimitive::from_int(n as int)
+        FromPrimitive::from_i64(n as i64)
     }
 
     /// Convert an `i16` to return an optional value of this type. If the
     /// type cannot be represented by this value, the `None` is returned.
     #[inline]
     fn from_i16(n: i16) -> Option<Self> {
-        FromPrimitive::from_int(n as int)
+        FromPrimitive::from_i64(n as i64)
     }
 
     /// Convert an `i32` to return an optional value of this type. If the
     /// type cannot be represented by this value, the `None` is returned.
     #[inline]
     fn from_i32(n: i32) -> Option<Self> {
-        FromPrimitive::from_int(n as int)
+        FromPrimitive::from_i64(n as i64)
     }
 
     /// Convert an `i64` to return an optional value of this type. If the
     /// type cannot be represented by this value, the `None` is returned.
-    #[inline]
-    fn from_i64(n: i64) -> Option<Self> {
-        FromPrimitive::from_int(n as int)
-    }
+    fn from_i64(n: i64) -> Option<Self>;
 
     /// Convert an `uint` to return an optional value of this type. If the
     /// type cannot be represented by this value, the `None` is returned.
-    fn from_uint(n: uint) -> Option<Self>;
+    #[inline]
+    fn from_uint(n: uint) -> Option<Self> {
+        FromPrimitive::from_u64(n as u64)
+    }
 
     /// Convert an `u8` to return an optional value of this type. If the
     /// type cannot be represented by this value, the `None` is returned.
     #[inline]
     fn from_u8(n: u8) -> Option<Self> {
-        FromPrimitive::from_uint(n as uint)
+        FromPrimitive::from_u64(n as u64)
     }
 
     /// Convert an `u16` to return an optional value of this type. If the
     /// type cannot be represented by this value, the `None` is returned.
     #[inline]
     fn from_u16(n: u16) -> Option<Self> {
-        FromPrimitive::from_uint(n as uint)
+        FromPrimitive::from_u64(n as u64)
     }
 
     /// Convert an `u32` to return an optional value of this type. If the
     /// type cannot be represented by this value, the `None` is returned.
     #[inline]
     fn from_u32(n: u32) -> Option<Self> {
-        FromPrimitive::from_uint(n as uint)
+        FromPrimitive::from_u64(n as u64)
     }
 
     /// Convert an `u64` to return an optional value of this type. If the
     /// type cannot be represented by this value, the `None` is returned.
-    #[inline]
-    fn from_u64(n: u64) -> Option<Self> {
-        FromPrimitive::from_uint(n as uint)
-    }
+    fn from_u64(n: u64) -> Option<Self>;
 
     /// Convert a `f32` to return an optional value of this type. If the
     /// type cannot be represented by this value, the `None` is returned.
     #[inline]
     fn from_f32(n: f32) -> Option<Self> {
-        FromPrimitive::from_float(n as float)
+        FromPrimitive::from_f64(n as f64)
     }
 
     /// Convert a `f64` to return an optional value of this type. If the
     /// type cannot be represented by this value, the `None` is returned.
     #[inline]
     fn from_f64(n: f64) -> Option<Self> {
-        FromPrimitive::from_float(n as float)
+        FromPrimitive::from_i64(n as i64)
     }
 }