From c2efbfd35d5eced25e77a8b3ce2e375da11ca7ea Mon Sep 17 00:00:00 2001 From: Andrei Cravtov Date: Sun, 14 Jun 2026 01:45:15 +0100 Subject: [PATCH] made new_py work --- Cargo.lock | 1 + rust/exo_rs/Cargo.toml | 2 +- rust/exo_rs/pyproject.toml | 2 +- rust/exo_rs/src/lib.rs | 2 +- rust/exo_rs/src/newtype/new_py.rs | 69 ++++++++++++++++++++++++++++++- rust/parts.nix | 2 +- 6 files changed, 73 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 417a00e12..9272f9742 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2926,6 +2926,7 @@ dependencies = [ "pyo3-build-config", "pyo3-ffi", "pyo3-macros", + "serde", ] [[package]] diff --git a/rust/exo_rs/Cargo.toml b/rust/exo_rs/Cargo.toml index 9eb21e964..189de8e69 100644 --- a/rust/exo_rs/Cargo.toml +++ b/rust/exo_rs/Cargo.toml @@ -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", diff --git a/rust/exo_rs/pyproject.toml b/rust/exo_rs/pyproject.toml index b13533581..df6c123ee 100644 --- a/rust/exo_rs/pyproject.toml +++ b/rust/exo_rs/pyproject.toml @@ -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 diff --git a/rust/exo_rs/src/lib.rs b/rust/exo_rs/src/lib.rs index 487cc9ae8..8971740cf 100644 --- a/rust/exo_rs/src/lib.rs +++ b/rust/exo_rs/src/lib.rs @@ -7,7 +7,7 @@ mod allow_threading; pub mod config; mod networking; -mod newtype; +pub mod newtype; mod pidfile; use crate::config::config_submodule; diff --git a/rust/exo_rs/src/newtype/new_py.rs b/rust/exo_rs/src/newtype/new_py.rs index 9e2971628..0cb0dffea 100644 --- a/rust/exo_rs/src/newtype/new_py.rs +++ b/rust/exo_rs/src/newtype/new_py.rs @@ -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 DerefMut for NewPy { } } +impl<'a, 'py, T> FromPyObject<'a, 'py> for NewPy +where + Py: FromPyObject<'a, 'py>, +{ + type Error = as FromPyObject<'a, 'py>>::Error; + + #[inline(always)] + fn extract(ob: Borrowed<'a, 'py, PyAny>) -> Result { + as FromPyObject<'a, 'py>>::extract(ob).map(Self::new) + } +} + +impl<'py, T> IntoPyObject<'py> for NewPy +where + Py: IntoPyObject<'py>, +{ + type Target = as IntoPyObject<'py>>::Target; + type Output = as IntoPyObject<'py>>::Output; + type Error = as IntoPyObject<'py>>::Error; + + #[inline(always)] + fn into_pyobject(self, py: Python<'py>) -> Result { + as IntoPyObject<'py>>::into_pyobject(self.into_inner(), py) + } +} + +impl Serialize for NewPy +where + Py: Serialize, +{ + #[inline(always)] + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + as Serialize>::serialize(&*self, serializer) + } +} + +impl<'de, T> Deserialize<'de> for NewPy +where + Py: Deserialize<'de>, +{ + #[inline(always)] + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + as Deserialize<'de>>::deserialize(deserializer).map(Self::new) + } +} + +impl PyStubType for NewPy +where + Py: PyStubType, +{ + #[inline(always)] + fn type_output() -> TypeInfo { + as PyStubType>::type_output() + } +} + impl NewPy { #[inline(always)] fn new(inner: impl Into>) -> Self { diff --git a/rust/parts.nix b/rust/parts.nix index 3fa8e2dab..cdb5085d6 100644 --- a/rust/parts.nix +++ b/rust/parts.nix @@ -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 '';