@@ -1373,10 +1373,18 @@ macro_rules! to_primitive_small {
13731373 self . to_integer( ) . to_i64( )
13741374 }
13751375
1376+ fn to_i128( & self ) -> Option <i128 > {
1377+ self . to_integer( ) . to_i128( )
1378+ }
1379+
13761380 fn to_u64( & self ) -> Option <u64 > {
13771381 self . to_integer( ) . to_u64( )
13781382 }
13791383
1384+ fn to_u128( & self ) -> Option <u128 > {
1385+ self . to_integer( ) . to_u128( )
1386+ }
1387+
13801388 fn to_f64( & self ) -> Option <f64 > {
13811389 Some ( self . numer. to_f64( ) . unwrap( ) / self . denom. to_f64( ) . unwrap( ) )
13821390 }
@@ -1387,6 +1395,9 @@ macro_rules! to_primitive_small {
13871395#[ cfg( not( all( feature = "bigint" , feature = "std" ) ) ) ]
13881396to_primitive_small ! ( u8 i8 u16 i16 u32 i32 ) ;
13891397
1398+ #[ cfg( all( target_pointer_width = "32" , not( all( feature = "bigint" , feature = "std" ) ) ) ) ]
1399+ to_primitive_small ! ( usize isize ) ;
1400+
13901401#[ cfg( all( feature = "std" , not( feature = "bigint" ) ) ) ]
13911402macro_rules! to_primitive_64 {
13921403 ( $( $type_name: ty) * ) => ( $(
@@ -1395,22 +1406,33 @@ macro_rules! to_primitive_64 {
13951406 self . to_integer( ) . to_i64( )
13961407 }
13971408
1409+ fn to_i128( & self ) -> Option <i128 > {
1410+ self . to_integer( ) . to_i128( )
1411+ }
1412+
13981413 fn to_u64( & self ) -> Option <u64 > {
13991414 self . to_integer( ) . to_u64( )
14001415 }
14011416
1417+ fn to_u128( & self ) -> Option <u128 > {
1418+ self . to_integer( ) . to_u128( )
1419+ }
1420+
14021421 fn to_f64( & self ) -> Option <f64 > {
14031422 Some ( ratio_to_f64(
1404- < i128 as From <$type_name>> :: from ( self . numer) ,
1405- < i128 as From <$type_name>> :: from ( self . denom)
1423+ self . numer as i128 ,
1424+ self . denom as i128
14061425 ) )
14071426 }
14081427 }
14091428 ) * )
14101429}
14111430
14121431#[ cfg( all( feature = "std" , not( feature = "bigint" ) ) ) ]
1413- to_primitive_64 ! ( i64 u64 ) ;
1432+ to_primitive_64 ! ( u64 i64 ) ;
1433+
1434+ #[ cfg( all( target_pointer_width = "64" , feature = "std" , not( feature = "bigint" ) ) ) ]
1435+ to_primitive_64 ! ( usize isize ) ;
14141436
14151437#[ cfg( all( feature = "bigint" , feature = "std" ) ) ]
14161438impl < T : Clone + Integer + ToPrimitive + ToBigInt > ToPrimitive for Ratio < T > {
@@ -1431,10 +1453,15 @@ impl<T: Clone + Integer + ToPrimitive + ToBigInt> ToPrimitive for Ratio<T> {
14311453 }
14321454
14331455 fn to_f64 ( & self ) -> Option < f64 > {
1434- let numer: BigInt = self . numer . to_bigint ( ) ?;
1435- let denom: BigInt = self . denom . to_bigint ( ) ?;
1436-
1437- Some ( ratio_to_f64 ( numer, denom) )
1456+ match ( self . numer . to_i64 ( ) , self . denom . to_i64 ( ) ) {
1457+ ( Some ( numer) , Some ( denom) ) =>
1458+ Some ( ratio_to_f64 ( <i128 as From < _ > >:: from ( numer) , <i128 as From < _ > >:: from ( denom) ) ) ,
1459+ _ => {
1460+ let numer: BigInt = self . numer . to_bigint ( ) ?;
1461+ let denom: BigInt = self . denom . to_bigint ( ) ?;
1462+ Some ( ratio_to_f64 ( numer, denom) )
1463+ }
1464+ }
14381465 }
14391466}
14401467
@@ -1450,16 +1477,10 @@ impl Bits for BigInt {
14501477 }
14511478}
14521479
1453- #[ cfg( all ( feature = "std" , not ( feature = "bigint" ) ) ) ]
1480+ #[ cfg( feature = "std" ) ]
14541481impl Bits for i128 {
14551482 fn bits ( & self ) -> usize {
1456- let mut bit_count = 0 ;
1457- let mut remainder = * self ;
1458- while remainder > 0 {
1459- remainder >>= 1 ;
1460- bit_count += 1 ;
1461- }
1462- bit_count
1483+ ( 128 - self . wrapping_abs ( ) . leading_zeros ( ) ) as usize
14631484 }
14641485}
14651486
0 commit comments