about summary refs log tree commit diff
diff options
context:
space:
mode:
authorErick Tryzelaar <erick.tryzelaar@gmail.com>2013-09-27 17:09:18 -0700
committerErick Tryzelaar <erick.tryzelaar@gmail.com>2013-10-05 13:59:06 -0700
commit50fde8c024a30d01ed54a2d40eab7399bf1e7a3c (patch)
tree08314cfcef42bbf843fc4fb0ae2ec08ae4643fe4
parentda145b237241345a9b14531d37f290082c4fb5f0 (diff)
downloadrust-50fde8c024a30d01ed54a2d40eab7399bf1e7a3c.tar.gz
rust-50fde8c024a30d01ed54a2d40eab7399bf1e7a3c.zip
std: ToPrimitive's default impls should use `.to_*()`
This allows the default methods to be properly range checked.
-rw-r--r--src/libstd/num/num.rs33
1 files changed, 11 insertions, 22 deletions
diff --git a/src/libstd/num/num.rs b/src/libstd/num/num.rs
index f2160b3735c..bccb20de458 100644
--- a/src/libstd/num/num.rs
+++ b/src/libstd/num/num.rs
@@ -353,29 +353,25 @@ pub trait ToPrimitive {
     /// Converts the value of `self` to an `int`.
     #[inline]
     fn to_int(&self) -> Option<int> {
-        // XXX: Check for range.
-        self.to_i64().and_then(|x| Some(x as int))
+        self.to_i64().and_then(|x| x.to_int())
     }
 
     /// Converts the value of `self` to an `i8`.
     #[inline]
     fn to_i8(&self) -> Option<i8> {
-        // XXX: Check for range.
-        self.to_i64().and_then(|x| Some(x as i8))
+        self.to_i64().and_then(|x| x.to_i8())
     }
 
     /// Converts the value of `self` to an `i16`.
     #[inline]
     fn to_i16(&self) -> Option<i16> {
-        // XXX: Check for range.
-        self.to_i64().and_then(|x| Some(x as i16))
+        self.to_i64().and_then(|x| x.to_i16())
     }
 
     /// Converts the value of `self` to an `i32`.
     #[inline]
     fn to_i32(&self) -> Option<i32> {
-        // XXX: Check for range.
-        self.to_i64().and_then(|x| Some(x as i32))
+        self.to_i64().and_then(|x| x.to_i32())
     }
 
     /// Converts the value of `self` to an `i64`.
@@ -384,50 +380,43 @@ pub trait ToPrimitive {
     /// Converts the value of `self` to an `uint`.
     #[inline]
     fn to_uint(&self) -> Option<uint> {
-        // XXX: Check for range.
-        self.to_u64().and_then(|x| Some(x as uint))
+        self.to_u64().and_then(|x| x.to_uint())
     }
 
     /// Converts the value of `self` to an `u8`.
     #[inline]
     fn to_u8(&self) -> Option<u8> {
-        // XXX: Check for range.
-        self.to_u64().and_then(|x| Some(x as u8))
+        self.to_u64().and_then(|x| x.to_u8())
     }
 
     /// Converts the value of `self` to an `u16`.
     #[inline]
     fn to_u16(&self) -> Option<u16> {
-        // XXX: Check for range.
-        self.to_u64().and_then(|x| Some(x as u16))
+        self.to_u64().and_then(|x| x.to_u16())
     }
 
     /// Converts the value of `self` to an `u32`.
     #[inline]
     fn to_u32(&self) -> Option<u32> {
-        // XXX: Check for range.
-        self.to_u64().and_then(|x| Some(x as u32))
+        self.to_u64().and_then(|x| x.to_u32())
     }
 
     /// Converts the value of `self` to an `u64`.
     #[inline]
     fn to_u64(&self) -> Option<u64> {
-        // XXX: Check for range.
-        self.to_u64().and_then(|x| Some(x as u64))
+        self.to_u64().and_then(|x| x.to_u64())
     }
 
     /// Converts the value of `self` to an `f32`.
     #[inline]
     fn to_f32(&self) -> Option<f32> {
-        // XXX: Check for range.
-        self.to_float().and_then(|x| Some(x as f32))
+        self.to_f64().and_then(|x| x.to_f32())
     }
 
     /// Converts the value of `self` to an `f64`.
     #[inline]
     fn to_f64(&self) -> Option<f64> {
-        // XXX: Check for range.
-        self.to_i64().and_then(|x| Some(x as f64))
+        self.to_i64().and_then(|x| x.to_f64())
     }
 }