From d03b6d138b9c8074b1f7f04df2bee91dc23d3dec Mon Sep 17 00:00:00 2001 From: haixuantao Date: Mon, 14 Jul 2025 13:41:38 +0200 Subject: [PATCH] Add pyo3 special method --- Cargo.lock | 47 +++++++++++++++++++ apis/python/node/Cargo.toml | 2 +- apis/python/node/src/lib.rs | 4 +- .../extensions/ros2-bridge/python/Cargo.toml | 2 +- .../extensions/ros2-bridge/python/src/lib.rs | 15 +++--- .../extensions/ros2-bridge/python/src/qos.rs | 11 ++--- 6 files changed, 62 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ff733ad..f86715e0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3230,6 +3230,7 @@ dependencies = [ "futures", "pyo3", "pyo3-build-config", + "pyo3_special_method_derive", "pythonize", "serde_yaml 0.9.34+deprecated", "tokio", @@ -3414,6 +3415,7 @@ dependencies = [ "eyre", "futures", "pyo3", + "pyo3_special_method_derive", "serde", "serde_assert", ] @@ -8992,6 +8994,29 @@ dependencies = [ "syn 2.0.101", ] +[[package]] +name = "pyo3_special_method_derive" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b75ccfc0eef7c0478db8aa23d23e97b39c74e29da21d9bf1e1934e63ad0709d" +dependencies = [ + "pyo3", + "pyo3_special_method_derive_macro", +] + +[[package]] +name = "pyo3_special_method_derive_macro" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfc6e17b94c919f8295729dcddd368b7cd78231c39a3c23cdeec77b5834010cf" +dependencies = [ + "proc-macro2", + "pyo3", + "quote", + "quote_into", + "syn 2.0.101", +] + [[package]] name = "python3-dll-a" version = "0.2.13" @@ -9178,6 +9203,28 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "quote_into" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93886ed56f228a5d960fc4d26afa3736df12a251872869cf24f5efe5f07699b9" +dependencies = [ + "proc-macro2", + "quote", + "quote_into_macro", +] + +[[package]] +name = "quote_into_macro" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36b828998c40452b5afe441c75194e93181432e669585f4ceb7b0d32a3f73525" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "r-efi" version = "5.2.0" diff --git a/apis/python/node/Cargo.toml b/apis/python/node/Cargo.toml index f03a4036..58832ffd 100644 --- a/apis/python/node/Cargo.toml +++ b/apis/python/node/Cargo.toml @@ -30,7 +30,7 @@ arrow = { workspace = true, features = ["pyarrow"] } pythonize = { workspace = true } futures = "0.3.28" dora-ros2-bridge-python = { workspace = true } -# pyo3_special_method_derive = "0.4.2" +pyo3_special_method_derive = "0.4.3" tokio = { version = "1.24.2", features = ["rt"] } [build-dependencies] diff --git a/apis/python/node/src/lib.rs b/apis/python/node/src/lib.rs index 6b279ca1..a203f0ba 100644 --- a/apis/python/node/src/lib.rs +++ b/apis/python/node/src/lib.rs @@ -17,7 +17,7 @@ use eyre::Context; use futures::{Stream, StreamExt}; use pyo3::prelude::*; use pyo3::types::{PyBytes, PyDict}; -/// use pyo3_special_method_derive::{Dict, Dir, Repr, Str}; +use pyo3_special_method_derive::{Dict, Dir, Repr, Str}; /// The custom node API lets you integrate `dora` into your application. /// It allows you to retrieve input and send output in any fashion you want. @@ -32,7 +32,7 @@ use pyo3::types::{PyBytes, PyDict}; /// /// :type node_id: str, optional #[pyclass] -/// #[derive(Dir, Dict, Str, Repr)] +#[derive(Dir, Dict, Str, Repr)] pub struct Node { events: Events, node: DelayedCleanup, diff --git a/libraries/extensions/ros2-bridge/python/Cargo.toml b/libraries/extensions/ros2-bridge/python/Cargo.toml index 9d8f94cb..b9253fb7 100644 --- a/libraries/extensions/ros2-bridge/python/Cargo.toml +++ b/libraries/extensions/ros2-bridge/python/Cargo.toml @@ -12,7 +12,7 @@ eyre = "0.6" serde = "1.0.166" arrow = { workspace = true, features = ["pyarrow"] } futures = "0.3.28" -# pyo3_special_method_derive = "0.4.2" +pyo3_special_method_derive = "0.4.3" [dev-dependencies] serde_assert = "0.7.1" diff --git a/libraries/extensions/ros2-bridge/python/src/lib.rs b/libraries/extensions/ros2-bridge/python/src/lib.rs index a8c206b9..14984245 100644 --- a/libraries/extensions/ros2-bridge/python/src/lib.rs +++ b/libraries/extensions/ros2-bridge/python/src/lib.rs @@ -18,7 +18,7 @@ use pyo3::{ types::{PyAnyMethods, PyDict, PyList, PyModule, PyModuleMethods}, Bound, PyAny, PyObject, PyResult, Python, }; -/// use pyo3_special_method_derive::{Dict, Dir, Repr, Str}; +use pyo3_special_method_derive::{Dict, Dir, Repr, Str}; use typed::{deserialize::StructDeserializer, TypeInfo, TypedValue}; pub mod qos; @@ -46,7 +46,7 @@ pub mod typed; /// :type ros_paths: typing.List[str], optional /// #[pyclass] -/// #[derive(Str, Repr, Dir, Dict)] +#[derive(Str, Repr, Dir, Dict)] pub struct Ros2Context { context: ros2_client::Context, messages: Arc>>, @@ -150,7 +150,7 @@ impl Ros2Context { /// See: https://github.com/jhelovuo/ros2-client/issues/4 /// #[pyclass] -/// #[derive(Str, Repr, Dir, Dict)] +#[derive(Str, Repr, Dir, Dict)] pub struct Ros2Node { node: ros2_client::Node, messages: Arc>>, @@ -257,8 +257,7 @@ impl Ros2Node { /// ROS2 Node Options /// :type rosout: bool, optional /// -#[derive(Clone, Default)] -/// , Str, Repr, Dir, Dict)] +#[derive(Clone, Default, Str, Repr, Dir, Dict)] #[pyclass] #[non_exhaustive] pub struct Ros2NodeOptions { @@ -289,7 +288,7 @@ impl From for ros2_client::NodeOptions { /// - dora Ros2 bridge functionality is considered **unstable**. It may be changed /// at any point without it being considered a breaking change. #[pyclass] -/// #[derive(Str, Repr, Dir, Dict)] +#[derive(Str, Repr, Dir, Dict)] #[non_exhaustive] pub struct Ros2Topic { topic: rustdds::Topic, @@ -302,7 +301,7 @@ pub struct Ros2Topic { /// - dora Ros2 bridge functionality is considered **unstable**. It may be changed /// at any point without it being considered a breaking change. #[pyclass] -/// #[derive(Str, Repr, Dir, Dict)] +#[derive(Str, Repr, Dir, Dict)] #[non_exhaustive] pub struct Ros2Publisher { publisher: ros2_client::Publisher>, @@ -373,7 +372,7 @@ impl Ros2Publisher { /// - dora Ros2 bridge functionality is considered **unstable**. It may be changed /// at any point without it being considered a breaking change. #[pyclass] -/// #[derive(Str, Repr, Dir, Dict)] +#[derive(Str, Repr, Dir, Dict)] #[non_exhaustive] pub struct Ros2Subscription { deserializer: StructDeserializer<'static>, diff --git a/libraries/extensions/ros2-bridge/python/src/qos.rs b/libraries/extensions/ros2-bridge/python/src/qos.rs index 24c10cbc..315b8ec1 100644 --- a/libraries/extensions/ros2-bridge/python/src/qos.rs +++ b/libraries/extensions/ros2-bridge/python/src/qos.rs @@ -1,6 +1,6 @@ use ::dora_ros2_bridge::rustdds::{self, policy}; use pyo3::prelude::{pyclass, pymethods}; -/// use pyo3_special_method_derive::{Dict, Dir, Repr, Str}; +use pyo3_special_method_derive::{Dict, Dir, Repr, Str}; /// ROS2 QoS Policy /// @@ -13,8 +13,7 @@ use pyo3::prelude::{pyclass, pymethods}; /// :type keep_last: int, optional /// :rtype: dora.Ros2QoSPolicies /// -#[derive(Clone)] -/// , Str, Repr, Dir, Dict)] +#[derive(Clone, Str, Repr, Dir, Dict)] #[pyclass] #[non_exhaustive] pub struct Ros2QosPolicies { @@ -80,8 +79,7 @@ impl From for rustdds::QosPolicies { /// DDS 2.2.3.4 DURABILITY /// /// :rtype: dora.Ros2Durability -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -/// , Str, Repr, Dir, Dict)] +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Str, Repr, Dir, Dict)] #[pyclass(eq, eq_int)] pub enum Ros2Durability { Volatile, @@ -107,8 +105,7 @@ impl From for policy::Durability { /// DDS 2.2.3.11 LIVELINESS /// :rtype: dora.Ros2Liveliness -#[derive(Copy, Clone, PartialEq)] -/// , Str, Repr, Dir, Dict)] +#[derive(Copy, Clone, PartialEq, Str, Repr, Dir, Dict)] #[pyclass(eq, eq_int)] pub enum Ros2Liveliness { Automatic,