@@ -6,7 +6,7 @@ use crate::{
6
6
exceptions, ffi, AsPyPointer , FromPyObject , IntoPy , PyAny , PyErr , PyNativeType , PyObject ,
7
7
PyResult , Python , ToPyObject ,
8
8
} ;
9
- use num_traits :: cast :: cast ;
9
+ use std :: convert :: TryFrom ;
10
10
use std:: i64;
11
11
use std:: os:: raw:: { c_int, c_long, c_uchar} ;
12
12
@@ -39,10 +39,7 @@ macro_rules! int_fits_larger_int {
39
39
impl <' source> FromPyObject <' source> for $rust_type {
40
40
fn extract( obj: & ' source PyAny ) -> PyResult <Self > {
41
41
let val = $crate:: objectprotocol:: ObjectProtocol :: extract:: <$larger_type>( obj) ?;
42
- match cast:: <$larger_type, $rust_type>( val) {
43
- Some ( v) => Ok ( v) ,
44
- None => Err ( exceptions:: OverflowError . into( ) ) ,
45
- }
42
+ <$rust_type>:: try_from( val) . map_err( |_| exceptions:: OverflowError . into( ) )
46
43
}
47
44
}
48
45
} ;
@@ -146,10 +143,7 @@ macro_rules! int_fits_c_long {
146
143
val
147
144
}
148
145
} ?;
149
- match cast:: <c_long, $rust_type>( val) {
150
- Some ( v) => Ok ( v) ,
151
- None => Err ( exceptions:: OverflowError . into( ) ) ,
152
- }
146
+ <$rust_type>:: try_from( val) . map_err( |_| exceptions:: OverflowError . into( ) )
153
147
}
154
148
}
155
149
} ;
@@ -322,7 +316,6 @@ mod bigint_conversion {
322
316
use super :: * ;
323
317
use crate :: types:: { PyDict , PyModule } ;
324
318
use indoc:: indoc;
325
- use num_traits:: { One , Zero } ;
326
319
327
320
fn python_fib ( py : Python ) -> & PyModule {
328
321
let fib_code = indoc ! (
@@ -342,11 +335,11 @@ mod bigint_conversion {
342
335
343
336
fn rust_fib < T > ( n : usize ) -> T
344
337
where
345
- T : Zero + One ,
338
+ T : From < u16 > ,
346
339
for < ' a > & ' a T : std:: ops:: Add < Output = T > ,
347
340
{
348
- let mut f0: T = Zero :: zero ( ) ;
349
- let mut f1: T = One :: one ( ) ;
341
+ let mut f0: T = T :: from ( 0 ) ;
342
+ let mut f1: T = T :: from ( 1 ) ;
350
343
for _ in 0 ..n {
351
344
let f2 = & f0 + & f1;
352
345
f0 = std:: mem:: replace ( & mut f1, f2) ;
@@ -428,15 +421,15 @@ mod bigint_conversion {
428
421
test ! ( BigInt , BigInt :: from( i) , py) ;
429
422
test ! ( BigUint , BigUint :: from( i) , py) ;
430
423
test ! ( BigInt , -BigInt :: from( i) , py) ;
431
- test ! ( BigInt , BigInt :: one ( ) << i, py) ;
432
- test ! ( BigUint , BigUint :: one ( ) << i, py) ;
433
- test ! ( BigInt , -BigInt :: one ( ) << i, py) ;
434
- test ! ( BigInt , ( BigInt :: one ( ) << i) + 1u32 , py) ;
435
- test ! ( BigUint , ( BigUint :: one ( ) << i) + 1u32 , py) ;
436
- test ! ( BigInt , ( -BigInt :: one ( ) << i) + 1u32 , py) ;
437
- test ! ( BigInt , ( BigInt :: one ( ) << i) - 1u32 , py) ;
438
- test ! ( BigUint , ( BigUint :: one ( ) << i) - 1u32 , py) ;
439
- test ! ( BigInt , ( -BigInt :: one ( ) << i) - 1u32 , py) ;
424
+ test ! ( BigInt , BigInt :: from ( 1 ) << i, py) ;
425
+ test ! ( BigUint , BigUint :: from ( 1u32 ) << i, py) ;
426
+ test ! ( BigInt , -BigInt :: from ( 1 ) << i, py) ;
427
+ test ! ( BigInt , ( BigInt :: from ( 1 ) << i) + 1u32 , py) ;
428
+ test ! ( BigUint , ( BigUint :: from ( 1u32 ) << i) + 1u32 , py) ;
429
+ test ! ( BigInt , ( -BigInt :: from ( 1 ) << i) + 1u32 , py) ;
430
+ test ! ( BigInt , ( BigInt :: from ( 1 ) << i) - 1u32 , py) ;
431
+ test ! ( BigUint , ( BigUint :: from ( 1u32 ) << i) - 1u32 , py) ;
432
+ test ! ( BigInt , ( -BigInt :: from ( 1 ) << i) - 1u32 , py) ;
440
433
}
441
434
}
442
435
}
0 commit comments