From Python, I want to call a Rust function that returns a Python object:
my_rust_module.my_function() # => <object>
I am not sure how to create this function since the PYO3 guide on class instantiation describes how to instantiate such an object as a PyRef
, however I can't work out how to return such a reference from a pyfunction
.
This is what I am trying:
#[pyfunction]
fn my_function(py: Python) -> PyRef {
let gil = Python::acquire_gil();
let py = gil.python();
PyRef::new(py, MyStruct { }).unwrap()
}
However, PyRef
does not seem to be a valid return type (compiler says "wrong number of type arguments: expected 1, found 0"), and I don't know how to convert a PyRef
into something that can be returned such as a PyObject
.