@@ -13,7 +13,9 @@ use crate::{
1313 __internal:: forward_ref_partial_eq,
1414} ;
1515
16- use super :: conversion:: { forward_try_from, try_from_int_to_int} ;
16+ use super :: conversion:: {
17+ forward_try_from, primitive_to_wrapped_int, try_from_int_to_int, wrapped_int_to_primitive,
18+ } ;
1719use super :: impl_int_serde;
1820use super :: num_consts:: NumConsts ;
1921
@@ -306,29 +308,10 @@ forward_try_from!(Uint256, Int128);
306308forward_try_from ! ( Uint512 , Int128 ) ;
307309
308310// uint to Int
309- impl From < u64 > for Int128 {
310- fn from ( val : u64 ) -> Self {
311- Int128 ( val. into ( ) )
312- }
313- }
314-
315- impl From < u32 > for Int128 {
316- fn from ( val : u32 ) -> Self {
317- Int128 ( val. into ( ) )
318- }
319- }
320-
321- impl From < u16 > for Int128 {
322- fn from ( val : u16 ) -> Self {
323- Int128 ( val. into ( ) )
324- }
325- }
326-
327- impl From < u8 > for Int128 {
328- fn from ( val : u8 ) -> Self {
329- Int128 ( val. into ( ) )
330- }
331- }
311+ primitive_to_wrapped_int ! ( u8 , Int128 ) ;
312+ primitive_to_wrapped_int ! ( u16 , Int128 ) ;
313+ primitive_to_wrapped_int ! ( u32 , Int128 ) ;
314+ primitive_to_wrapped_int ! ( u64 , Int128 ) ;
332315
333316// Int to Int
334317impl From < Int64 > for Int128 {
@@ -341,35 +324,14 @@ try_from_int_to_int!(Int256, Int128);
341324try_from_int_to_int ! ( Int512 , Int128 ) ;
342325
343326// int to Int
344- impl From < i128 > for Int128 {
345- fn from ( val : i128 ) -> Self {
346- Int128 ( val )
347- }
348- }
327+ primitive_to_wrapped_int ! ( i8 , Int128 ) ;
328+ primitive_to_wrapped_int ! ( i16 , Int128 ) ;
329+ primitive_to_wrapped_int ! ( i32 , Int128 ) ;
330+ primitive_to_wrapped_int ! ( i64 , Int128 ) ;
331+ primitive_to_wrapped_int ! ( i128 , Int128 ) ;
349332
350- impl From < i64 > for Int128 {
351- fn from ( val : i64 ) -> Self {
352- Int128 ( val. into ( ) )
353- }
354- }
355-
356- impl From < i32 > for Int128 {
357- fn from ( val : i32 ) -> Self {
358- Int128 ( val. into ( ) )
359- }
360- }
361-
362- impl From < i16 > for Int128 {
363- fn from ( val : i16 ) -> Self {
364- Int128 ( val. into ( ) )
365- }
366- }
367-
368- impl From < i8 > for Int128 {
369- fn from ( val : i8 ) -> Self {
370- Int128 ( val. into ( ) )
371- }
372- }
333+ // Int to int
334+ wrapped_int_to_primitive ! ( Int128 , i128 ) ;
373335
374336impl TryFrom < & str > for Int128 {
375337 type Error = StdError ;
@@ -626,6 +588,12 @@ mod tests {
626588 assert_eq ! ( num1, num2) ;
627589 }
628590
591+ #[ test]
592+ fn int128_convert_to ( ) {
593+ let a = Int128 :: new ( 5 ) ;
594+ assert_eq ! ( i128 :: from( a) , 5 ) ;
595+ }
596+
629597 #[ test]
630598 fn int128_convert_from ( ) {
631599 let a = Int128 :: from ( 5i128 ) ;
0 commit comments