Skip to content

Commit e42e322

Browse files
committed
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.
2 parents 8db52a5 + 6dfc5d5 commit e42e322

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

src/libstd/num/num.rs

+48-3
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,7 @@ pub trait ToPrimitive {
404404

405405
/// Converts the value of `self` to an `u64`.
406406
#[inline]
407-
fn to_u64(&self) -> Option<u64> {
408-
self.to_u64().and_then(|x| x.to_u64())
409-
}
407+
fn to_u64(&self) -> Option<u64>;
410408

411409
/// Converts the value of `self` to an `f32`.
412410
#[inline]
@@ -1481,4 +1479,51 @@ mod tests {
14811479
assert_eq!(third.checked_mul(&3), Some(third * 3));
14821480
assert_eq!(third.checked_mul(&4), None);
14831481
}
1482+
1483+
1484+
#[deriving(Eq)]
1485+
struct Value { x: int }
1486+
1487+
impl ToPrimitive for Value {
1488+
fn to_i64(&self) -> Option<i64> { self.x.to_i64() }
1489+
fn to_u64(&self) -> Option<u64> { self.x.to_u64() }
1490+
}
1491+
1492+
impl FromPrimitive for Value {
1493+
fn from_i64(n: i64) -> Option<Value> { Some(Value { x: n as int }) }
1494+
fn from_u64(n: u64) -> Option<Value> { Some(Value { x: n as int }) }
1495+
}
1496+
1497+
#[test]
1498+
fn test_to_primitive() {
1499+
let value = Value { x: 5 };
1500+
assert_eq!(value.to_int(), Some(5));
1501+
assert_eq!(value.to_i8(), Some(5));
1502+
assert_eq!(value.to_i16(), Some(5));
1503+
assert_eq!(value.to_i32(), Some(5));
1504+
assert_eq!(value.to_i64(), Some(5));
1505+
assert_eq!(value.to_uint(), Some(5));
1506+
assert_eq!(value.to_u8(), Some(5));
1507+
assert_eq!(value.to_u16(), Some(5));
1508+
assert_eq!(value.to_u32(), Some(5));
1509+
assert_eq!(value.to_u64(), Some(5));
1510+
assert_eq!(value.to_f32(), Some(5f32));
1511+
assert_eq!(value.to_f64(), Some(5f64));
1512+
}
1513+
1514+
#[test]
1515+
fn test_from_primitive() {
1516+
assert_eq!(from_int(5), Some(Value { x: 5 }));
1517+
assert_eq!(from_i8(5), Some(Value { x: 5 }));
1518+
assert_eq!(from_i16(5), Some(Value { x: 5 }));
1519+
assert_eq!(from_i32(5), Some(Value { x: 5 }));
1520+
assert_eq!(from_i64(5), Some(Value { x: 5 }));
1521+
assert_eq!(from_uint(5), Some(Value { x: 5 }));
1522+
assert_eq!(from_u8(5), Some(Value { x: 5 }));
1523+
assert_eq!(from_u16(5), Some(Value { x: 5 }));
1524+
assert_eq!(from_u32(5), Some(Value { x: 5 }));
1525+
assert_eq!(from_u64(5), Some(Value { x: 5 }));
1526+
assert_eq!(from_f32(5f32), Some(Value { x: 5 }));
1527+
assert_eq!(from_f64(5f64), Some(Value { x: 5 }));
1528+
}
14841529
}

0 commit comments

Comments
 (0)