Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement the buffer protocol safely #657

Closed
jimy-byerley opened this issue Oct 30, 2019 · 4 comments
Closed

Implement the buffer protocol safely #657

jimy-byerley opened this issue Oct 30, 2019 · 4 comments

Comments

@jimy-byerley
Copy link

jimy-byerley commented Oct 30, 2019

Hi !
I'm using rust as backend for a python module I'm working on, to get high performances on mesh operations. The data is owned by some rust Vecs but I would like to expose its content to python, to make possible extend the module with pure python (or at least to get some data points).

So I'm trying to implement the buffer protocol for my structs. I seen this page in pyo3. But it relies on pointers so it's unsafe.
But the buffer protocol is not incompatible with pyo3's safety since i read in the python doc that the memoryview object kept a hook on the viewed object

Is there a way to safely implement the buffer protocol for rust objects exposed by a #[pyclass] statement ?

@kngwyu
Copy link
Member

kngwyu commented Oct 30, 2019

Thank you for reporting it.
I think our current implementation is really bad and needs to be refined.
I haven't done refactoring for class/buffer.rs(because of some reasons: its complicated design, integration with PyBuffer, and difficulty to sync with rust-numpy), but it's time to do it.

@jimy-byerley
Copy link
Author

I don't know for the code that is behind it (I can imagine that it can be easily headache with all these structs and traits ;) but I find the API of the PyBuffer quite elegant by now: one method to get it from any object, one method to get the buffer in the final type :)

@kngwyu
Copy link
Member

kngwyu commented Oct 30, 2019

Sorry, I'm saying about class/buffer.rs where PyBufferProtocol is.
Though PyBuffer also can be changed a bit...(e.g., it can have lifetime py)

@davidhewitt
Copy link
Member

This is now tracked in #1884 as part of the move to #[pymethods].

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants