Best way of transforming a struct from an external library into a #[pyclass] #4801
Unanswered
MartinJepsen
asked this question in
Questions
Replies: 1 comment
-
This is a hard problem. The only other idea I've heard of is to build something to use rustdoc to generate JSON output about the external library types, and then to generate wrapper code from that JSON output. However I think the JSON output is still not available on stable rust so you'd have to reach for nightly... |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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