From 39b41b3dc7eaba45892aa7cf1b1c5f65c48e88eb Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 4 May 2020 15:32:20 +0200 Subject: [PATCH] Replace num-traits dependency by std's TryFrom. --- CHANGELOG.md | 1 + Cargo.toml | 1 - src/types/num.rs | 37 +++++++++++++++---------------------- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 689b1c84..6769ef0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/Cargo.toml b/Cargo.toml index 02839551..73aa7708 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/src/types/num.rs b/src/types/num.rs index a2fa5994..e3b8e053 100644 --- a/src/types/num.rs +++ b/src/types/num.rs @@ -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}; @@ -39,10 +39,7 @@ macro_rules! int_fits_larger_int { impl<'source> FromPyObject<'source> for $rust_type { fn extract(obj: &'source PyAny) -> PyResult { 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()) } } }; @@ -146,10 +143,7 @@ macro_rules! int_fits_c_long { val } }?; - match cast::(val) { - Some(v) => Ok(v), - None => Err(exceptions::OverflowError.into()), - } + <$rust_type>::try_from(val).map_err(|_| exceptions::OverflowError.into()) } } }; @@ -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!( @@ -342,11 +335,11 @@ mod bigint_conversion { fn rust_fib(n: usize) -> T where - T: Zero + One, + T: From, for<'a> &'a T: std::ops::Add, { - 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); @@ -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); } } }