From 5b19613a024925af85feace6ed9f05be932d2394 Mon Sep 17 00:00:00 2001 From: Philipp Rehner Date: Tue, 22 Oct 2024 23:01:23 +0200 Subject: [PATCH] rewrite si-units without py-clone feature --- Cargo.toml | 3 +-- si-units/Cargo.toml | 3 +-- si-units/src/lib.rs | 25 ++++++++++++++----------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c91d3cd..64c5d27 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,8 +25,7 @@ approx = "0.5" num-traits = "0.2" typenum = "1.17" pyo3 = { version = "0.22", optional = true } -# numpy = { version = "0.21", optional = true } -numpy = { git = "https://github.com/JRRudy1/rust-numpy", branch = "pyo3-0.22.0", optional = true } +numpy = { version = "0.22", optional = true } [features] default = [] diff --git a/si-units/Cargo.toml b/si-units/Cargo.toml index 129fcfa..f9271d1 100644 --- a/si-units/Cargo.toml +++ b/si-units/Cargo.toml @@ -28,7 +28,6 @@ pyo3 = { version = "0.22", features = [ "abi3", "abi3-py37", ] } -# numpy = "0.21" -numpy = { git = "https://github.com/JRRudy1/rust-numpy", branch = "pyo3-0.22.0" } +numpy = "0.22" thiserror = "1.0" regex = "1.10" diff --git a/si-units/src/lib.rs b/si-units/src/lib.rs index 063b0f4..495605f 100644 --- a/si-units/src/lib.rs +++ b/si-units/src/lib.rs @@ -28,7 +28,6 @@ pub enum QuantityError { } #[pyclass(name = "SIObject", module = "si_units")] -#[derive(Clone)] pub struct PySIObject { value: PyObject, unit: SIUnit, @@ -176,7 +175,7 @@ impl PySIObject { /// Returns /// ------- /// bool - pub fn has_unit(&self, other: Self) -> bool { + pub fn has_unit(&self, other: PyRef<'_, Self>) -> bool { self.unit.eq(&other.unit) } @@ -204,7 +203,8 @@ impl PySIObject { } fn __mul__<'py>(&self, rhs: &Bound<'py, PyAny>) -> PyResult> { - let (rhs_value, unit) = if let Ok(r) = rhs.extract::() { + let (rhs_value, unit) = if let Ok(r) = rhs.downcast::() { + let r = r.borrow(); (r.value.bind(rhs.py()).clone(), self.unit * r.unit) } else { (rhs.clone(), self.unit) @@ -220,7 +220,8 @@ impl PySIObject { } fn __rmul__<'py>(&self, lhs: &Bound<'py, PyAny>) -> PyResult> { - let (lhs_value, unit) = if let Ok(l) = lhs.extract::() { + let (lhs_value, unit) = if let Ok(l) = lhs.downcast::() { + let l = l.borrow(); (l.value.bind(lhs.py()).clone(), l.unit * self.unit) } else { (lhs.clone(), self.unit) @@ -236,7 +237,8 @@ impl PySIObject { } fn __truediv__<'py>(&self, rhs: &Bound<'py, PyAny>) -> PyResult> { - let (rhs_value, unit) = if let Ok(r) = rhs.extract::() { + let (rhs_value, unit) = if let Ok(r) = rhs.downcast::() { + let r = r.borrow(); (r.value.bind(rhs.py()).clone(), self.unit / r.unit) } else { (rhs.clone(), self.unit) @@ -252,7 +254,8 @@ impl PySIObject { } fn __rtruediv__<'py>(&self, lhs: &Bound<'py, PyAny>) -> PyResult> { - let (lhs_value, unit) = if let Ok(l) = lhs.extract::() { + let (lhs_value, unit) = if let Ok(l) = lhs.downcast::() { + let l = l.borrow(); (l.value.bind(lhs.py()).clone(), l.unit / self.unit) } else { (lhs.clone(), self.unit.recip()) @@ -331,7 +334,7 @@ impl SIObject { impl<'py> FromPyObject<'py> for SINumber { fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { let py = ob.py(); - let ob = ob.extract::()?; + let ob = ob.downcast::()?.borrow(); let value = ob.value.extract::(py)?; let unit = ob.unit; Ok(SINumber { value, unit }) @@ -345,12 +348,12 @@ struct SIArray1; impl SIArray1 { #[staticmethod] #[expect(clippy::new_ret_no_self)] - fn new(value: Bound<'_, PyAny>) -> PyResult { + fn new(value: Bound<'_, PyAny>) -> PyResult> { let py = value.py(); if let Ok(v) = value.extract::() { let value = arr1(&[1.0]) * v.value; let value = value.into_pyarray_bound(py).into_any().unbind(); - Ok(PySIObject::new(value, v.unit)) + Bound::new(py, PySIObject::new(value, v.unit)) } else if let Ok(v) = value.extract::>() { let mut unit = SIUnit::DIMENSIONLESS; let (value, units): (Vec<_>, Vec<_>) = v.into_iter().map(|v| (v.value, v.unit)).unzip(); @@ -366,9 +369,9 @@ impl SIArray1 { } let value: Array1<_> = Array1::from_vec(value); let value = value.into_pyarray_bound(py).into_any().unbind(); - Ok(PySIObject::new(value, unit)) + Bound::new(py, PySIObject::new(value, unit)) } else { - value.extract::() + Ok(value.downcast_into::()?) } }