Skip to content

Commit c4f3653

Browse files
authored
Merge pull request #895 from fusion-engineering-forks/tryfrom
Replace num-traits dependency by std's TryFrom.
2 parents 9f18610 + 39b41b3 commit c4f3653

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2828

2929
### Removed
3030
* `PyMethodsProtocol` is now renamed to `PyMethodsImpl` and hidden. [#889](https://github.com/PyO3/pyo3/pull/889)
31+
* `num-traits` is no longer a dependency. [#895](https://github.com/PyO3/pyo3/pull/895)
3132

3233

3334
## [0.9.2]

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ inventory = "0.1.4"
2424
libc = "0.2.62"
2525
num-bigint = { version = "0.2", optional = true }
2626
num-complex = { version = "0.2", optional = true }
27-
num-traits = "0.2.8"
2827
parking_lot = { version = "0.10.2" }
2928
paste = "0.1.6"
3029
pyo3cls = { path = "pyo3cls", version = "=0.9.2" }

src/types/num.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
exceptions, ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyNativeType, PyObject,
77
PyResult, Python, ToPyObject,
88
};
9-
use num_traits::cast::cast;
9+
use std::convert::TryFrom;
1010
use std::i64;
1111
use std::os::raw::{c_int, c_long, c_uchar};
1212

@@ -39,10 +39,7 @@ macro_rules! int_fits_larger_int {
3939
impl<'source> FromPyObject<'source> for $rust_type {
4040
fn extract(obj: &'source PyAny) -> PyResult<Self> {
4141
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())
4643
}
4744
}
4845
};
@@ -146,10 +143,7 @@ macro_rules! int_fits_c_long {
146143
val
147144
}
148145
}?;
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())
153147
}
154148
}
155149
};
@@ -322,7 +316,6 @@ mod bigint_conversion {
322316
use super::*;
323317
use crate::types::{PyDict, PyModule};
324318
use indoc::indoc;
325-
use num_traits::{One, Zero};
326319

327320
fn python_fib(py: Python) -> &PyModule {
328321
let fib_code = indoc!(
@@ -342,11 +335,11 @@ mod bigint_conversion {
342335

343336
fn rust_fib<T>(n: usize) -> T
344337
where
345-
T: Zero + One,
338+
T: From<u16>,
346339
for<'a> &'a T: std::ops::Add<Output = T>,
347340
{
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);
350343
for _ in 0..n {
351344
let f2 = &f0 + &f1;
352345
f0 = std::mem::replace(&mut f1, f2);
@@ -428,15 +421,15 @@ mod bigint_conversion {
428421
test!(BigInt, BigInt::from(i), py);
429422
test!(BigUint, BigUint::from(i), py);
430423
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);
440433
}
441434
}
442435
}

0 commit comments

Comments
 (0)