Skip to content

Commit

Permalink
rewrite si-units without py-clone feature
Browse files Browse the repository at this point in the history
  • Loading branch information
prehner committed Oct 22, 2024
1 parent d7b500d commit 5b19613
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
3 changes: 1 addition & 2 deletions si-units/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
25 changes: 14 additions & 11 deletions si-units/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub enum QuantityError {
}

#[pyclass(name = "SIObject", module = "si_units")]
#[derive(Clone)]
pub struct PySIObject {
value: PyObject,
unit: SIUnit,
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -204,7 +203,8 @@ impl PySIObject {
}

fn __mul__<'py>(&self, rhs: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
let (rhs_value, unit) = if let Ok(r) = rhs.extract::<Self>() {
let (rhs_value, unit) = if let Ok(r) = rhs.downcast::<Self>() {
let r = r.borrow();
(r.value.bind(rhs.py()).clone(), self.unit * r.unit)
} else {
(rhs.clone(), self.unit)
Expand All @@ -220,7 +220,8 @@ impl PySIObject {
}

fn __rmul__<'py>(&self, lhs: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
let (lhs_value, unit) = if let Ok(l) = lhs.extract::<Self>() {
let (lhs_value, unit) = if let Ok(l) = lhs.downcast::<Self>() {
let l = l.borrow();
(l.value.bind(lhs.py()).clone(), l.unit * self.unit)
} else {
(lhs.clone(), self.unit)
Expand All @@ -236,7 +237,8 @@ impl PySIObject {
}

fn __truediv__<'py>(&self, rhs: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
let (rhs_value, unit) = if let Ok(r) = rhs.extract::<Self>() {
let (rhs_value, unit) = if let Ok(r) = rhs.downcast::<Self>() {
let r = r.borrow();
(r.value.bind(rhs.py()).clone(), self.unit / r.unit)
} else {
(rhs.clone(), self.unit)
Expand All @@ -252,7 +254,8 @@ impl PySIObject {
}

fn __rtruediv__<'py>(&self, lhs: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
let (lhs_value, unit) = if let Ok(l) = lhs.extract::<Self>() {
let (lhs_value, unit) = if let Ok(l) = lhs.downcast::<Self>() {
let l = l.borrow();
(l.value.bind(lhs.py()).clone(), l.unit / self.unit)
} else {
(lhs.clone(), self.unit.recip())
Expand Down Expand Up @@ -331,7 +334,7 @@ impl<T> SIObject<T> {
impl<'py> FromPyObject<'py> for SINumber {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let py = ob.py();
let ob = ob.extract::<PySIObject>()?;
let ob = ob.downcast::<PySIObject>()?.borrow();
let value = ob.value.extract::<f64>(py)?;
let unit = ob.unit;
Ok(SINumber { value, unit })
Expand All @@ -345,12 +348,12 @@ struct SIArray1;
impl SIArray1 {
#[staticmethod]
#[expect(clippy::new_ret_no_self)]
fn new(value: Bound<'_, PyAny>) -> PyResult<PySIObject> {
fn new(value: Bound<'_, PyAny>) -> PyResult<Bound<'_, PySIObject>> {
let py = value.py();
if let Ok(v) = value.extract::<SINumber>() {
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::<Vec<SINumber>>() {
let mut unit = SIUnit::DIMENSIONLESS;
let (value, units): (Vec<_>, Vec<_>) = v.into_iter().map(|v| (v.value, v.unit)).unzip();
Expand All @@ -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::<PySIObject>()
Ok(value.downcast_into::<PySIObject>()?)
}
}

Expand Down

0 comments on commit 5b19613

Please sign in to comment.