But adding a generic lifetime annotation to the struct gives a "#[pyclass] cannot have generic parameters" error. A static lifetime for the PyArray causes errors if I try to create an instance of the struct in the implementation of ToPyObject.
Wrapping the PyArray in a Py struct did the trick. Thanks!
BTW, when converting from a Rust vector to a PyArray, does it matter whether I use:
PyArray::from_vec(py, rust_vector).to_owned()
or
rust_vector.to_pyarray(py).to_owned()
The docs at numpy::array::PyArray - Rust indicate that the first approach using from_vec() doesn't allocate memory on the Python heap while the second approach using to_pyarray() does, but does the to_owned() method allocate memory and perform a deep copy?
I've tried both approaches and they both seem to work but I'm not sure what the advantage of one over the other is in this case.
does the to_owned() method allocate memory and perform a deep copy?
I don't really know how rust-numpy works, but that shouldn't be the case.
Python manages objects similarly to how Rc<...> works in Rust - nobody owns the inner object, cloning increments the reference count. Going from &PyXXX to Py<PyXXX> just increments the reference count.