@@ -404,9 +404,7 @@ pub trait ToPrimitive {
404
404
405
405
/// Converts the value of `self` to an `u64`.
406
406
#[ 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 > ;
410
408
411
409
/// Converts the value of `self` to an `f32`.
412
410
#[ inline]
@@ -1481,4 +1479,51 @@ mod tests {
1481
1479
assert_eq ! ( third. checked_mul( & 3 ) , Some ( third * 3 ) ) ;
1482
1480
assert_eq ! ( third. checked_mul( & 4 ) , None ) ;
1483
1481
}
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
+ }
1484
1529
}
0 commit comments