From b310c0184babe39be478ef032a990e5f1853e24e Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 19 Nov 2024 10:26:07 +0100 Subject: [PATCH] Upgrade to PyO3 0.23 --- instant-distance-py/Cargo.toml | 2 +- instant-distance-py/src/lib.rs | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/instant-distance-py/Cargo.toml b/instant-distance-py/Cargo.toml index 20541fc..120d0fb 100644 --- a/instant-distance-py/Cargo.toml +++ b/instant-distance-py/Cargo.toml @@ -17,6 +17,6 @@ crate-type = ["cdylib"] [dependencies] bincode = "1.3.1" instant-distance = { version = "0.6", path = "../instant-distance", features = ["with-serde"] } -pyo3 = { version = "0.22", features = ["extension-module"] } +pyo3 = { version = "0.23", features = ["extension-module"] } serde = { version = "1", features = ["derive"] } serde-big-array = "0.5.0" diff --git a/instant-distance-py/src/lib.rs b/instant-distance-py/src/lib.rs index 6271427..e05eaa6 100644 --- a/instant-distance-py/src/lib.rs +++ b/instant-distance-py/src/lib.rs @@ -2,16 +2,15 @@ // https://github.com/rust-lang/rust-clippy/issues/12039 #![allow(clippy::unnecessary_fallible_conversions)] -use std::convert::TryFrom; +use std::convert::{Infallible, TryFrom}; use std::fs::File; use std::io::{BufReader, BufWriter}; use std::iter::FromIterator; use instant_distance::Point; -use pyo3::conversion::IntoPy; use pyo3::exceptions::{PyTypeError, PyValueError}; use pyo3::types::{PyAnyMethods, PyList, PyListMethods, PyModule, PyModuleMethods, PyString}; -use pyo3::{pyclass, pymethods, pymodule, Bound}; +use pyo3::{pyclass, pymethods, pymodule, Bound, IntoPyObject}; use pyo3::{Py, PyAny, PyErr, PyObject, PyRef, PyRefMut, PyResult, Python}; use serde::{Deserialize, Serialize}; use serde_big_array::BigArray; @@ -203,7 +202,7 @@ impl Search { item.map(|item| Neighbor { distance: item.distance, pid: item.pid.into_inner(), - value: item.value.into_py(py), + value: item.value.into_pyobject(py).unwrap().unbind(), // Infallible conversion }) } }; @@ -370,7 +369,7 @@ impl TryFrom<&Bound<'_, PyAny>> for FloatArray { fn try_from(value: &Bound<'_, PyAny>) -> Result { let mut new = FloatArray([0.0; DIMENSIONS]); - for (i, val) in value.iter()?.enumerate() { + for (i, val) in value.try_iter()?.enumerate() { match i >= DIMENSIONS { true => return Err(PyTypeError::new_err("point array too long")), false => new.0[i] = val?.extract::()?, @@ -438,10 +437,14 @@ impl TryFrom> for MapValue { } } -impl IntoPy> for &'_ MapValue { - fn into_py(self, py: Python<'_>) -> Py { +impl<'py> IntoPyObject<'py> for &'_ MapValue { + type Target = PyAny; + type Output = Bound<'py, Self::Target>; + type Error = Infallible; + + fn into_pyobject(self, py: Python<'py>) -> Result { match self { - MapValue::String(s) => PyString::new_bound(py, s).into(), + MapValue::String(s) => Ok(PyString::new(py, s).into_any()), } } }