@@ -57,8 +57,8 @@ impl TryFrom<PgNumeric> for BigDecimal {
5757 // no optimized algorithm for base-10 so use base-100 for faster processing
5858 let mut cents = Vec :: with_capacity ( digits. len ( ) * 2 ) ;
5959 for digit in & digits {
60- cents. push ( ( digit / 100 ) as u8 ) ;
61- cents. push ( ( digit % 100 ) as u8 ) ;
60+ cents. push ( u8 :: try_from ( digit / 100 ) . unwrap ( ) ) ;
61+ cents. push ( u8 :: try_from ( digit % 100 ) . unwrap ( ) ) ;
6262 }
6363
6464 let bigint = BigInt :: from_radix_be ( sign, & cents, 100 )
@@ -83,7 +83,7 @@ impl TryFrom<&'_ BigDecimal> for PgNumeric {
8383
8484 // weight is positive power of 10000
8585 // exp is the negative power of 10
86- let weight_10 = base_10. len ( ) as i64 - exp;
86+ let weight_10 = i64 :: try_from ( base_10. len ( ) ) ? - exp;
8787
8888 // scale is only nonzero when we have fractional digits
8989 // since `exp` is the _negative_ decimal exponent, it tells us
@@ -105,7 +105,7 @@ impl TryFrom<&'_ BigDecimal> for PgNumeric {
105105 base_10. len ( ) / 4
106106 } ;
107107
108- let offset = weight_10. rem_euclid ( 4 ) as usize ;
108+ let offset = usize :: try_from ( weight_10. rem_euclid ( 4 ) ) ? ;
109109
110110 let mut digits = Vec :: with_capacity ( digits_len) ;
111111
@@ -114,14 +114,16 @@ impl TryFrom<&'_ BigDecimal> for PgNumeric {
114114 digits. push ( base_10_to_10000 ( first) ) ;
115115 }
116116 } else if offset != 0 {
117- digits. push ( base_10_to_10000 ( & base_10) * 10i16 . pow ( ( offset - base_10. len ( ) ) as u32 ) ) ;
117+ digits. push (
118+ base_10_to_10000 ( & base_10) * 10i16 . pow ( u32:: try_from ( offset - base_10. len ( ) ) ?) ,
119+ ) ;
118120 }
119121
120122 if let Some ( rest) = base_10. get ( offset..) {
121- digits. extend (
122- rest . chunks ( 4 )
123- . map ( |chunk| base_10_to_10000 ( chunk ) * 10i16 . pow ( 4 - chunk. len ( ) as u32 ) ) ,
124- ) ;
123+ digits. extend ( rest . chunks ( 4 ) . map ( |chunk| {
124+ base_10_to_10000 ( chunk )
125+ * 10i16 . pow ( 4 - u32 :: try_from ( chunk. len ( ) ) . unwrap_or ( u32:: MAX ) )
126+ } ) ) ;
125127 }
126128
127129 while let Some ( & 0 ) = digits. last ( ) {
@@ -154,7 +156,7 @@ impl Encode<'_, Postgres> for BigDecimal {
154156 fn size_hint ( & self ) -> usize {
155157 // BigDecimal::digits() gives us base-10 digits, so we divide by 4 to get base-10000 digits
156158 // and since this is just a hint we just always round up
157- 8 + ( self . digits ( ) / 4 + 1 ) as usize * 2
159+ 8 + usize :: try_from ( self . digits ( ) / 4 + 1 ) . unwrap ( ) * 2
158160 }
159161}
160162
0 commit comments