Best way of transforming a struct from an external library into a #[pyclass] #4801
Unanswered
MartinJepsen
asked this question in
Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Related:
pub struct PyRustStruct(external_crate::RustStruct)
does the trick, but still requires decorating all structs.Hello,
I have a Rust library with a lot of structs. Most of them have primitive-typed fields. I want to transform them into read-only
#[pyclass]
so their fields are exposed in Python in a read-only fashion.What I want to achieve
Let's take this struct as an example:
I wish to transform this struct into another Python-compatible struct (
#[pyclass]
) with the following properties:Py
#[getter]
for each public field.#[new]
.Based on these properties,
RustStruct
should be transformed intoApproaches I've considered
I've mainly considered writing a macro that wraps
RustStruct
and simply generates the equivalentPyRustStruct
in the following way:The corresponding call to transform
RustStruct
would then be:However, It looks like I can't access the struct definition merely from the identifier
RustStruct
, as it won't parse into anItemStruct
. So far, the only solution I've found is to generate a derive macro, that wraps the entire struct definition or decorates the struct definition, like so:But that would require me to alter the structs where they are defined. Because they come from an external crate, It's not ideal to do this.
Any suggestions are welcome!
Beta Was this translation helpful? Give feedback.
All reactions