@@ -1105,27 +1105,47 @@ impl ToPrimitive for BigUint {
1105
1105
impl FromPrimitive for BigUint {
1106
1106
#[ inline]
1107
1107
fn from_i64 ( n : i64 ) -> Option < BigUint > {
1108
- if n > 0 {
1109
- FromPrimitive :: from_u64 ( n as u64 )
1110
- } else if n == 0 {
1111
- Some ( Zero :: zero ( ) )
1108
+ if n >= 0 {
1109
+ Some ( BigUint :: from ( n as u64 ) )
1112
1110
} else {
1113
1111
None
1114
1112
}
1115
1113
}
1116
1114
1117
- // `DoubleBigDigit` size dependent
1118
1115
#[ inline]
1119
1116
fn from_u64 ( n : u64 ) -> Option < BigUint > {
1120
- let n = match big_digit:: from_doublebigdigit ( n) {
1121
- ( 0 , 0 ) => Zero :: zero ( ) ,
1122
- ( 0 , n0) => BigUint :: new ( vec ! ( n0) ) ,
1123
- ( n1, n0) => BigUint :: new ( vec ! ( n0, n1) )
1124
- } ;
1125
- Some ( n)
1117
+ Some ( BigUint :: from ( n) )
1126
1118
}
1127
1119
}
1128
1120
1121
+ impl From < u64 > for BigUint {
1122
+ // `DoubleBigDigit` size dependent
1123
+ #[ inline]
1124
+ fn from ( n : u64 ) -> Self {
1125
+ match big_digit:: from_doublebigdigit ( n) {
1126
+ ( 0 , 0 ) => BigUint :: zero ( ) ,
1127
+ ( 0 , n0) => BigUint { data : vec ! [ n0] } ,
1128
+ ( n1, n0) => BigUint { data : vec ! [ n0, n1] } ,
1129
+ }
1130
+ }
1131
+ }
1132
+
1133
+ macro_rules! impl_biguint_from_uint {
1134
+ ( $T: ty) => {
1135
+ impl From <$T> for BigUint {
1136
+ #[ inline]
1137
+ fn from( n: $T) -> Self {
1138
+ BigUint :: from( n as u64 )
1139
+ }
1140
+ }
1141
+ }
1142
+ }
1143
+
1144
+ impl_biguint_from_uint ! ( u8 ) ;
1145
+ impl_biguint_from_uint ! ( u16 ) ;
1146
+ impl_biguint_from_uint ! ( u32 ) ;
1147
+ impl_biguint_from_uint ! ( usize ) ;
1148
+
1129
1149
/// A generic trait for converting a value to a `BigUint`.
1130
1150
pub trait ToBigUint {
1131
1151
/// Converts the value of `self` to a `BigUint`.
@@ -1973,24 +1993,77 @@ impl ToPrimitive for BigInt {
1973
1993
impl FromPrimitive for BigInt {
1974
1994
#[ inline]
1975
1995
fn from_i64 ( n : i64 ) -> Option < BigInt > {
1996
+ Some ( BigInt :: from ( n) )
1997
+ }
1998
+
1999
+ #[ inline]
2000
+ fn from_u64 ( n : u64 ) -> Option < BigInt > {
2001
+ Some ( BigInt :: from ( n) )
2002
+ }
2003
+ }
2004
+
2005
+ impl From < i64 > for BigInt {
2006
+ #[ inline]
2007
+ fn from ( n : i64 ) -> Self {
1976
2008
if n >= 0 {
1977
- FromPrimitive :: from_u64 ( n as u64 )
2009
+ BigInt :: from ( n as u64 )
1978
2010
} else {
1979
2011
let u = u64:: MAX - ( n as u64 ) + 1 ;
1980
- FromPrimitive :: from_u64 ( u) . map ( |n| {
1981
- BigInt :: from_biguint ( Minus , n)
1982
- } )
2012
+ BigInt { sign : Minus , data : BigUint :: from ( u) }
1983
2013
}
1984
2014
}
2015
+ }
1985
2016
2017
+ macro_rules! impl_bigint_from_int {
2018
+ ( $T: ty) => {
2019
+ impl From <$T> for BigInt {
2020
+ #[ inline]
2021
+ fn from( n: $T) -> Self {
2022
+ BigInt :: from( n as i64 )
2023
+ }
2024
+ }
2025
+ }
2026
+ }
2027
+
2028
+ impl_bigint_from_int ! ( i8 ) ;
2029
+ impl_bigint_from_int ! ( i16 ) ;
2030
+ impl_bigint_from_int ! ( i32 ) ;
2031
+ impl_bigint_from_int ! ( isize ) ;
2032
+
2033
+ impl From < u64 > for BigInt {
1986
2034
#[ inline]
1987
- fn from_u64 ( n : u64 ) -> Option < BigInt > {
1988
- if n == 0 {
1989
- Some ( Zero :: zero ( ) )
2035
+ fn from ( n : u64 ) -> Self {
2036
+ if n > 0 {
2037
+ BigInt { sign : Plus , data : BigUint :: from ( n) }
2038
+ } else {
2039
+ BigInt :: zero ( )
2040
+ }
2041
+ }
2042
+ }
2043
+
2044
+ macro_rules! impl_bigint_from_uint {
2045
+ ( $T: ty) => {
2046
+ impl From <$T> for BigInt {
2047
+ #[ inline]
2048
+ fn from( n: $T) -> Self {
2049
+ BigInt :: from( n as u64 )
2050
+ }
2051
+ }
2052
+ }
2053
+ }
2054
+
2055
+ impl_bigint_from_uint ! ( u8 ) ;
2056
+ impl_bigint_from_uint ! ( u16 ) ;
2057
+ impl_bigint_from_uint ! ( u32 ) ;
2058
+ impl_bigint_from_uint ! ( usize ) ;
2059
+
2060
+ impl From < BigUint > for BigInt {
2061
+ #[ inline]
2062
+ fn from ( n : BigUint ) -> Self {
2063
+ if n. is_zero ( ) {
2064
+ BigInt :: zero ( )
1990
2065
} else {
1991
- FromPrimitive :: from_u64 ( n) . map ( |n| {
1992
- BigInt :: from_biguint ( Plus , n)
1993
- } )
2066
+ BigInt { sign : Plus , data : n }
1994
2067
}
1995
2068
}
1996
2069
}
0 commit comments