made new_py work

This commit is contained in:
Andrei Cravtov
2026-06-14 01:45:15 +01:00
parent 9fb66e313b
commit c2efbfd35d
6 changed files with 73 additions and 5 deletions

1
Cargo.lock generated
View File

@@ -2926,6 +2926,7 @@ dependencies = [
"pyo3-build-config",
"pyo3-ffi",
"pyo3-macros",
"serde",
]
[[package]]

View File

@@ -28,7 +28,7 @@ extend.workspace = true
itertools = "0.14.0"
# interop
pyo3 = { workspace = true, features = ["experimental-async"] }
pyo3 = { workspace = true, features = ["serde", "experimental-async"] }
pyo3-stub-gen.workspace = true
pyo3-async-runtimes = { workspace = true, features = [
"attributes",

View File

@@ -19,7 +19,7 @@ dev = ["exo_rs", "pytest>=8.4.0", "pytest-asyncio>=1.0.0"]
[tool.maturin]
module-name = "exo_rs"
features = ["pyo3/extension-module", "pyo3/experimental-async"]
features = ["pyo3/extension-module", "pyo3/serde", "pyo3/experimental-async"]
[tool.pyo3-stub-gen]
generate-init-py = true

View File

@@ -7,7 +7,7 @@
mod allow_threading;
pub mod config;
mod networking;
mod newtype;
pub mod newtype;
mod pidfile;
use crate::config::config_submodule;

View File

@@ -1,7 +1,12 @@
use crate::ext::ResultExt;
use clap::{ArgMatches, Args, CommandFactory, FromArgMatches, Parser, Subcommand};
use pyo3::pyclass::boolean_struct::False;
use pyo3::{Py, PyClass, PyClassInitializer, PyErr, PyResult, Python};
use pyo3::{
Borrowed, FromPyObject, IntoPyObject, Py, PyAny, PyClass, PyClassInitializer, PyErr, PyResult,
Python,
};
use pyo3_stub_gen::{PyStubType, TypeInfo};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::ffi::OsString;
use std::fmt::{Debug, Display, Formatter};
use std::ops::{Deref, DerefMut};
@@ -85,6 +90,68 @@ impl<T> DerefMut for NewPy<T> {
}
}
impl<'a, 'py, T> FromPyObject<'a, 'py> for NewPy<T>
where
Py<T>: FromPyObject<'a, 'py>,
{
type Error = <Py<T> as FromPyObject<'a, 'py>>::Error;
#[inline(always)]
fn extract(ob: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
<Py<T> as FromPyObject<'a, 'py>>::extract(ob).map(Self::new)
}
}
impl<'py, T> IntoPyObject<'py> for NewPy<T>
where
Py<T>: IntoPyObject<'py>,
{
type Target = <Py<T> as IntoPyObject<'py>>::Target;
type Output = <Py<T> as IntoPyObject<'py>>::Output;
type Error = <Py<T> as IntoPyObject<'py>>::Error;
#[inline(always)]
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
<Py<T> as IntoPyObject<'py>>::into_pyobject(self.into_inner(), py)
}
}
impl<T> Serialize for NewPy<T>
where
Py<T>: Serialize,
{
#[inline(always)]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
<Py<T> as Serialize>::serialize(&*self, serializer)
}
}
impl<'de, T> Deserialize<'de> for NewPy<T>
where
Py<T>: Deserialize<'de>,
{
#[inline(always)]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
<Py<T> as Deserialize<'de>>::deserialize(deserializer).map(Self::new)
}
}
impl<T> PyStubType for NewPy<T>
where
Py<T>: PyStubType,
{
#[inline(always)]
fn type_output() -> TypeInfo {
<Py<T> as PyStubType>::type_output()
}
}
impl<T> NewPy<T> {
#[inline(always)]
fn new(inner: impl Into<Py<T>>) -> Self {

View File

@@ -97,7 +97,7 @@
--release \
--manylinux off \
--manifest-path rust/exo_rs/Cargo.toml \
--features "pyo3/extension-module,pyo3/experimental-async" \
--features "pyo3/extension-module,pyo3/serde,pyo3/experimental-async" \
--interpreter ${pkgs.python313}/bin/python \
--out dist
'';