diff options
| author | bors <bors@rust-lang.org> | 2013-10-08 11:06:41 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-10-08 11:06:41 -0700 |
| commit | e42e32291ea779a30818521e8aea9693899b8188 (patch) | |
| tree | e03062fc4e8d6d8c674e171bdbde8bde2bc3ef49 /src | |
| parent | 8db52a5c0eff35a87007533d57127e7afd91fb24 (diff) | |
| parent | 6dfc5d5de1c933a030618e6dcf20a1c3027fe846 (diff) | |
| download | rust-e42e32291ea779a30818521e8aea9693899b8188.tar.gz rust-e42e32291ea779a30818521e8aea9693899b8188.zip | |
auto merge of #9757 : erickt/rust/master, r=alexcrichton
I accidentally left an infinite loop in a default method in `num::ToPrimitive::to_u64()`. This fixes it.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libstd/num/num.rs | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/src/libstd/num/num.rs b/src/libstd/num/num.rs index fde1928f4a3..379e874f862 100644 --- a/src/libstd/num/num.rs +++ b/src/libstd/num/num.rs @@ -404,9 +404,7 @@ pub trait ToPrimitive { /// Converts the value of `self` to an `u64`. #[inline] - fn to_u64(&self) -> Option<u64> { - self.to_u64().and_then(|x| x.to_u64()) - } + fn to_u64(&self) -> Option<u64>; /// Converts the value of `self` to an `f32`. #[inline] @@ -1481,4 +1479,51 @@ mod tests { assert_eq!(third.checked_mul(&3), Some(third * 3)); assert_eq!(third.checked_mul(&4), None); } + + + #[deriving(Eq)] + struct Value { x: int } + + impl ToPrimitive for Value { + fn to_i64(&self) -> Option<i64> { self.x.to_i64() } + fn to_u64(&self) -> Option<u64> { self.x.to_u64() } + } + + impl FromPrimitive for Value { + fn from_i64(n: i64) -> Option<Value> { Some(Value { x: n as int }) } + fn from_u64(n: u64) -> Option<Value> { Some(Value { x: n as int }) } + } + + #[test] + fn test_to_primitive() { + let value = Value { x: 5 }; + assert_eq!(value.to_int(), Some(5)); + assert_eq!(value.to_i8(), Some(5)); + assert_eq!(value.to_i16(), Some(5)); + assert_eq!(value.to_i32(), Some(5)); + assert_eq!(value.to_i64(), Some(5)); + assert_eq!(value.to_uint(), Some(5)); + assert_eq!(value.to_u8(), Some(5)); + assert_eq!(value.to_u16(), Some(5)); + assert_eq!(value.to_u32(), Some(5)); + assert_eq!(value.to_u64(), Some(5)); + assert_eq!(value.to_f32(), Some(5f32)); + assert_eq!(value.to_f64(), Some(5f64)); + } + + #[test] + fn test_from_primitive() { + assert_eq!(from_int(5), Some(Value { x: 5 })); + assert_eq!(from_i8(5), Some(Value { x: 5 })); + assert_eq!(from_i16(5), Some(Value { x: 5 })); + assert_eq!(from_i32(5), Some(Value { x: 5 })); + assert_eq!(from_i64(5), Some(Value { x: 5 })); + assert_eq!(from_uint(5), Some(Value { x: 5 })); + assert_eq!(from_u8(5), Some(Value { x: 5 })); + assert_eq!(from_u16(5), Some(Value { x: 5 })); + assert_eq!(from_u32(5), Some(Value { x: 5 })); + assert_eq!(from_u64(5), Some(Value { x: 5 })); + assert_eq!(from_f32(5f32), Some(Value { x: 5 })); + assert_eq!(from_f64(5f64), Some(Value { x: 5 })); + } } |
