I have an external rust crate, and I would like to build some subset of it as a Python extension, all the while keeping the original crate independent from pyo3 (among other things due to the Rust nightly requirement). Is this currently possible and could you please outline the general approach? Currently, I created a second crate that depends on pyo3 and the original crate that I want to wrap, but I'm not sure what to do next.
For instance, taking the example from https://pyo3.rs/v0.5.2/class.html , but assuming that we have a struct with an implementation in an external crate that we would like to expose in Python. Is it possible to apply the pyclass macro to an external struct? If one needs to write some wrapper, how would it look roughly? Thank you!
Is it possible to apply the pyclass macro to an external struct?
It's impossible for 2 reasons.
impl A for B have to be in the crate where A is defined, or the crate where B is defined).I think the easiest way is to define a wrapper struct, llike
#[pyclass]
pub struct PyWrapper(ExternalStruct);
#[pymethods]
impl PyWrapper {
#[new]
fn __new__(obj: &PyRawObject, /*some python objects you want to pass*/) -> PyResult<()> {
obj.init(|_token| PyWrapper(ExternalStruct {..})
}
// write methods you want to use from python side
Thank you for the explanation! I understand.
I tried the above suggestion, however,
#[pyclass]
pub struct PyWrapper(ExternalStruct);
produces
error: custom attribute panicked
--> src/lib.rs:31:1
|
31 | #[pyclass]
| ^^^^^^^^^^
|
= help: message: #[pyclass] can only be used with C-style structs
error: aborting due to previous error
where ExternalStruct is defined in a separate crate as,
pub struct ExternalStruct {
lowercase: bool,
token_pattern: String,
n_features: u32,
}
This happens due to this condition not passing in PyO3 but I'm not sure what could be a way around it?
Sorry, it looks tuple struct is currently unsupported :sweat:
How about
#[pyclass]
pub struct PyWrapper {
inner: ExternalStruct,
}
?
Yes, that works, thank you @kngwyu !
Most helpful comment
It's impossible for 2 reasons.
impl A for Bhave to be in the crate where A is defined, or the crate where B is defined).I think the easiest way is to define a wrapper struct, llike