@@ -20,7 +20,7 @@ use crate::errors::{
20
20
py_err_string, ErrorType , ErrorTypeDefaults , InputValue , ToErrorValue , ValError , ValLineError , ValResult ,
21
21
} ;
22
22
use crate :: py_gc:: PyGcTraverse ;
23
- use crate :: tools:: { extract_i64, new_py_string, py_err} ;
23
+ use crate :: tools:: { extract_i64, extract_int , new_py_string, py_err} ;
24
24
use crate :: validators:: { CombinedValidator , Exactness , ValidationState , Validator } ;
25
25
26
26
use super :: { py_error_on_minusone, BorrowInput , Input } ;
@@ -662,6 +662,15 @@ pub enum Int {
662
662
Big ( BigInt ) ,
663
663
}
664
664
665
+ impl IntoPy < PyObject > for Int {
666
+ fn into_py ( self , py : Python < ' _ > ) -> PyObject {
667
+ match self {
668
+ Self :: I64 ( i) => i. into_py ( py) ,
669
+ Self :: Big ( big_i) => big_i. into_py ( py) ,
670
+ }
671
+ }
672
+ }
673
+
665
674
// The default serialization for BigInt is some internal representation which roundtrips efficiently
666
675
// but is not the JSON value which users would expect to see.
667
676
fn serialize_bigint_as_number < S > ( big_int : & BigInt , serializer : S ) -> Result < S :: Ok , S :: Error >
@@ -706,12 +715,9 @@ impl<'a> Rem for &'a Int {
706
715
707
716
impl FromPyObject < ' _ > for Int {
708
717
fn extract_bound ( obj : & Bound < ' _ , PyAny > ) -> PyResult < Self > {
709
- if let Some ( i) = extract_i64 ( obj) {
710
- Ok ( Int :: I64 ( i) )
711
- } else if let Ok ( b) = obj. extract :: < BigInt > ( ) {
712
- Ok ( Int :: Big ( b) )
713
- } else {
714
- py_err ! ( PyTypeError ; "Expected int, got {}" , obj. get_type( ) )
718
+ match extract_int ( obj) {
719
+ Some ( i) => Ok ( i) ,
720
+ None => py_err ! ( PyTypeError ; "Expected int, got {}" , obj. get_type( ) ) ,
715
721
}
716
722
}
717
723
}
0 commit comments