Upgrade to PyO3 0.23

This commit is contained in:
Dirkjan Ochtman 2024-11-19 10:26:07 +01:00
parent 8d01c5c678
commit 944bf9bcca
2 changed files with 12 additions and 9 deletions

View File

@ -17,6 +17,6 @@ crate-type = ["cdylib"]
[dependencies] [dependencies]
bincode = "1.3.1" bincode = "1.3.1"
instant-distance = { version = "0.6", path = "../instant-distance", features = ["with-serde"] } 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 = { version = "1", features = ["derive"] }
serde-big-array = "0.5.0" serde-big-array = "0.5.0"

View File

@ -2,16 +2,15 @@
// https://github.com/rust-lang/rust-clippy/issues/12039 // https://github.com/rust-lang/rust-clippy/issues/12039
#![allow(clippy::unnecessary_fallible_conversions)] #![allow(clippy::unnecessary_fallible_conversions)]
use std::convert::TryFrom; use std::convert::{Infallible, TryFrom};
use std::fs::File; use std::fs::File;
use std::io::{BufReader, BufWriter}; use std::io::{BufReader, BufWriter};
use std::iter::FromIterator; use std::iter::FromIterator;
use instant_distance::Point; use instant_distance::Point;
use pyo3::conversion::IntoPy;
use pyo3::exceptions::{PyTypeError, PyValueError}; use pyo3::exceptions::{PyTypeError, PyValueError};
use pyo3::types::{PyAnyMethods, PyList, PyListMethods, PyModule, PyModuleMethods, PyString}; 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 pyo3::{Py, PyAny, PyErr, PyObject, PyRef, PyRefMut, PyResult, Python};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_big_array::BigArray; use serde_big_array::BigArray;
@ -203,7 +202,7 @@ impl Search {
item.map(|item| Neighbor { item.map(|item| Neighbor {
distance: item.distance, distance: item.distance,
pid: item.pid.into_inner(), 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<Self, Self::Error> { fn try_from(value: &Bound<'_, PyAny>) -> Result<Self, Self::Error> {
let mut new = FloatArray([0.0; DIMENSIONS]); 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 { match i >= DIMENSIONS {
true => return Err(PyTypeError::new_err("point array too long")), true => return Err(PyTypeError::new_err("point array too long")),
false => new.0[i] = val?.extract::<f32>()?, false => new.0[i] = val?.extract::<f32>()?,
@ -438,10 +437,14 @@ impl TryFrom<Bound<'_, PyAny>> for MapValue {
} }
} }
impl IntoPy<Py<PyAny>> for &'_ MapValue { impl<'py> IntoPyObject<'py> for &'_ MapValue {
fn into_py(self, py: Python<'_>) -> Py<PyAny> { type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = Infallible;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
match self { match self {
MapValue::String(s) => PyString::new_bound(py, s).into(), MapValue::String(s) => Ok(PyString::new(py, s).into_any()),
} }
} }
} }