Let us say I have the following Rust struct which becomes a Python class:
#[pyclass]
struct MyRustClass {
foo: i32
}
#[pymethods]
impl MyRustClass {
#[new]
fn new(foo: i32) {
MyRustClass { foo }
}
}
This class is made available to Python, and a new Python class, which is not implemented in Rust at all, holds an instance of this class in an attribute like so, for the purposes of this question, assume that this class is impossible to implement in Rust, and must be entirely Python side:
class MyPythonClass:
def __init__(self, foo: int):
self.my_rust_class = MyRustClass(foo)
Now what I want to do, is be able to expose a #[pyfunction]
which accepts an instance of MyPythonClass
as an argument, and retrieves the MyRustClass
struct from the MyPythonClass.my_rust_class
attribute. This is the part I'm not sure about how to accomplish, but the function signature is something like below, where the python_class
argument is an instance MyPythonClass
:
#[pyfunction]
fn my_cool_function(python_class: PyAny) {
// Get some sort of reference to the MyRustClass struct from the MyPythonClass.my_rust_class attribute
}