From 9de7ad2d8c1729b7b11c6d234fc8ef8ce96809bb Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Tue, 17 Sep 2013 19:28:35 -0700 Subject: 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. --- src/libstd/num/num.rs | 68 +++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 32 deletions(-) (limited to 'src/libstd/num') 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; + #[inline] + fn to_int(&self) -> Option { + // 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 { // 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 { // 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 { // 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; + + /// Converts the value of `self` to an `uint`. #[inline] - fn to_i64(&self) -> Option { + fn to_uint(&self) -> Option { // 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; - /// Converts the value of `self` to an `u8`. #[inline] fn to_u8(&self) -> Option { // 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 { // 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 { // 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 { // 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 { // 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; + #[inline] + fn from_int(n: int) -> Option { + 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 { - 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 { - 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 { - 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 { - FromPrimitive::from_int(n as int) - } + fn from_i64(n: i64) -> Option; /// 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; + #[inline] + fn from_uint(n: uint) -> Option { + 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 { - 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 { - 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 { - 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 { - FromPrimitive::from_uint(n as uint) - } + fn from_u64(n: u64) -> Option; /// 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 { - 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 { - FromPrimitive::from_float(n as float) + FromPrimitive::from_i64(n as i64) } } -- cgit 1.4.1-3-g733a5