Upgrade to PyO3 0.23

This commit is contained in:
Simon Hausmann 2025-04-03 10:55:55 +02:00 committed by Simon Hausmann
parent ad74c31ed4
commit 0c51f69b0a
6 changed files with 45 additions and 39 deletions

View file

@ -3,6 +3,7 @@
use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyo3::IntoPyObjectExt;
use pyo3_stub_gen::{derive::gen_stub_pyclass, derive::gen_stub_pymethods};
use std::collections::HashMap;
@ -11,49 +12,46 @@ use std::collections::HashMap;
pub struct PyValue(pub slint_interpreter::Value);
struct PyValueRef<'a>(&'a slint_interpreter::Value);
impl IntoPy<PyObject> for PyValue {
fn into_py(self, py: Python<'_>) -> PyObject {
impl<'py> IntoPyObject<'py> for PyValue {
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
// Share the conversion code below that operates on the reference
self.to_object(py).into_py(py)
PyValueRef(&self.0).into_pyobject(py)
}
}
impl ToPyObject for PyValue {
fn to_object(&self, py: Python<'_>) -> PyObject {
PyValueRef(&self.0).to_object(py)
}
}
impl<'a, 'py> IntoPyObject<'py> for PyValueRef<'a> {
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = PyErr;
impl IntoPy<PyObject> for PyValueRef<'_> {
fn into_py(self, py: Python<'_>) -> PyObject {
// Share the conversion code below that operates on the reference
self.to_object(py).into_py(py)
}
}
impl ToPyObject for PyValueRef<'_> {
fn to_object(&self, py: Python<'_>) -> PyObject {
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
match &self.0 {
slint_interpreter::Value::Void => ().into_py(py),
slint_interpreter::Value::Number(num) => num.into_py(py),
slint_interpreter::Value::String(str) => str.into_py(py),
slint_interpreter::Value::Bool(b) => b.into_py(py),
slint_interpreter::Value::Void => ().into_bound_py_any(py),
slint_interpreter::Value::Number(num) => num.into_bound_py_any(py),
slint_interpreter::Value::String(str) => str.into_bound_py_any(py),
slint_interpreter::Value::Bool(b) => b.into_bound_py_any(py),
slint_interpreter::Value::Image(image) => {
crate::image::PyImage::from(image).into_py(py)
crate::image::PyImage::from(image).into_bound_py_any(py)
}
slint_interpreter::Value::Model(model) => {
crate::models::PyModelShared::rust_into_js_model(model, py)
.unwrap_or_else(|| crate::models::ReadOnlyRustModel::from(model).into_py(py))
crate::models::PyModelShared::rust_into_js_model(model, py).map_or_else(
|| crate::models::ReadOnlyRustModel::from(model).into_bound_py_any(py),
|m| Ok(m),
)
}
slint_interpreter::Value::Struct(structval) => {
PyStruct { data: structval.clone() }.into_py(py)
PyStruct { data: structval.clone() }.into_bound_py_any(py)
}
slint_interpreter::Value::Brush(brush) => {
crate::brush::PyBrush::from(brush.clone()).into_py(py)
crate::brush::PyBrush::from(brush.clone()).into_bound_py_any(py)
}
v @ _ => {
eprintln!("Python: conversion from slint to python needed for {v:#?} and not implemented yet");
().into_py(py)
().into_bound_py_any(py)
}
}
}