Skip to content
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

method argument Vec<&Texture> causing problems after moving from pyo3 v0.8 to v0.11.1 #1177

Closed
paddywwoof opened this issue Sep 11, 2020 · 2 comments
Labels

Comments

@paddywwoof
Copy link
Contributor

This might be just clarification needed in the documentation.

Previously I could make a list of instances of pyo3 class Texture in python and pass that list to a method as Vec<&Texture>. Now however I have to impl Clone for Texture and define the method argument as Vec otherwise I get a compiler error

the trait pyo3::FromPyObject<'_> is not implemented for `std::vec::Vec<&core::Texture>

But this scrambles the behavior. Texture is a pyo3 wrapper for a rust struct containing a large array of pixels as well as OpenGL references. By passing the ownership into the class method the GL ref gets dropped then reused by other Textures. If I don't pass the Texture references in as a list but individually it all works fine. I tried defining the method argument as &PyList but that doesn't help (see question here)

Is there a straightforward way to do this or do I have to refactor my code by ref counting Textures or such like?

@davidhewitt
Copy link
Member

davidhewitt commented Sep 11, 2020

Can you try using Vec<PyRef<Texture>>? I believe that should work. To make pyo3's APIs safe in the face of python's shared mutability model a refcell-like mechanism has been added.

In the future, if you don't ever need Texture instances to be mutated from Python then I'd like to provide a way to opt-out of the pycell system so you can have Vec<&Texture> again - see #1068

@paddywwoof
Copy link
Contributor Author

paddywwoof commented Sep 12, 2020

@davidhewitt That's exactly what I needed. It works fine and is more explicit about what's actually going on than the Vec<&

In this instance the extra overhead unpacking a pycell is low as the GL Texture2D ref is passed relatively infrequently (If it needed to be unpacked for every draw on every buffer on every frame then that would be more of an issue)

Thanks so much for your prompt help.

Paddy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants