-
Notifications
You must be signed in to change notification settings - Fork 787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrapping external rust crate #287
Comments
It's impossible for 2 reasons.
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
where 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 😓 #[pyclass]
pub struct PyWrapper {
inner: ExternalStruct,
} ? |
Yes, that works, thank you @kngwyu ! |
#[pyclass]
pub struct PyWrapper(ExternalStruct); @kngwyu Is there any reason why tuple struct cannot be supported? Is it possible to add support for it? |
From a technical level, I think it should be possible to support. Design-wise I think it makes sense to support it; the prevailing direction of PyO3 is (imo) write natural Rust code when writing Python bindings. I think the first step would be to add support in the parsing code here: pyo3/pyo3-macros-backend/src/pyclass.rs Line 182 in acf7271
... and then add a bunch of tests and see if anything else needs adjusting to support tuple structs of varying length / with |
I don't see any specific reason now, but need to check it more precisely. |
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!The text was updated successfully, but these errors were encountered: