考虑一个简单的 Rust 类通过 PyO3 暴露给 Python。
现在有一个函数,应该以这两个结构体的方式从Python中调用:
因此,人们定义了
现在编译器声称
use pyo3::prelude::*;
#[pyclass(name=MyClass)]
pub struct PyMyClass {
// Some fields
}
#[pymethods]
impl PyMyStruct {
#[new]
fn py_new(obj: PyRawObject) {
obj.init({
PyMyStruct {
// ...
}
});
}
}
现在有一个函数,应该以这两个结构体的方式从Python中调用:
a = MyStruct()
b = MyStruct()
c = foo(a,b)
因此,人们定义了
#[pyfunction]
fn foo(a: PyMyStruct, b: PyMyStruct) -> PyResult<PyMyStruct> {
// some high performance logic implemented in rust ...
}
现在编译器声称
PyMyStruct
应该实现特质FromPyObject
:impl FromPyObject<'_> for PyMyStruct {
fn extract(ob: &'_ PyAny) ->PyResult<Self> {
// I dont know what to do here :(
}
}
但我不知道如何从PyAny
中检索出PyMyStruct
的实例、指针或其他东西... 有人能帮我吗?