Skip to content

Commit

Permalink
Merge pull request #895 from fusion-engineering-forks/tryfrom
Browse files Browse the repository at this point in the history
Replace num-traits dependency by std's TryFrom.
  • Loading branch information
kngwyu authored May 5, 2020
2 parents 9f18610 + 39b41b3 commit c4f3653
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

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


## [0.9.2]
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ inventory = "0.1.4"
libc = "0.2.62"
num-bigint = { version = "0.2", optional = true }
num-complex = { version = "0.2", optional = true }
num-traits = "0.2.8"
parking_lot = { version = "0.10.2" }
paste = "0.1.6"
pyo3cls = { path = "pyo3cls", version = "=0.9.2" }
Expand Down
37 changes: 15 additions & 22 deletions src/types/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
exceptions, ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyNativeType, PyObject,
PyResult, Python, ToPyObject,
};
use num_traits::cast::cast;
use std::convert::TryFrom;
use std::i64;
use std::os::raw::{c_int, c_long, c_uchar};

Expand Down Expand Up @@ -39,10 +39,7 @@ macro_rules! int_fits_larger_int {
impl<'source> FromPyObject<'source> for $rust_type {
fn extract(obj: &'source PyAny) -> PyResult<Self> {
let val = $crate::objectprotocol::ObjectProtocol::extract::<$larger_type>(obj)?;
match cast::<$larger_type, $rust_type>(val) {
Some(v) => Ok(v),
None => Err(exceptions::OverflowError.into()),
}
<$rust_type>::try_from(val).map_err(|_| exceptions::OverflowError.into())
}
}
};
Expand Down Expand Up @@ -146,10 +143,7 @@ macro_rules! int_fits_c_long {
val
}
}?;
match cast::<c_long, $rust_type>(val) {
Some(v) => Ok(v),
None => Err(exceptions::OverflowError.into()),
}
<$rust_type>::try_from(val).map_err(|_| exceptions::OverflowError.into())
}
}
};
Expand Down Expand Up @@ -322,7 +316,6 @@ mod bigint_conversion {
use super::*;
use crate::types::{PyDict, PyModule};
use indoc::indoc;
use num_traits::{One, Zero};

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

fn rust_fib<T>(n: usize) -> T
where
T: Zero + One,
T: From<u16>,
for<'a> &'a T: std::ops::Add<Output = T>,
{
let mut f0: T = Zero::zero();
let mut f1: T = One::one();
let mut f0: T = T::from(0);
let mut f1: T = T::from(1);
for _ in 0..n {
let f2 = &f0 + &f1;
f0 = std::mem::replace(&mut f1, f2);
Expand Down Expand Up @@ -428,15 +421,15 @@ mod bigint_conversion {
test!(BigInt, BigInt::from(i), py);
test!(BigUint, BigUint::from(i), py);
test!(BigInt, -BigInt::from(i), py);
test!(BigInt, BigInt::one() << i, py);
test!(BigUint, BigUint::one() << i, py);
test!(BigInt, -BigInt::one() << i, py);
test!(BigInt, (BigInt::one() << i) + 1u32, py);
test!(BigUint, (BigUint::one() << i) + 1u32, py);
test!(BigInt, (-BigInt::one() << i) + 1u32, py);
test!(BigInt, (BigInt::one() << i) - 1u32, py);
test!(BigUint, (BigUint::one() << i) - 1u32, py);
test!(BigInt, (-BigInt::one() << i) - 1u32, py);
test!(BigInt, BigInt::from(1) << i, py);
test!(BigUint, BigUint::from(1u32) << i, py);
test!(BigInt, -BigInt::from(1) << i, py);
test!(BigInt, (BigInt::from(1) << i) + 1u32, py);
test!(BigUint, (BigUint::from(1u32) << i) + 1u32, py);
test!(BigInt, (-BigInt::from(1) << i) + 1u32, py);
test!(BigInt, (BigInt::from(1) << i) - 1u32, py);
test!(BigUint, (BigUint::from(1u32) << i) - 1u32, py);
test!(BigInt, (-BigInt::from(1) << i) - 1u32, py);
}
}
}
Expand Down

0 comments on commit c4f3653

Please sign in to comment.